0% found this document useful (0 votes)
11 views26 pages

1.4. PHP Variables

This document covers the topic of PHP Variables, including their declaration, scope, and the use of keywords like global and static. It explains the rules for naming variables, the concept of loosely typed languages, and how to display strings and variables using echo and print statements. Additionally, it provides examples and references for further learning.

Uploaded by

Karthikeyini S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views26 pages

1.4. PHP Variables

This document covers the topic of PHP Variables, including their declaration, scope, and the use of keywords like global and static. It explains the rules for naming variables, the concept of loosely typed languages, and how to display strings and variables using echo and print statements. Additionally, it provides examples and references for further learning.

Uploaded by

Karthikeyini S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

SRI KRISHNA COLLEGE OF ENGINEERING AND TECHNOLOGY

Kuniamuthur, Coimbatore, Tamilnadu, India


An Autonomous Institution, Affiliated to Anna University,
Accredited by NAAC with “A” Grade & Accredited by NBA (CSE, ECE, IT, MECH ,EEE, CIVIL& MCT)

Course : 21CSI504 – PHP and JS Framework


Module : 1
Topic : PHP Variables
Faculty : Dr. S. Karthikeyini
Department : M.Tech – Computer Science & Engineering

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

 Variables names are case sensitive ($age is not $Age)


 Variables names always begin with $, on both
declaration and usage
Variables
 Rules for PHP variables:
o A variable starts with the $ sign, followed by the name of the
variable
o A variable name must start with a letter or the underscore
character
o A variable name cannot start with a number
o A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
o Variable names are case-sensitive ($age and $AGE are two
different variables)
Variables
• As with algebra, PHP variables can be used to hold values (x=5)
or expressions (z=x+y).
• A variable can have a short name (like x and y) or a more
descriptive name (age, carname, total_volume).
• Rules for PHP variables:
• A variable starts with the $ sign, followed by the name of the
variable
• A variable name must start with a letter or the underscore
character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
• Variable names are case sensitive ($y and $Y are two different
variables)
In PHP, all variables are case-sensitive.
In the example below, only the first statement will display the value of the
$color variable (this is because $color, $COLOR, and $coLOR are treated as
three different variables):
 <!DOCTYPE html>
<html>
<body>
• My car is red
<?php My house is
$color="red"; My boat is
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>

</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):

<!DOCTYPE html> • Output: 15


<html>
<body>

<?php
$x=5;
$y=10;

function myTest() {
global $x,$y;
$y=$x+$y;
}

myTest(); // run function


echo $y; // output the new value for variable $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

• Tip: echo is marginally faster compared to


print as echo does not return any value.
The PHP echo Statement: Display Strings

• <?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

You might also like