![]() |
Best Php Variable Store Data Value |
Best Php Variable Store Data Value
Variable
in PHP: It is a name of storage
space that is used to store data. Its value may be changed. It always
contains the last value stored in it. Variable in PHP starts with the dollar($) sign.
How To Use Variable in PHP?
<?PHP $x=100; $x=50; $x=80; echo "The Value of x is: ".$x; ?> *****OUTPUT***** The value of x is:80 |
How To Use Variable in Double quotes in PHP?
<?php $s="Icoderweb Website"; echo "I like $s"; ?> *****OUTPUT***** I like Icoderweb Website |
How To Use Concatenate Variable Using dot(.) operator in PHP?
<php $s="Icoderweb"; echo "I like ".$s; ?> *****OUTPUT***** I like Icoderweb |
Rules to define a variable
1. It must start with a dollar sign.
2. There is no need for the data type(int, float, char)
to define variables.
3. Variables can but do not need, to be declared
before assignment.
4. After the dollar sign the first letter of a
variable should be alphabet or underscore(_).
5. After the dollar sign the first letter of a
variable should not be a digit.
6. After the first character it may be a combination of
alphabets and digits.
7. Blank spaces are not allowed in a variable name.
Types of Variable in PHP
1. Local Variable
2. Global Variable
3. Static Variable
Important Variable Topic in Other Languages
👉C++ Variables
👉Java Variables
👉Python Variables
Local
Variable: A variable declared
inside the body of the method or constructor is called a local variable. A local variable can be used only inside that method/function in which it is declared. A local variable can be a static variable.
How To Use Local Variable in PHP?
<?php function fundata() { $a=100; $b=200; echo "The Value of a is $a and b is $b"; } fundata(); ?> *****OUTPUT***** The value of a is 100 and b is 200 |
Global Variable: A variable that is declared outside the body of the method or constructor is called a global variable.
How To Use Global Variable in PHP?
<?php $a=10; $b=20; function fundata() { $a=100; $b=200; echo "<b>Inside function:</b>
The value of a is $a and b is $b <br>"; } fundata(); echo "<b>Outside
function:</b>The value of a is $a and b is $b "; ?> *****OUTPUT***** Inside function: The value of a is 100 and b
is 200 Outside function: The value of a is 10 and b
is 20 |
Static
Variable: A variable that is
declared with a static keyword is called a static variable. The static variable is
stored in the static memory. Static variables are created when the program
starts and destroyed when the program stops.
How To Use Normal Variable in PHP?
<?php function fundata() { $a=100; echo "$a<br>"; $a++; } fundata(); fundata(); fundata(); ?> *****OUTPUT***** 100 100 100 |
How To Use With Static Variable in PHP?
<?php function fundata() { static $a=100; echo "$a<br>"; $a++; } fundata(); fundata(); fundata(); ?> *****OUTPUT***** 100 101 102 |
Use of Global variable as a local variable method 1
<?php $x=50; $y=100; function fundata() { global $x,$y; echo "<b>Inside function:</b>
The value of x is $x and y is $y <br>"; } fundata(); echo "<b>Outside function:</b>The
value of x is $x and y is $y "; ?> *****OUTPUT***** Inside function: The value of x is 50 and y is
100 Outside function: The value of x is 50 and y
is 100 |
Use of Global variable as a local variable method 2
<?php $x=50; $y=100; function fundata() { $sum=$GLOBALS['x']+$GLOBALS['y']; echo "<b>Inside function:</b>
Sum:$sum"; } fundata(); $sum=$x+$y; echo "<b>Outside
function:</b> Sum:$sum"; ?> *****OUTPUT***** Inside function: Sum:150 Outside function: Sum:150 |