PHP for the following RECURSIVE function?
function display(array $input)
{
foreach ($input as $key => $value) {
echo $key;
if (is_array($value)) {
/** Call the same function again */
return display($value);
}
echo $value;
}
}
The following works but I get 500 Internal error in the console of the browser
function display(array $input) : mixed
Is there a way I can use UNION Types over here? If yes then which are those
is mixed a return type? i think it should be void or separated by a | if there is more than one return type
Your function doesn't actually return anything, just echoes
I think this was like a mind game for me. With union I can use pipe ( | ) operator I can use multiple
Yup it prints the data. Also it calls the same function (RECURSION) in one of the condition So I cannot just use void as void means nothing. Also if I use void I cant use (union with other data types) any other data types with it
Didn't know about the mixed return type, right now mostly working on php 7.1 supported features as most servers haven't upgraded to php 8.0 yet
Sorry it was late when I read this. Looking at this again, when you recursively call the function you return the result of it, but it doesn't actually return anything. Instead of trying to return the result of recursively calling the function, you should just call it, ie use display($value); instead of return display ($value);
You can upgrade the PHP 7.4 to PHP 8.x in servers with the help of PPA Ondrej Repository. PHP 8.0 is really awesome in terms of performance. They even state that they have 2 JIT compilers as per the feature released for 8.0
You cannot omit the return keyword. If you could run the following example you will understand it. $array = [ 'a' => 'b', 'c' => 'd', 'e' => ['f' => ['g' => ['h' => 'i']]] ]; display($array);
What i mean is making cross php applications, where you leave the user to setup by themselves, for example code canyon projects where you do not know the environment a user will run your app in
Обсуждают сегодня