Define a variable
A variable has a name and is associated with a value. To define a variable, you use the following syntax:
When defining a variable, you need to follow these rules:
- The variable name must start with the dollar sign (
$
). - The first character after the dollar sign (
$
) must be a letter (a-z
) or the underscore (_
). - The remaining characters can be underscores, letters, or numbers.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>PHP Variables</title> </head> <body> <?php $title = 'PHP is awesome!'; ?> <h1><?php echo $title; ?></h1> </body> </html> |
Output:
1 | PHP is awesome! |
Another shorter way to show the value of a variable on a page is to use the following syntax:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>PHP Variables</title> </head> <body> <?php $title = 'PHP is awesome!'; ?> <h1><?= $title; ?></h1> </body> </html> |
Mixing PHP code with HTML will make the code unmaintainable, especially when the application grows. To avoid this, you can separate the code into separate files. For example:
index.php
– store the logic for defining and assigning value to variables.index.view.php
– store the code that displays the variables.- Use the
require
construct to include the code from theindex.view.php
in theindex.php
file.
The following shows the contents of the index.view.php
file:
1 2 3 4 5 6 7 8 9 10 11 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>PHP Variables</title> </head> <body> <h1><?= $title ?></h1> </body> </html> |
And the following shows the contents of the index.php
file:
1 2 3 4 | <?php $title = 'PHP is awesome!'; require 'index.view.php'; |
If you open the index.php
file on the web browser, you’ll see the same output.
Variable Scopes
Scope of a variable is defined as its extent in a program within which it can be accessed, i.e. the scope of a variable is the portion of the program within which it is visible or can be accessed.
Depending on the scopes, PHP has three variable scopes:
Local variable
The variables declared within a function are called local variables to that function and have their scope only in that particular function.
It cannot be accessed outside that function. Any declaration of a variable outside the function with the same name as that of the one within the function is a completely different variable.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php // function to demonstrate static variables function static_var() { // static variable static $num = 5; $sum = 2; $sum++; $num++; echo $num, "\n"; echo $sum, "\n"; } // first function call static_var(); // second function call static_var(); ?> |
Output:
1 2 | local num = 50 Variable num outside local_var() is 60 |
Global variables
The variables declared outside a function are called global variables.
These variables can be accessed directly outside a function. To get access within a function we need to use the global keyword before the variable to refer to the global variable.
Ex:
1 2 3 4 5 | <?php $a = 'hello'; //hello is value of variable $a $$a = 'World'; //$($a) is equals to $(hello) echo $hello; //$hello is World i.e. $hello is new variable with value 'World' ?> |
Output:
1 2 | Variable num inside function : 20 Variable num outside function : 20 |
Static variable
It is the characteristic of PHP to delete the variable, once it completes its execution and the memory is freed. But sometimes we need to store the variables even after the completion of function execution.
To do this we use the static keywords and the variables are then called static variables. PHP associates a data type depending on the value for the variable.
Ex:
1 2 3 4 5 6 7 8 9 | function myTest() { static $x = 0; echo $x; $x++; } myTest(); myTest(); myTest(); |
Output:
1 2 3 | 0 1 2 |
Variable Variables
- PHP allows us to use dynamic variable names, called variable variables.
- Variable variables are simply variables whose names are dynamically created by another variable’s value.
Ex:
1 2 3 4 5 | <?php $a = 'hello'; //hello is value of variable $a $$a = 'World'; //$($a) is equals to $(hello) echo $hello; //$hello is World i.e. $hello is new variable with value 'World' ?> |
Output:
1 | World |