We have the following code:

<?php $a = $_POST['a']; $b = $_POST['b']; $act = $_POST['act']; ?> Тут форма, через которую вводим значения переменных <?php function calc($a,$b,$act) { switch ($act) { case "+": $rez = $a + $b; return $rez; break; case "-": $rez = $a - $b; return $rez; break; case "*": $rez = $a * $b; return $rez; break; case "/": $rez = $a / $b; return $rez; break; case "%": $rez = $a % $b; return $rez; break; default: return "Выберите действие"; break; } } $calc = calc($a,$b,$act); echo $calc; 

Is it right to use the same variables ($ a, $ b, $ act) twice in the input data and when calling a function? The code works, but I would like to know how php will see it, will there be conflicts in other versions, for example?

  • one
    The scope of the variable on php.net - read carefully, look at the examples, and everything will immediately become clear to you. - terantul
  • @ sereja322; If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - Vitalina

1 answer 1

Variables inside the function are in the scope of the function, variables outside the function within the function are not available, so there can be no conflicts. Using variables with the same name is normal - there will be less confusion, because the name of a variable should speak as accurately as possible about what data is stored in it.