Turn php variables to functions
I've tried many times to turn Variables into Functions
<?php
$echo = "Hello world!";
function echoIt() {
echo $echo;
}
echoIt();
?>
But return this error
Notice: Undefined variable: echo
Answer
Solution:
Your code won't work because $echo
is not in the function scope.
You don't normally want to do that anyway. It tends to make your code impossible to understand because variables pop up from nowhere and you never know where they get created or modified. Instead, you want to pass each function the values it needs, e.g.:
<?php
function echoIt(string $text): void
{
echo $text;
}
$echo = "Hello world!";
echoIt($echo);
Please note I've also removed the trailing ?>
tag. It isn't required and it can often cause problems when trailing whitespace characters slip.