Global Variables and Local Variables
You learned how to create a function and how to use it.
The variable has a working range that works.
Often divided into global and local variables.
Global variables operate on a single file. But it doesn't work inside a function.
Local variables operate within the declared function.
Gloman is hard to explain, so let's look at the code one by one.
Global variables
Global variables refer to variables declared outside a function. For example
$disney = 'mickey';
In the code above, the variable disney is a global variable.
Because you declared it outside the function.
Just this code is hard to understand? Let's declare the mickey global variable and see if it works inside a function.
<?php $disney = 'mickey'; function func(){ echo $disney; return; } func(); ?>
If you look at the result of the code above, you can see that nothing is executed.
It works fine outside the function.
<?php $disney = 'mickey'; function func(){ echo $disney; return; } func(); echo $disney; ?>
Result of the code above
Unlike the first example, the output is from outside the function, so the value of the variable disney is output.
This time, let's declare a variable inside the function to see if it's output outside the function.
<?php function func(){ $disney = 'mickey'; echo 'in function '.$disney; echo '<br>'; return; } func(); echo 'out of function'.$disney; ?>
result
In the image above, the variable disney declared in the function is displayed as normal but it is valid only inside the function, so it is not output outside the function.
So, today we have talked about global and local variables.
Global variables are called Korean in Korean and local variables are called Korean in Korean.
What if you want to use global variables in a function?
See you next time.