what is variable in PHP
Variables are used to keep data in one place. Such as string of text numbers, etc. The value of a variable can change on the browser of a script. Here are some important things to know about variables.
- In PHP, a variable does not need to be declared before adding a value to it. PHP automatically converts the variable to the correct data type, depending on its value.
- After declaring a variable it can be reused throughout the code.
- The assignment operator (=) used to assign value to a variable.
Creating PHP Variables
A variable ($) in the first sentence starts with this symbol, followed by the name of Valiabale,
As given in the example below
<!DOCTYPE html> | |
<html> | |
<body> | |
<?php | |
$txt = "Hello world!"; | |
$x = 5; | |
$y = 10.5; | |
echo $txt; | |
echo "<br>"; | |
echo $x; | |
echo "<br>"; | |
echo $y; | |
?> | |
</body> | |
</html> |
After the first given above, the value of $ txt will be Hello World! The value of the next $ X will be 5, and the value of the next $ y will be 10.5
Note: When we assign a text value to a variable, we must put a quote around the value.
PHP Variables
A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).
Rules for PHP variables:
- A variable starts with the $ sign, followed by the name of the variable
- A variable name must start with a letter or the underscore character
- A variable name cannot start with a number
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
- Variable names are case-sensitive ($age and $AGE are two different variables)
Output Variablesβ
The PHP echo statement is often used to output data to the screen.
The example. I have given below is how to output the variable
Example:
<!DOCTYPE html> | |
<html> | |
<body> | |
<?php | |
$txt = "devopsschool.com"; | |
echo "I love $txt!"; | |
?> | |
</body> | |
</html> |
The following example will output the sum of two variables:
<!DOCTYPE html> | |
<html> | |
<body> | |
<?php | |
$x = 5; | |
$y = 4; | |
echo $x + $y; | |
?> | |
</body> | |
</html> |
PHP Variables Scope
In PHP, variables can be declared anywhere in the script.
The scope of a variable is the part of the script where the variable can be referenced/used.
PHP has three different variable scopes:
- local
- global
- static
local scope
A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function:
Example
<!DOCTYPE html> | |
<html> | |
<body> | |
<?php | |
function myTest() { | |
$x = 5; // local scope | |
echo "<p>Variable x inside function is: $x</p>"; | |
} | |
myTest(); | |
// using x outside the function will generate an error | |
echo "<p>Variable x outside function is: $x</p>"; | |
?> | |
</body> | |
</html> |
Global Scope
A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function:
Example:
<!DOCTYPE html> | |
<html> | |
<body> | |
<?php | |
$x = 5; // global scope | |
function myTest() { | |
// using x inside this function will generate an error | |
echo "<p>Variable x inside function is: $x</p>"; | |
} | |
myTest(); | |
echo "<p>Variable x outside function is: $x</p>"; | |
?> | |
</body> | |
</html> |
Static Keyword:
Generally, when we write a function completely, all its variables are removed.Sometimes we want no local variable to be removed, we need static for further work,
When we first declare a variable, in this case we use the static keyword
As i explained in the example below
Example:
<!DOCTYPE html> | |
<html> | |
<body> | |
<?php | |
function myTest() { | |
static $x = 10; | |
echo $x; | |
$x++; | |
} | |
myTest(); | |
echo "<br>"; | |
myTest(); | |
echo "<br>"; | |
myTest(); | |
?> | |
</body> | |
</html> |




Iβm a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND