Professional Documents
Culture Documents
Web Design Programming Chapter 4
Web Design Programming Chapter 4
PHP Variables
"echo" to output the text "Hello
World!" on a web page:
PHP Variables
<body>
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
WEB DESIGN PROGRAMMING : CHAPTER 4
( PHP: Programming Introduction) ----JITENDRA SINGH
PHP is a Loosely
sensitive ($age and $AGE are
two different variables)
The example below returns the The output of the code above will
length of the string "Hello world!": be: !dlrow olleH.
Example
echo strrev("Hello world!"); //
outputs !dlrow olleH
?>
<?php
echo str_replace("world", "Dolly",
"Hello world!"); // outputs Hello
WEB DESIGN PROGRAMMING : CHAPTER 4
( PHP: Programming Introduction) ----JITENDRA SINGH
Dolly!
?>
- Subtracti $x - Differe Sh
The output of the code above will on $y nce of ow
$x and it »
be: Hello Dolly!
$y
PHP Operators
* Multiplic $x * Produc Sh
Operators are used to perform
ation $y t of $x ow
operations on variables and values.
and $y it »
PHP divides the operators in the
following groups:
/ Division $x / Quotie Sh
Arithmetic operators
$y nt of ow
Assignment operators
$x and it »
Comparison operators
$y
Increment/Decrement
operators
Logical operators
String operators
% Modulus $x % Remai Sh
Array operators
$y nder of ow
$x it »
PHP Arithmetic divided
by $y
Operators
The PHP arithmetic operators are ** Exponen $x ** Result
used with numeric values to perform tiation $y of
common arithmetical operations, raising
such as addition, subtraction, $x to
multiplication etc. the
$y'th
power
Oper Name Exa Result (Introd
ator mple uced in
PHP
5.6)
+ Addition $x + Sum of Sh
$y $x and ow
$y it »
WEB DESIGN PROGRAMMING : CHAPTER 4
( PHP: Programming Introduction) ----JITENDRA SINGH
PHP Assignment »
Operators
x /= y x= Division Sho
The PHP assignment operators are x/y w it
used with numeric values to write a »
value to a variable.
Assignme
nt
Sam Descriptio
e n PHP Comparison
as...
Operators
The PHP comparison operators are
x=y x= The left Sho used to compare two values
y operand w it (number or string):
gets set to »
the value
of the
Operat Name Exam Resu
expression
or ple lt
on the
right
to $y,
and > Great $x > Retur Sho
they er $y ns w it
are of than true »
the if $x
same is
type great
er
than
$y
!= Not $x != Retur Sho
equal $y ns w it
true »
if $x < Less $x < Retur Sho
is not than $y ns w it
equal true »
to $y if $x
is
less
than
<> Not $x <> Retur Sho $y
equal $y ns w it
true »
if $x
is not >= Great $x >= Retur Sho
equal er $y ns w it
to $y than true »
or if $x
equal is
to great
!== Not $x !== Retur Sho er
identic $y ns w it than
al true » or
if $x equal
is not to $y
equal
to $y,
or
they <= Less $x <= Retur Sho
are than $y ns w it
not of or true »
the equal if $x
same to is
type less
than
or
equal
WEB DESIGN PROGRAMMING : CHAPTER 4
( PHP: Programming Introduction) ----JITENDRA SINGH
to $y nt decrement »
s $x by
one
PHP Increment /
PHP Logical
Decrement Operators
Operators
The PHP increment operators are
used to increment a variable's value. The PHP logical operators are used
to combine conditional statements.
The PHP decrement operators are
used to decrement a variable's
value.
Operat Nam Examp Resu
or e le lt
not
both . Concate $txt1 Concate Sh
nation . nation ow
$txt2 of $txt1 it
and »
&& And $x && True Sho $txt2
$y if w it
both »
$x
and .= Concate $txt1 Append Sh
$y nation .= s $txt2 ow
are assignm $txt2 to $txt1 it
true ent »
|| Or $x || True Sho
$y if w it
either »
$x or PHP Array Operators
$y is
true The PHP array operators are used to
compare arrays.
Syntax
!== Non- $x Return
identit !== s true if (condition) {
y $y if $x is code to be executed if
not condition is true;
identic }
al to
$y The example below will output "Have
a good day!" if the current time
(HOUR) is less than 20:
WEB DESIGN PROGRAMMING : CHAPTER 4
( PHP: Programming Introduction) ----JITENDRA SINGH
Example
}
?>
<?php
PHP - The
$t = date("H");
}
echo "Have a good day!"; if...elseif....else
?>
Statement
Statement
than two conditions.
Example
Example
<?php
<?php $t = date("H");
$t = date("H");
if ($t < "10") {
if ($t < "20") { echo "Have a good morning!";
echo "Have a good day!"; } elseif ($t < "20") {
} else { echo "Have a good day!";
echo "Have a good night!"; } else {
WEB DESIGN PROGRAMMING : CHAPTER 4
( PHP: Programming Introduction) ----JITENDRA SINGH
Syntax
The PHP while Loop do {
code to be executed;
The while loop executes a block of
code as long as the specified } while (condition is true);
condition is true.
The example below first sets a
variable $x to 1 ($x = 1). Then, the
Syntax do while loop will write some output,
and then increment the variable $x
while (condition is true) { with 1. Then the condition is checked
code to be executed; (is $x less than, or equal to 5?), and
} the loop will continue to run as long
as $x is less than, or equal to 5:
WEB DESIGN PROGRAMMING : CHAPTER 4
( PHP: Programming Introduction) ----JITENDRA SINGH
Example
code to be executed;
}
<?php
$x = 1; Parameters:
Example
?>
<?php
$x = 6;
do {
The PHP foreach
echo "The number is: $x <br>";
$x++;
Loop
} while ($x <= 5);
?> The foreach loop works only on
arrays, and is used to loop through
each key/value pair in an array.
The PHP for Loop
Syntax
The for loop is used when you know
in advance how many times the foreach ($array as $value) {
script should run. code to be executed;
}
Syntax
For every loop iteration, the value of
for (init counter; test counter; the current array element is
increment counter) { assigned to $value and the array
WEB DESIGN PROGRAMMING : CHAPTER 4
( PHP: Programming Introduction) ----JITENDRA SINGH
Loop Through an
Associative Array
To loop through and print all the
values of an associative array, you
could use a foreach loop, like this:
Example
<?php
$age
= array("Peter"=>"35", "Ben"=>"37",
"Joe"=>"43");