Home » PHP » PHP Variables
In this tutorial, we will learn the Variable declaration and how to use this. But, first, let's know about PHP as a Loosely Typed Language.
This programming language does not require a variable to be defined. For instance, Perl is a loosely typed language, a variable can be declared using it, but you are not required to classify the variable type.
Now let's come to PHP. In PHP, the data type is automatically associated to the variable, depending on its value. However, since the data types are not defined strictly, it enables you to do things such as to add a string to an integer without causing an error.
Type declarations were added in PHP 7. Type declarations let you specify the data type expected when a function is declared, and by allowing the strict requirement, a "Fatal Error" will be thrown on a type mismatch.
In PHP, a variable begins with the $ sign and is followed by the variable name:
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$txt = "Hello Developers!";
$x = 50;
$y = 90;
echo $txt;
echo "<br>";
echo $x;
echo "<br>";
echo $y;
?>
</body>
</html>
Note- The PHP echo statement is often utilized to output data to the screen.
Output:
Hello Developers!
50
90
The variables can be declared by you anywhere in the script. The variable's scope is the part of the script where the variable can be referenced/used.
In PHP, there are three different variable scopes:
It is a variable's restricted boundary within its code block it is declared. This block can either be a class, function, or any conditional span. The specified code block's local variable is the variable within this limited local scope.
The given below code block depicts a PHP function. A variable $count has been declared inside this function. This variable is stated to be this function's local variable and is in the function block's local scope.
<?php
function calculate_count() {
$count = 5;
//will print 5; the value of local variable
echo $count++;
}
?>
On reaching the end of the code block, these local variables will be destroyed. Therefore, you can declare the same-named variables within different local scopes.
It provides overall access to the variable declared in this scope. You can access the variables in global scope from anywhere. It is accessible from class independent of its boundary or outside a function.
You can define PHP global variables by utilizing the global keyword. However, in case you want to use global variables inside a function, you have to prefix the global keyword with the variable.
The given below code depicts a code block to learn how to utilize the global keyword with a PHP variable for declaring it as a global variable.
<?php
$count = 0;
function calculate_count() {
global $count;
// will print 0 and increment global variable
echo $count++ . "<br/>";
}
calculate_count();
echo $count;
?>
It is a variable with local scope. But it is not destroyed outside the scope boundary. You can use a 'static' keyword to define a variable inside a function.
Even when the program execution goes past the scope boundary, a static variable does not lose its value. But the static variable can be accessed only within that boundary. Let us use an example code to understand it,
<?php
function counter()
{
static $count = 0;
echo $count;
$count++;
}
?>
The static variable 'count' has been declared within its local scope in the counter function above. On completing the function execution, the static count variable will still retain its value for further computation.
The value of the count is incremented every time you call the counter function. The count value is initialized only once on the first call.