PHP variable's rules
Now we will go through some of the rules assigned to variables in PHP.
- Only alpha-numeric character and underscore (A-z, 0-9, _) are allowed for naming a variable.
- Starting character can be underscore(_) or a letter but should not start with a special character or number.
- No spaces are permitted in the variable name.
- PHP language is case-sensitive, i.e, $car and $CAR is different for PHP.
Declaring PHP variables
In this section, we will see how we can store string, integer, and float using PHP variables.
Step 1
As we discussed above, we will declare the PHP variable using the $ symbol.
$greeting
You can also try this code with Online PHP Compiler
Run Code
Step 2
We will use the = operator for assigning value to it.
$greeting =
You can also try this code with Online PHP Compiler
Run Code
Step 3
Now we will give the value we want to assign to this variable.
$greeting = "Hello Ninjas!"
You can also try this code with Online PHP Compiler
Run Code
and add a semicolon at the end,
$greeting = "Hello Ninjas!";
You can also try this code with Online PHP Compiler
Run Code
Remember to put quotes("") at the end as well as at the starting of the string.
Here we have declared a variable that stores string value after running it.
Similarly, we will now declare variables that store integer and float values.
$x = 10;
$percentage = 87.89;
You can also try this code with Online PHP Compiler
Run CodeOutput variables
We use the ‘$echo’ command in PHP to display data to the screen. we can display output data in two ways -
1)
<?php
$text = "Coding Ninjas";
echo "I love $text!";
?>
You can also try this code with Online PHP Compiler
Run Code
Output
2)
<?php
$text = "Coding Ninjas";
echo "I love " . $text . " !";
?>
You can also try this code with Online PHP Compiler
Run Code
Output-
This code covers all the concepts that we have learned so far-
<?php
$greeting = "Hello Ninjas!";
$x = 10;
$percentage = 87.89;
echo "string is: $greeting <br/>";
echo "integer is: $x <br/>";
echo "float is: $percentage <br/>";
?>
You can also try this code with Online PHP Compiler
Run Code
Output-
PHP Variable Scope
The portion of the program within which a variable is declared and accessed is called the variable's scope. Three types of variable scopes are there in PHP-
Local variable
When we declare variables inside a function, they are called as local variables and can be accessed or have scope only inside that function within which it is declared. Program outside that function can not access this variable outside this function. Furthermore, if we declare a variable with the same name as the variable defined inside the function, it will be different.
<?php
function print_local_var()
{
$number = 45;
echo "Local variable which is declared inside the function is: ". $number . "</br>";
}
print_local_var();
echo "This value will not be displayed" . $number;
?>
You can also try this code with Online PHP Compiler
Run Code
Output
Global variable
These variables can be accessed anywhere in the program as they are declared outside the function. We use the GLOBAL keyword to access the global variable inside the function, but this is not required outside the function.
<?php
$name = "Coding ninja"; //Global Variable
function global_variable()
{
global $name;
echo "Variable inside the function: ". $name;
echo "</br>";
}
global_variable() ;
echo "Variable outside the function: ". $name;
?>
You can also try this code with Online PHP Compiler
Run Code
Output:
Another way to access the global variable inside the function is by using the $GLOBALS array.
<?php
echo "<h2> Coding ninjas </h2></br>";
$num1 = 45; //global variable
$num2 = 89; //global variable
function global_variables()
{
$sum = $GLOBALS['num1'] + $GLOBALS['num2'];
echo "Sum of global variables is: " .$sum;
}
global_variables();
?>
You can also try this code with Online PHP Compiler
Run Code
Output
The local variable is given much priority if the global and local variables have the same name.
<?php
echo "<h2> Coding ninjas </h2></br>";
$x = 5;
function mytest()
{
$x = 7;
echo "value of x: " .$x;
}
mytest();
?>
You can also try this code with Online PHP Compiler
Run Code
Output
Static variable
The variable gets deleted after execution, and the memory is freed but sometimes, after the complete performance of a function, we need to store that variable. Before declaring a variable, we use the static keyword and call this variable a static variable. Local functions stores these static variables, and after the program execution, it leaves the scope but doesn't free their memory.
<?php
echo "<h2> Coding Ninjas </h2></br>";
function static_variable()
{
static $number1 = 56; //static variable
$number2 = 6; //Non-static variable
//increment in non-static variable
$number1++;
//increment in static variable
$number2++;
echo "Static: " .$number1 ."</br>";
echo "Non-static: " .$number2 ."</br>";
}
//first function call
static_variable();
//second function call
static_variable();
?>
You can also try this code with Online PHP Compiler
Run Code
Output-
It's important to note that $num1 consistently increments after each function call; however, $num2 does not, because the memory of $num2 is freed after the execution of the program.
$ and $$ Variables of PHP
$ (single dollar) is for declaring a standard variable that stores the value of a string or integer or float, etc., while on the other side, $$ (double dollar) is a reference variable which stores the value of a variable with $ (single dollar). For better understanding, we will take an example-
<?php
$car = "ninja";
$$car = 478;
echo $car."<br/>";
echo $$car."<br/>";
echo $ninja;
?>
You can also try this code with Online PHP Compiler
Run Code
Output-
We will take another example -
<?php
$name="coding";
${$name}="ninja";
${${$name}}="best";
echo $name. "<br>";
echo ${$name}. "<br>";
echo $coding. "<br>";
echo ${${$name}}. "<br>";
echo $ninja. "<br>";
?>
You can also try this code with Online PHP Compiler
Run Code
Output-
Must Read PHP Projects With Source Code
Frequently Asked Questions
How many variable types are in PHP?
PHP supports four main variable types: integers, floats (doubles), strings, and booleans, with additional support for arrays, objects, and resources.
What is the $_ variable in PHP?
The $_ variable in PHP is not a special variable; it’s often used in variable names like $_POST or $_GET for superglobals.
What are variable references in PHP?
Variable references in PHP allow multiple variables to point to the same data. Using the & operator, changes to one variable affect all references to it.
Conclusion
PHP variables are a fundamental part of PHP programming, enabling data storage and manipulation. They are flexible, supporting various types including integers, strings, and arrays. Understanding their rules and behavior, such as case sensitivity and dynamic typing, is crucial for effective coding. Proper use of variables and references enhances code readability and functionality, making PHP a powerful tool for web development.