1.4. PHP Variables
1.4. PHP Variables
1
Topics Covered
▪ PHP Variables
▪ Declaring PHP Variables
▪ Loosely Typed Language
▪ PHP Variable Scope
▪ Global Keyword
▪ Static Keyword
▪ Display String
▪ Display Variable
2
Variables
$name = expression;
PHP
$user_name = “ahmed123";
$age = 16;
$driving_age = $age + 2;
$this_class_rocks = TRUE;
PHP
</body>
</html>
Creating (Declaring) PHP Variables
• PHP has no command for declaring a variable.
• A variable is created the moment you first assign a value to it:
<?php
$txt="Hello world!";
$x=5;
$y=6;
$z=$x+$y;
echo $z;
?>
Output : Hello world!
11
Note: When you assign a text value to a variable, put quotes
around the value.
PHP is a Loosely Typed Language
• In the example above slide, notice that we did not have to tell
PHP which data type the variable is.
• PHP automatically converts the variable to the correct data
type, depending on its value.
• In other languages such as C, C++, and Java, the programmer
must declare the name and type of the variable before using it.
PHP Variables Scope
• In PHP, variables can be declared anywhere in the script.
• The scope of a variable is the part of the script where the
variable can be referenced/used.
• PHP has different variable scopes:
• local
• global
• A variable declared outside a function has a GLOBAL
SCOPE and can only be accessed outside a function.
• A variable declared within a function has a LOCAL SCOPE
and can only be accessed within that function.
• Example:
<!DOCTYPE html>
<html> • Test variables inside
<body> the function:
<?php • Variable x is:
$x=5; // global scope
function myTest() { Variable y is: 10
$y=10; // local scope • Test variables outside
echo "<p>Test variables inside the the function:
function:</p>"; • Variable x is: 5
echo "Variable x is: $x";
echo "<br>"; Variable y is:
echo "Variable y is: $y";
}
myTest();
echo "<p>Test variables outside the
function:</p>";
echo "Variable x is: $x";
echo "<br>";
echo "Variable y is: $y";
?>
</body>
PHP The global Keyword:
The global keyword is used to access a global variable from within a function.
To do this, use the global keyword before the variables (inside the function):
<?php
$x=5;
$y=10;
function myTest() {
global $x,$y;
$y=$x+$y;
}
</body>
</html>
• PHP also stores all global variables in an array called $GLOBALS[index].
The index holds the name of the variable. This array is also accessible
from within functions and can be used to update global variables directly.
• The example above can be rewritten like this:
<!DOCTYPE html>
<html>
<body>
<?php
$x=5;
$y=10;
function myTest() {
$GLOBALS['y']=$GLOBALS['x']+$GLOBALS['y’];
}
myTest();
echo $y;
?>
</body>
</html>
Variable scopes
Global Variables
PHP The static Keyword
• The static keyword is also used to declare variables in a
function which keep their value after the function has ended.
Output: 0
• Normally, when a function is completed/executed, all of its 1
variables are deleted. However, sometimes we want a local 2
variable NOT to be deleted. We need it for a further job.
3
• To do this, use the static keyword when you first declare the
variable: 4
• <?php
function myTest() {
static $x=0;
echo $x;
$x++; Then, each time the function is called, that
} variable will still have the information it
myTest(); echo "<br>"; contained from the last time the function
myTest(); echo "<br>"; was called.
myTest(); echo "<br>"; Note: The variable is still local to the
function.
myTest(); echo “<br>";
myTest();
PHP echo and print Statements
• There are some differences between echo
and print:
• echo - can output one or more strings
• print - can only output one string, and returns
always 1
• <?php • Output:
echo "<h2>PHP is fun!</h2>"; • PHP is fun!
echo "Hello world!<br>"; • Hello world!
echo "I'm about to learn PHP!<br>"; I'm about to learn
echo "This", " string", " was", " made", PHP!
" with multiple parameters."; This string was
?> made with
multiple
parameters.
The PHP echo Statement: Display Variables
<?php • Output:
$txt1="Learn PHP"; • Learn PHP
$txt2="W3Schools.com"; Study PHP at
$cars=array("Volvo","BMW","Toyota"); W3Schools.com
echo $txt1; My car is a Volvo
echo "<br>";
echo "Study PHP at $txt2";
echo "<br>";
echo "My car is a {$cars[0]}";
?>
The PHP print Statement: Display Strings
<?php • Output:
print "<h2>PHP is fun!</h2>"; • PHP is fun!
print "Hello world!<br>"; • Hello world!
print "I'm about to learn PHP!"; I'm about to learn
?> PHP!
The PHP print Statement: Display Variables
• <?php • Output:
$txt1="Learn PHP"; • Learn PHP
$txt2="W3Schools.com"; Study PHP at
$cars=array("Volvo","BMW","Toyota") W3Schools.com
; My car is a Volvo
print $txt1;
print "<br>";
print "Study PHP at $txt2";
print "My car is a {$cars[0]}";
?>
Summary
▪ PHP Variables
▪ Declaring PHP Variables
▪ Loosely Typed Language
▪ PHP Variable Scope
▪ Global Keyword
▪ Static Keyword
▪ Display String
▪ Display Variable
References
1. Steven Holzner, “PHP:The Complete
Reference”, McGraw Hill Education, 2017
2. https://www.w3schools.com/php/
3. https://www.tutorialspoint.com/php/index.htm
26