Answer the question
In order to leave comments, you need to log in
Why does return not return a value?
The question is why return does not return a result.
Example :
<?php
$a = 1;
function test($a)
{
if($a == 5) {
return 'не возвращает значение';
} else {
$a += 1;
echo $a . '<br>'; // Это для проверки
test($a); // Пользуюсь рекурсией, но уже беру $a с новым значением
}
}
$a = test($a);
echo $a; // он должен выводить 5 но он ничего не выводит
Answer the question
In order to leave comments, you need to log in
Because the value of the test function is not returned when called recursively. Should be like this
<?php
function test($a)
{
if($a === 5) {
return 'не возвращает значение';
}
return test($a + 1); // Здесь нужен return
}
echo test(1);
return test($a); // Пользуюсь рекурсией, но уже беру $a с новым значением
echo - Output one or more lines
return returns program control to the module from which the function was called. Program execution continues with the instruction following the call site.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question