PHP basic variables:
PHP variable is an identifier. It will start with a dollar sign ($). No need to declare any variable before using, like other any programming languages.
Example
1 2 3 4 5 |
<?php $age = 34; $name= "my php programming"; $info = array(); ?> |
variable variables:
It refers the value of another variable as the variable name.
Example
1 2 3 4 |
<?php $tutorial = "php"; $$tutorial = "programming language"; ?> |
Now
$$tutorial is equal to $php
So $php outputs programming language
Variable scope:
The scope of the variable depends on the variable’s declaration. Depending on the place of declaration in the PHP programming we can access the variable.
There are four type of variable scope available in PHP, they are:
- local
- global
- static
- function parameters
Local:
The variable declared inside the function is called as local variable. It is accessible within the function only and it will not be known outside the function.
Example
1 2 3 4 5 6 |
<?php function increment() { $counter = 0; // local variable, not known outside of increment() $counter++; } ?> |
Global:
Variable declared outside the function is called a global variable. It is accessible anywhere in the program.
Example
1 2 3 4 5 6 7 8 9 |
<?php function increment() { global $counter;// global $counter++; // known outside of increment () } $counter = 5; increment(); echo $counter; // Output: 6 ?> |
Static:
Static variable is an important feature in variable scoping. The static variable will maintain the state between every function calls and will not lose it’s latest value. PHP has two types of static functionality:
- static in class variables and methods
- static in functions
Consider the following example to understand static variable scope in class.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php class static_example { public static $static_variable = 5; // static variable public static function static_method() // static method { self::$static_variable++; } } echo static_example::$static_variable; // output 5 static_example::static_method(); echo static_example::$static_variable; // output 6 $object = new static_example; echo $object->static_variable; // doesn't work (PHP Notice: Accessing static property) ?> |
Consider the following example to understand static variable scope in functions.
Example
1 2 3 4 5 6 7 8 9 |
<?php function increment( ) { static $static_variable = 5; $static_variable++; echo $static_variable; } increment( ); // Output -> 6 increment( ); // Output -> 7 ?> |
Function parameters:
Function parameters are local to that function itself where it is passed to. They are available only inside their function.
Example
1 2 3 4 5 6 |
<?php function callme($site_name) { echo 'welcome to', $site_name; } callme('my php programming'); // Output -> welcome to my PHP programming ?> |
In the above example, $site_name is not accessible from outside callme() function.
Difference between global and static variable:
Global variable can be accessed anywhere in programming, whereas static variable is accessible only within the function scope.
Variable References:
One variable name referred by another variable name, is called a variable reference.
Consider the below example:
1 2 3 4 |
<?php $color = 'red'; $black = 'black'; ?> |
After assigning $black to $color, the $color will become ‘black’
1 2 3 4 5 |
<?php echo $color; // Output -> red $color = $black; echo $color; // Output -> black ?> |
Now change the value of $black
1 2 3 4 |
<?php $black = 'gray'; echo $color; // Output -> black ?> |
In this case, changing the value of $black will not affect the variable $color.
But instead of assigning {$color = $black;} if we assign {$color = & $black;}, then in the above case $black will affect the variable $color:
Example of assign by value:
1 2 3 4 5 6 7 8 9 |
<?php $color = 'red'; $black = 'black'; echo $color; // Output -> red $color = $black; echo $color; // Output -> black $black = 'gray'; echo $color; // Output -> black // no change here ?> |
Example of assign by reference:
1 2 3 4 5 6 7 8 9 |
<?php $color = 'red'; $black = 'black'; echo $color; // Output -> red $color = & $black; echo $color; // Output -> black $black = 'gray'; echo $color; // Output -> gray // value has been changed here ?> |
This infоrmatin is priceless. Whre can I find out more?
It’s fantastic that you are getting ideas from this paragraph as well as from our dialogue made at this time.