PHP Variables
PHP Variables
In PHP, a variable is declared using a $ sign followed by the variable name. Here, some
important points to know about variables:
1. $variablename=value;
o A variable must start with a dollar ($) sign, followed by the variable name.
o It can only contain alpha-numeric character and underscore (A-z, 0-9, _).
o A variable name must start with a letter or underscore (_) character.
o A PHP variable name cannot contain spaces.
o One thing to be kept in mind that the variable name cannot start with a
number or special symbols.
o PHP variables are case-sensitive, so $name and $NAME both are treated as
different variable.
File: variable1.php
1. <?php
2. $str="hello string";
3. $x=200;
4. $y=44.6;
5. echo "string is: $str <br/>";
6. echo "integer is: $x <br/>";
7. echo "float is: $y <br/>";
8. ?>
Output:
1. <?php
2. $x=5;
3. $y=6;
4. $z=$x+$y;
5. echo $z;
6. ?>
Output:
11
File: variable3.php
1. <?php
2. $color="red";
3. echo "My car is " . $color . "<br>";
4. echo "My house is " . $COLOR . "<br>";
5. echo "My boat is " . $coLOR . "<br>";
6. ?>
Output:
My car is red
Notice: Undefined variable: COLOR in C:\wamp\www\variable.php on line 4
My house is
Notice: Undefined variable: coLOR in C:\wamp\www\variable.php on line 5
My boat is
File: variablevalid.php
1. <?php
2. $a="hello";//letter (valid)
3. $_b="hello";//underscore (valid)
4.
5. echo "$a <br/> $_b";
6. ?>
Output:
hello
hello
File: variableinvalid.php
1. <?php
2. $4c="hello";//number (invalid)
3. $*d="hello";//special symbol (invalid)
4.
5. echo "$4c <br/> $*d";
6. ?>
Output:
1. Local variable
2. Global variable
3. Static variable
Local variable
The variables that are declared within a function are called local variables for
that function. These local variables have their scope only in that particular
function in which they are declared. This means that these variables cannot
be accessed outside the function, as they have local scope.
A variable declaration outside the function with the same name is completely
different from the variable declared inside the function. Let's understand the
local variables with the help of an example:
File: local_variable1.php
1. <?php
2. function local_var()
3. {
4. $num = 45; //local variable
5. echo "Local variable declared inside the function is: ". $num;
6. }
7. local_var();
8. ?>
Output:
Local variable declared inside the function is: 45
File: local_variable2.php
1. <?php
2. function mytest()
3. {
4. $lang = "PHP";
5. echo "Web development language: " .$lang;
6. }
7. mytest();
8. //using $lang (local variable) outside the function will generate an error
9. echo $lang;
10.?>
Output:
Global variable
The global variables are the variables that are declared outside the function.
These variables can be accessed anywhere in the program. To access the
global variable within a function, use the GLOBAL keyword before the
variable. However, these variables can be directly accessed or used outside
the function without any keyword. Therefore there is no need to use any
keyword to access a global variable outside the function.
Example:
File: global_variable1.php
1. <?php
2. $name = "Sanaya Sharma"; //Global Variable
3. function global_var()
4. {
5. global $name;
6. echo "Variable inside the function: ". $name;
7. echo "</br>";
8. }
9. global_var();
10. echo "Variable outside the function: ". $name;
11.?>
Output:
Note: Without using the global keyword, if you try to access a global variable inside the
function, it will generate an error that the variable is undefined.
Example:
File: global_variable2.php
1. <?php
2. $name = "Sanaya Sharma"; //global variable
3. function global_var()
4. {
5. echo "Variable inside the function: ". $name;
6. echo "</br>";
7. }
8. global_var();
9. ?>
Output:
Example:
File: global_variable3.php
1. <?php
2. $num1 = 5; //global variable
3. $num2 = 13; //global variable
4. function global_var()
5. {
6. $sum = $GLOBALS['num1'] + $GLOBALS['num2'];
7. echo "Sum of global variables is: " .$sum;
8. }
9. global_var();
10.?>
Output:
If two variables, local and global, have the same name, then the local
variable has higher priority than the global variable inside the function.
Example:
File: global_variable2.php
1. <?php
2. $x = 5;
3. function mytest()
4. {
5. $x = 7;
6. echo "value of x: " .$x;
7. }
8. mytest();
9. ?>
Output:
Value of x: 7
Static variable
It is a feature of PHP to delete the variable, once it completes its execution
and memory is freed. Sometimes we need to store a variable even after
completion of function execution. Therefore, another important feature of
variable scoping is static variable. We use the static keyword before the
variable to define a variable, and this variable is called as static variable.
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:
Example:
File: static_variable.php
1. <?php
2. function static_var()
3. {
4. static $num1 = 3; //static variable
5. $num2 = 6; //Non-static variable
6. //increment in non-static variable
7. $num1++;
8. //increment in static variable
9. $num2++;
10. echo "Static: " .$num1 ."</br>";
11. echo "Non-static: " .$num2 ."</br>";
12. }
13.
14.//first function call
15. static_var();
16.
17. //second function call
18. static_var();
19.?>
Output:
Static: 4
Non-static: 7
Static: 5
Non-static: 7
You have to notice that $num1 regularly increments after each function call,
whereas $num2 does not. This is why because $num1 is not a static variable,
so it freed its memory after the execution of each function call.
The $$var (double dollar) is a reference variable that stores the value of the
$variable inside it.
Example 1
1. <?php
2. $x = "abc";
3. $$x = 200;
4. echo $x."<br/>";
5. echo $$x."<br/>";
6. echo $abc;
7. ?>
Output:
1. <?php
2. $x="U.P";
3. $$x="Lucknow";
4. echo $x. "<br>";
5. echo $$x. "<br>";
6. echo "Capital of $x is " . $$x;
7. ?>
Output:
Example3
1. <?php
2. $name="Cat";
3. ${$name}="Dog";
4. ${${$name}}="Monkey";
5. echo $name. "<br>";
6. echo ${$name}. "<br>";
7. echo $Cat. "<br>";
8. echo ${${$name}}. "<br>";
9. echo $Dog. "<br>";
10. ?>
Output:
In the above example, we have assigned a value to the variable name Cat.
Value of reference variable ${$name} is assigned as Dog and ${$
{$name}} as Monkey.
Dynamic Variable:
In programming, a dynamic variable is a variable whose address is
determined when the program is run. In contrast, a static
variable has memory reserved for it at compilation time.
A variable of a variable takes a value of a variable and threads which is the name of a
variable. This is new feature of using variables and by using double dollar signs. This
technique is called a dynamic variable in PHP. Those variables you can use a dynamically
generated variable of variable as well as OOP Concept.
Example1
<?php
$b = 'hello';
$$b = 'Sharad Gupta';
echo $b.${$b};
?>
Output
Variable Manipulation Functions
The following functions operate on PHP variables. There are functions for
getting and setting the type of a variable, as well as various ways to encode
and decode variables for storage.
Get the integer value of a variable using the optional base for the
conversion
Take one or more arguments and pack them into a binary string
according to the format argument
bool is_string(mixed
var)
void putenv(string
setting)
string ...