Variables are container that stores value.
A variable starts with the $ sign & followed by the variable name.
<?php
$var = "Welcome to LearnCoding";
echo $var;
?>
Note: - As PHP is a loosely typed language, so no need to declare the data types of the variables. It automatically analyses the values and makes conversions to its correct datatype.
<?php
$str="LearnCoding";
$x=299;
$y=423.3;
echo "String is: $str <br/>";
echo "Integer is: $x <br/>";
echo "Float is: $y <br/>";
?>
As, PHP employs dynamic typing, meaning it automatically assigns a data type to a variable based on its value. Unlike strict typing, where data types are explicitly defined, PHP allows flexbility.
In PHP, you have the flexibility to declare variables at any point in your script. The scope of a variable in a script determines where in the code that variable can be used or referred to.
In PHP there are three types of Variable scope which are as follows:
Local variable can only be used within the function or code block where it's created.
It's not visible outside of that of that specific area, and once the function or block finishes its execution, the variable's value if forgotten. This design promotes cleaner code organization and helps avoid clashes with variable names in other parts of the program.
<?php
function localTest() {
$y = 5; //local scope
echo "<p>Variable value: $y</p>";
}
localTest();
?>
Did you Know?
It's possible to use the same name for local variables in different functions because each function recognizes and operates with its own set of local variables.
A global variable is available throughout the entire script, including within functions and methods. These variables are defined outside any specific function or class and their values can be modified from any part of the script. However, it's crucial to mindful that excessive use of global variables can result in disorganized code and may lead to conflicts with variable names across different parts of the program.
<?php
$lang = "PHP"; //Global Scope
function globalTest()
{
global $lang;
echo "Interested Language: ". $lang; // accessing as global
echo "</br>";
}
globalTest();
echo "Interested Language: ". $lang; // accessing as local
?>
Did you know?
You can access a global variable within a function using the predefined $GLOBALS array. This array holds all global variables, allowing you to refer to a specific global variable by its name within the function
A static variable, like a local variable, is limited to the function or code block where it's defined. However, unlike local variables, the value of a static variable persists across multiple calls to the function. This feature is handy for preserving a state or keeping track of how many times a function has been called.
In simple words, Static variables exist only in a local function, but it does not free its memory after the program execution leaves the scope. Understand it with the help of an example:
<?php
function staticTest()
{
static $stat = 1; //static variable
$nonStat = 5; //Non-static variable
$stat++; //increment in non-static variable
$nonStat++; //increment in static variable
echo "Static value: " .$stat ."</br>";
echo "Non-static value: " .$nonStat ."</br>";
}
//first function call
staticTest();
//second function call
staticTest();
?>
Explanation:
You have noticed that $stat regularly increments after each function call, whereas $nonStat does not. This is why because $nonStat is not a static variable, so it freed its memory after the execution of each function call.