CS 6
ADVANCED WEB
PROGRAMMING
Unit 5:
PHP: echo/ print, and
Control Structures
What is echo & print statements in
PHP?
The echo is used to display the
output of parameters that are passed to
it. It displays the outputs of one or more
strings separated by commas.
What is echo & print statements in
PHP?
The print accepts one argument at
a time & cannot be used as a variable
function in PHP. The print outputs only
the strings.
PHP echo statement: It is a
language construct and never behaves like
a function, hence no parenthesis is
required. But the developer can use
parenthesis if they want. The end of the
echo statement is identified by the semi-
colon (‘;’). It output one or more strings.
<?php
echo "This is a display string example!";
?>
Output:
This is a display string example!
Displaying Strings as multiple
arguments: We can pass multiple string
arguments to the echo statement instead
of a single string argument, separating
them by comma (‘,’) operator. For
example, if we have two strings i.e “Hello”
and “World” then we can pass them as
(“Hello”, “World”).
<?php
echo "Multiple ",“Argument ",“String!";
?>
Output:
Multiple argument string!
Displaying Variables: Displaying
variables with echo statements is also as
easy as displaying normal strings.
<?php
// Defining the variables
$text = "Hello, World!";
$num1 = 7;
$num2 = 3;
// Echoing the variables
echo $text."\n";
echo $num1."+".$num2."=";
echo $num1 + $num2;
?>
Output:
Hello, World!
7 + 3 = 10
.
The (.) operator in the above code
can be used to concatenate two strings in
PHP and the “\n” is used for a new line and
is also known as line-break.
PHP print statement: The PHP print
statement is similar to the echo statement
and can be used alternative to echo
many times. It is also a language
construct, so we may not use
parenthesis i.e print or print().
The main difference between the
print and echo statement is that echo
does not behave like a function whereas
print behaves like a function. The print
statement can have only one argument at
a time and thus can print a single string.
Also, the print statement always returns a
value of 1.
Displaying String of Text: We can
display strings with the print statement in
the same way we did with echo
statements. The only difference is we
cannot display multiple strings
separated by comma(,) with a single
print statement.
<?php
print "Hello, world!";
?>
Output:
Hello, world!
Displaying Variables: Displaying
variables with print statements is also the
same as that of echo statements.
<?php
// Defining the variables
$text = "Hello, World!";
$num1 = 7;
$num2 = 3;
// Echoing the variables
print $text."\n";
print $num1."+".$num2."=";
print $num1 + $num2;
?>
.
Output:
Hello, World!
7 + 3 = 10
Difference between echo and print statements in
PHP
No. echo statement print statement
1 echo accepts a list of arguments (multiple print accepts only one
arguments can be passed), separated by argument at a time.
commas.
2 It returns no value or returns void. It returns the value 1.
3 It displays the outputs of one or more The print outputs only the
strings separated by commas. strings.
4 It is comparatively faster than the print It is slower than an echo
statement. statement.
Conditional Statements in PHP
Conditional statements are used to
execute different code based on different
conditions.
• The If statement
The if statement executes a piece of
code if a condition is true.
if (condition) {
// code to be executed in case the
condition is true
}
Example would be:
<?php
$age = 13;
if ($age < 21) {
echo "You are a teenager";
}
?>
• The If…Else statement
The If…Else statement executed a
piece of code if a condition is true and
another piece of code if the condition is
false.
if (condition) {
// code to be executed in case the
condition is true
}
else {
// code to be executed in case the
condition is false
}
Example would be:
<?php
$age = 27;
if ($age < 21) {
echo "You are a teenager";
}
else {
echo "You are an adult";
}
?>
• The If…Elseif…Else statement
This kind of statement is used to
define what should be executed in the
case when two or more conditions are
present.
if (condition1) {
// code to be executed in case condition1 is true
}
elseif (condition2) {
// code to be executed in case condition2 is true
}
else {
// code to be executed in case all conditions are false
}
Example would be:
<?php
$age = 3;
if ($age < 10) {
echo "You are a kid";
} elseif ($age < 20) {
echo "You are a teenager";
} else {
echo "You are an adult";
}
?>
Loops in PHP
In PHP, just like any other
programming language, loops are used to
execute the same code block for a
specified number of times. Except for the
common loop types (for, while, do…while),
PHP also support foreach loops, which is
not only specific to PHP.
• The for loop
The for loop is used when the
programmer knows in advance how many
times the block of code should be
executed. This is the most common type of
loop encountered in almost every
programming language.
for (initialization; condition; step){
// executable code
}
Example would be:
<?php
for ($i=0; $i < 5; $i++) {
echo "This is loop number $i
";
}
?>
• The while loop
The while loop is used when we want
to execute a block of code as long as a
test expression continues to be true.
while (condition){
// executable code
}
Example would be:
<?php
$i=0; // initialization
while ($i < 5) {
echo "This is loop number $i
";
$i++; // step
}
?>
• The do…while loop
The do...while loop is used when we
want to execute a block of code at least
once and then as long as a test expression
is true.
do {
// executable code
}
while (condition);
Example would be:
<?php
$i = 0; // initialization
do {
$i++; // step
echo "This is loop number $i
";
}
while ($i < 5); // condition?>
• The foreach loop
The foreach loop is used to loop
through arrays, using a logic where for
each pass, the array element is considered
a value and the array pointer is advanced
by one, so that the next element can be
processed.
foreach (array as value) {
// executable code
}
Example would be:
<?php
$var = array('a’, 'b’, 'c’, 'd’, 'e'); // array declaration
foreach ($var as $key) {
echo "Element is $key
";
}
?>
PHP Arrays
Arrays are used to store multiple values in a
single variable. A simple example of an array in PHP
would be:
<?php
$languages = array("JS”, "PHP”, “SQL”, “C#”);
?>
Array elements are accessed like
this: $arrayName [positionIndex]. For the
above example we could access “PHP”
this way: $languages[1]. Position index is
1 because in programming languages the
first element is always element 0. So, PHP
would be 1 in this case.
Three types of arrays in PHP
• Indexed Arrays
We can create these kind of arrays in
two ways shown below:
<?php
$names = array(“Gon", “Luffy", “Killua");
?>
<?php
// this is a rather manual way of doing it
$names[0] = “Gono";
$names[1] = “Luffy";
$names[2] = “Killua";
?>
Example would be:
<?php
$names = array(“Gon", “Luffy", “Killua");
echo "My friends are " . $names[0] . ", " . $names[1] . " and " . $names[2];
?>
Looping through an indexed array is
done like
<?php
$names = array(“Gon", “Luffy", “Killua");
$arrayLength = count($names);
for($i = 0; $i < $arrayLength; $i++) {
echo $names[$i];
echo "";
}
?>
• Associative Arrays
Associative arrays are arrays which
use named keys that you assign. Again,
there are two ways we can create them:
<?php
$namesAge = array(“Gon"=>“12", “Luffy"=>"14", “Killua"=>“13");
?>
<?php
// this is a rather manual way of doing it
$namesAge[‘Gon'] = “12";
$namesAge[‘Luffy'] = "14";
$namesAge[‘Killua'] = “13";
?>
Example would be:
<?php
$namesAge = array(“Gon"=>“12", “Luffy"=>"14", “Killua"=>“13");
echo “Gon's age is " . $namesAge[‘Gon'] . " years old.";
?>
Looping through an associative
array is done like
<?php
$namesAge = array(“Gon"=>“12", “Luffy"=>"14", “Killua"=>“14");
foreach ($namesAge as $i => $value) {
echo "Key = " . $i . ", Value = " . $value;
echo "";
}
?>
• Multidimensional Arrays
This is a rather advanced PHP stuff, but to
understand what is a multidimensional array is.
Basically, it is an arrays the elements of which
are other arrays. For example, a three-
dimensional array is an array of arrays of
arrays.
<?php
$socialNetowrks = array (
array("Facebook", “Feb", 14),
array("Twitter",“Dec", 16),
array("Instagram",“Aug", 15));
?>
Self-Assessment