PHP Variables
Variables are data storage "containers".
A variables may be given a short name (such as x and y) or a more specific name (such as era, carname, or total volume).
Rules for PHP variables
- A variable is identified by the $ symbol, which is followed by the variable's name.
- A variable name must begin with a letter or an underscore.
- A number cannot be the first character in a variable name.
- Only alpha-numeric characters and underscores (A-z, 0-9, and_ ) are used in variable names.
- Case matters when naming variables ($age and $AGE are two distinct variables).
Creating (Declaring) PHP Variables
A variable begins with a $ symbol in PHP and follows the variable name.
Example :-
Example Explained :-
Following the execution of the preceding statements, the variable $msg will contain the value Welcome to PHP!, the variable $a will contain the value 6, and the variable $b will contain the value 15.5.
Note :- When you add a text value to an variable, surround the value with single or double quotes.
In contrast to other programming languages, PHP does not provide a command or keyword to declare a variable. It is generated or defined when you add a value to it for the first time.
Output Variables
To display data to the browser screen, the PHP echo
command is frequently used.
Example 1 :- The following example will show how to output text and a variable.
Output :-
Example 2 :- The following example will produce the same output as the example above.
Output :-
Note:- We used "." symbol to concat the text and the variable name.
Example 3 :- The following example will output the sum of two variables:
Output :-
PHP is a Loosely Typed Language
Depending on the value of the variable, PHP automatically assigns a data type to it. You can perform things like adding a string to an integer without raising an error since the data types aren't established in a strict sense.
Related Links
PHP Variables Scope
In PHP, anyplace in the script, variables can be declared.
The scope of a variable refers to the area of the script where it may be the variable referenced and utilized.
PHP has three different variable scopes.
- local
- global
- static
Global Variable Scope
A variable declared outside of a function has a GLOBAL SCOPE and can be accessed only from outside the function.
Example :-
Variable x inside function is: $x";
}
myTest();
echo "Variable x outside function is: $x
";
?>
Output :-
Variable x inside function is: ERROR
Variable x outside function is: 5Local Variable Scope
A LOCAL SCOPE variable declared within a function can only be accessed inside that function.
Example :-
Variable e inside function is: $e";
}
myExam();
// using e outside the function will generate an error
echo "Variable e outside function is: $e
";
?>
Output :-
Variable x inside function is: 5
Variable x outside function is: ERRORlocal variables are only recognized by the function in which they are defined, you can have local variables with the same name in various functions.
Related Links
PHP The global Keyword
To access a global variable from within a function, use the global keyword.
To do this, use the global keyword before the variables (inside the function).
Example:-
Output :-
PHP also keeps all global variables in a $GLOBALS[index] array. The variable's name is stored in the index. This array is also available via functions and may be used to directly update global variables.
Example:- The example above can be rewritten like this:
Output :-
PHP The static Keyword
When a function is finished/executed, all of its variables are normally erased.
However, there are occasions when we do not want a local variable to be destroyed and it also needs for the job.
Example :-
Output :-
a = 1 , b = 0
a = 2 , b = 0
A local variable will be destroyed after completing a function execution. It will be newly initialized whenever a function was called.
The static variable will then retain the information it contained the previous time the function was called each time the function is invoked.