External Variables of Functions in PHP
Variables declared outside of a function are not visible inside. Let's look at an example. Suppose we have the following variable:
<?php
$num = 1;
?>
Let's try to output this variable inside a function:
<?php
$num = 1;
function func()
{
echo $num;
}
func(); // error
?>
Explain what the result of the code execution will be:
<?php
$aaa = 111;
function func()
{
echo $aaa;
}
func();
?>
Explain what the result of the code execution will be:
<?php
$num1 = 1;
$num2 = 2;
function func()
{
return $num1 + $num2;
}
echo func();
?>