In this lesson, you will learn about Variables in PHP, Variable Types, declarations, and assignments, along with examples to better understand the topic.
“Think variables” are the like places where you store data. In PHP, variables begin with the dollar symbol $
, and when you assign a value, PHP declares it.
<!DOCTYPE html> <html> <body> <?PHP $string_Variable = "PHP My String Variable"; $num_Variable = 585; $decimal_Variable = 49.5; ?> </body> </html>
The scope is termed as the range of accessibility of a variable to the source code where it is declared. PHP variables scope types are below:
A local variable in PHP references the function or program in which it is declared or can be defined as function parameters in a function. If you declare variables in a program, you can only use them within the same program.
Example
<?php print "Local Variable Scope Example" . "<br>"; $num_var = 14; function LocalFunction() { $num_var = 0; print "Variable value within body of function is = $num_var" . "<br>"; } LocalFunction(); print "Variable value outside body of function is = $num_var"; ?>
Output
Local Variable Scope Example Variable value within body of function is = 0 Variable value outside body of function is = 14
In the above example, $num_var
variable in declared within LocalFunction()
function, thus $num_var
has a LOCAL SCOPE and can only be accessed from LocalFunction()
. However, $num_var
outside LocalFunction()
shows different value(14).
Function parameters are like conventional variables and are declared after the function name and within parentheses of the function. The example below uses $var_num1
and $var_num2
.
function Sum( $var_num1, $var_num2)
Example
<?PHP // add the two numbers passed and return it to the caller function Sum($var_num1, $var_num2) { $num_res = $var_num1 + $var_num2; return $num_res; } $var_return = Sum(10, 20); // values 10 and 20 pass through sum echo "Function Parameters Example"; echo "<br>"; echo "Sum is ", $var_return; ?>
Output
Function Parameters Example Sum is 30
In PHP, Global variables are declared and accessed anywhere in any program function. Additionally, global variables are claimed to be GLOBAL in the function that needs to be changed. Traditionally, the keyword GLOBAL with the variable makes it global. The GLOBAL keyword tells PHP that a variable is the same one that has already been declared by using it with that variable.
Example
<?PHP //global scope $var_num1 = 12; $var_num2 = 16; $num_res = 0; // Multiply the two numbers passed and access it to the within outside the function function GlobalFunc() { echo "Global Variable Example"; echo "<br>"; echo "_________________________________"; echo "<br>"; global $var_num1; global $var_num2; global $num_res; $num_res = $var_num1 * $var_num1; echo "Global variable within the function"; echo "<br>"; echo "Result = ", $num_res; echo "<br>"; echo "_________________________________"; echo "<br>"; } GlobalFunc(); echo "Global variable outside the function"; echo "<br>"; echo "Result = ", $num_res; echo "<br>"; echo "_________________________________"; echo "<br>"; ?>
Output
Global Variable Example _________________________________ Global variable within the function Result = 144 _________________________________ Global variable outside the function Result = 144 _________________________________
PHP can hold global variables in an array that begins with $GLOBALS[]
and index within the brackets. The index is the variable name that can also be accessible within the program’s function. You can update global variables directly. Let’s examine an example of accessing the array variable within and outside a function.
Example
<?PHP //global scope $var_num1 = 12; $var_num2 = 16; $num_res = 0; // Multiply the two numbers passed and access it to the within outside the function function GlobalFunc() { echo "Global Array Example"; echo "<br>"; echo "_________________________________"; echo "<br>"; $GLOBALS['num_res'] = $GLOBALS['var_num2'] * $GLOBALS['var_num1']; echo "Global Array variable within the function"; echo "<br>"; echo "Result = ", $GLOBALS['num_res']; echo "<br>"; echo "_________________________________"; echo "<br>"; } GlobalFunc(); echo "Global Array variable outside the function"; echo "<br>"; echo "Result = ", $num_res; echo "<br>"; echo "_________________________________"; echo "<br>"; ?>
Output
Global Array Example _________________________________ Global Array variable within the function Result = 192 _________________________________ Global Array variable outside the function Result = 192 _________________________________
Static variables preserve their value between function calls as they cannot be modified outside of the function. The STATIC keyword is used with the variable name to make it static. The reason for making variables static is that sometimes you want a value to do some other job in a program, so you share it among all instances of the class, and you want t to preserve its value. Moreover, a static variable is linked to a class, not some object.
Static declarations are resolved at compile time, and incorrectly declared static variables are caught at compile time.
Example
static $var_correct_ declaration = 17+42; // correct declaration static $var_wrong_declaration = sqrt(251); // wrong as a function, it requires a declaration.
Using a function with a static variable will return a parse error. Let’s look at examples with and without static to understand better understand the topic.
<?PHP //static variable function StaticFunc() { echo "Static Variable Example"; echo "<br>"; echo "_________________________________"; echo "<br>"; static $var_increase_salary = 60000; $var_increase_salary++; static $func_call = 0; $func_call++; echo "static variable call ", $func_call, " times"; echo "<br>"; echo "Result = ", $var_increase_salary; echo "<br>"; } StaticFunc(); StaticFunc(); StaticFunc(); ?>
Output
Static Variable Example _________________________________ static variable call one time Result = 60001 Static Variable Example _________________________________ static variable call 2 times Result = 60002 Static Variable Example _________________________________ static variable call 3 times Result = 60003
Every time StaticFunc()
is called, var_increase_salary
, the func_call
variables preserve the values they held the last time StaticFunc()
was executed, whereas both var_increase_salary
and the func_call
variables are local to StaticFunc()
. If we execute the same example without the static keywords, var_increase_salary
and the func_call
variables will be initialized on every call of StaticFunc()
, and each time the value of the new var_increase_salary
and the func_call
is displayed, not the last preserved one, and increments it.
<?php //static variable function WithoutStaticFunc() { echo " Without Static Keyword Variable Example"; echo "<br>"; echo "_________________________________"; echo "<br>"; $var_increase_salary = 60000; $var_increase_salary++; $func_call = 0; $func_call++; echo " Without static variable, call ", $func_call, " time."; echo "<br>"; echo "Result = ", $var_increase_salary; echo "<br>"; } WithoutStaticFunc(); WithoutStaticFunc(); WithoutStaticFunc(); ?>
Output
Without Static Variable Example _________________________________ Without static variable, call 1 time. Result = 60001 Without Static Variable Example _________________________________ Without static variable, call 1 time. Result = 60001 Without Static Variable Example _________________________________ Without static variable, call 1 time. Result = 60001
<?php //Declaration of static variable function StaticFunc() { echo "Static Variable Declaration Example"; echo "<br>"; static $var_increase_salary = 60000; $var_increase_salary++; static $func_call = sqrt(121); echo " ", $func_call, " ",$var_increase_salary; echo "<br>"; } StaticFunc(); ?>
Output
Fatal error: Constant expression contains invalid operations in C:\hello.PHP on line 17
The same example without static keyword function assignment $func_call = sqrt(121)
will run fine without any issues.
This concludes the PHP Variables lesson. In the next lesson, we are going to demonstrate two important functions of PHP, the $
and $$
.