G
G
ganbatte2020-05-13 13:06:37
PHP
ganbatte, 2020-05-13 13:06:37

One PHP works in a function with no variable set and the other doesn't, why?

This function works for me, but it does not work for another PHP, what does it depend on?

test($test);
 function test($test = "") {
   echo "123";
}

When calling the test function, $test is not set.
In one PHP it passes without any errors, and in the other it writes an error test, undefined variable

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
ThunderCat, 2020-05-13
@ganbatte

In one PHP runs without any errors,
Just because you don't see them doesn't mean they don't exist.

Q
qwermus, 2020-05-13
@qwermus

Error reporting is enabled in one place, disabled in another. A variable must be defined before it can be used. Or, if it is defined elsewhere, then you can add before calling the function:

if (!isset($test)) { // Если переменная не определена
$test = false; // Определим её
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question