Just like other programming languages, PHP has variables and constants. A variable is a storage space paired with an associated symbolic name that stores a particular value. All PHP variables start with the $ (dollar) sign, followed by the name of the variable.
This article explains the usage of variables in a block of PHP code.
Index
A variable is a storage site that is useful to store various types of values, such as numeric values, characters, etc., that uses in any part of a program. Variables are associated with a particular name.
Read Also: PHP Comments
$variablename=value;
Example
$color = Blue;
These are the following rules for naming variables in PHP:
Example 1
<?php //valid variable declarations in PHP $val = 5; $val2 = 2; $x_Y = "errorsea"; $_X = "Suman"; // Invalid declarations in PHP, uncomment the below variables and you will get errors // begins with a number // $10_val = 10; // This is also invalid as it contains special character other than underscore(_) // $a.b = "num"; ?>
Example 2
<?php $color="red"; echo "My car is " . $color . "<br>"; echo "My house is " . $COLOR . "<br>"; echo "My boat is " . $coLOR . "<br>"; ?>
Output
My car is red My house is My boat is
PHP variable names are case-sensitive. So variable name “color” is different from Color, COLOR, COLor etc.
In PHP, the variables can be declared anywhere in the script. In PHP, we can declare the variable for a particular scope.
PHP has three variable scopes.
The variables declared within the function are called local variables. In simple words, it cannot be accessed outside that particular function.
Example
<?php $num = 60; function abc() { $num = 50; echo "local num = $num <br>"; } abc(); echo "Variable num outside abc() is $num"; ?>
Output
local num = 50 Variable num outside abc() is 60
This variable $num is local to the abc() function and the variable $num outside this function is a different variable.
The variables declared outside the function are known as global variables. These variables can be accessed from anywhere within the PHP code block. To use within a function, we need to use the “global” keyword before the variable.
Example
<?php $num = 20; function abc() { global $num; echo "Variable num inside function : $num <br>"; } abc(); echo "Variable num outside function : $num"; ?>
Output
Variable num inside function : 20 Variable num outside function : 20
We have to use the global keyword before the variable $num to access within the function.
When a function executes all of its variables, the variables lose their original values due to regular increment or decrement or other operations performed in it. But the original value might be needed later by the developer. To retain the original value of the variable even when other operations are performed on it, we have to use the static keyword when we first declare the variable.
Example
<?php function abc() { // static variable static $num = 5; $sum = 2; $sum++; $num++; echo $num, "<br>"; echo $sum, "<br>"; } abc(); abc(); ?>
Output
6 3 7 3
The variable $num retains its value and regularly increments even after the first function call. However, $sum doesn’t because $sum is not a static variable.
Read More: PHP Echo & Print Statements
Variables play an important role in programming because they enable the programmer to write flexible programs rather than writing long pieces of code with frequently occurring values. A programmer uses variables to store and represent the data.
We hope we have been able to help you on your next step in the endeavor of conquering the PHP language. Keep reading and implementing the further modules’ steps, and check out the previous posts if you haven’t. Happy Learning!
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…