PHP Ch2
PHP Ch2
College of Computing
Department of Information
Technology
Internet Programming II
Arithmetic operators
Assignment operators
Comparison operators
Increment/Decrement operators
Logical operators
String operators
Array operators
Assignment Operators
The assignment operators are used to assign
value to different variables. The basic
assignment operator is "=".
6 PHP Operators Cont’d…
Bitwise Operators
The bitwise operators are used to perform bit-
level operations on operands. These operators
allow the evaluation and manipulation of
specific bits within the integer.
7 PHP Operators Cont’d…
Comparison Operators
Comparison operators allow comparing two
values, such as number or string.
8 PHP Operators Cont’d…
Incrementing/Decrementing Operators
The increment and decrement operators are
used to increase and decrease the value of a
variable.
9 PHP Operators Cont’d…
Logical Operators
The logical operators are used to perform bit-
level operations on operands.
10 PHP Operators Cont’d…
Array Operators
The array operators are used in case of array.
11 PHP Conditional Statements
1.if Statement.
2.if-else Statement
3.If-elseif-else Statement
4.Switch statement
12 Conditional Statements Cont’d…
if Statement
This statement executes the block of code
inside the if statement if the expression is
evaluated as True.
Syntax
if (condition) {
<?php
// code to be executed if condition is true;
$x = 22;
if ($x < 23) {
} echo “$x is less than 23";
}
Example: ?>
13 Conditional Statements Cont’d…
Example: <?php
$num=12;
if($num%2==0){
echo "$num is even number";
}else{
echo "$num is odd number";
}
?>
14 Conditional Statements Cont’d…
} elseif (condition2){
//
code to be executed if condition2 is true
} elseif (condition3){
//
code to be executed if condition3 is true
15 Conditional Statements Cont’d…
If-else-if Example:
<?php
$num = 4;
if($num > 0){
echo "num is positive";
}
else if($num < 0){
echo "num is negative";
}
else
{
echo "num is zero";
}
?>
16 Conditional Statements Cont’d…
Switch Statement
This statement allows us to execute different
<?php
blocks of code based on
$i = different conditions.
"2";
switch ($i) {
case 0:
echo "i equals 0";
break;
Example: case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
default:
echo "i is not equal to 0, 1 or 2";
}
?>
17 PHP Loops
While loop
The while loop executes a block of code as long as the
specified condition is true.
Syntax
Example
while (condition is true) { <?php
code to be executed; $x=1;
while($x<=5) {
} echo "The number
is: $x <br>";
$x++;
}
?>
19 PHP Loops Cont’d…
do...while Loop
The do...while loop will always execute the block of code
once, it will then check the condition, and repeat the loop
while the specified condition is true.
Syntax
do {
code to be executed;
} while (condition is true);
The example below first sets a variable $x to 1 ($x=1;).
Then, the do while loop will write some output, and then
increment the variable $x with 1. Then the condition is
checked (is $x less than, or equal to 5?), and the loop will
20 PHP Loops Cont’d…
<?php
$x=1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x<=5);
?>
21 PHP Loops Cont’d…
Parameters:
init counter: Initialize the loop counter value
0 to 10:
<?php
for($x=0;$x<=10;$x++){
?>
23 PHP Loops Cont’d…
<?php
$colors=array("Red","Yellow","Blue");
foreach($colors as $value){
echo $value;
echo "<br>";
?>
25 PHP Arrays
Indexed arrays
Indexed Arrays We can create these kind of
arrays in two ways shown below:
Example1
<?php
$fruits = array("Banana","Orange","Papaya");
echo $fruits[0].",".$fruits[1].",".$fruits[2];
?>
28 PHP Arrays Cont’d…
Indexed arrays
Example 2
<?php
$names[0] = "Html";
$names[1] = "JavaScript";
$names[2] = "VBScript";
?> Output
Html,JavaScript and VBScript are Client Side
Scripting Languages
29 PHP Arrays Cont’d…
Example 1
In this example we use an array to assign ages
to the different persons:
Example 1
<?php
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
?>
31 PHP Arrays Cont’d…
Example2
<?php
$age = array("Fabio"=>"20",
"Klevi"=>"16", "John"=>"43");
?>
32 PHP Arrays Cont’d…
<?php
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15));
Example1
<html>
<body>
<?php
function writeMessage()
{
echo "This is PHP Function";
}
writeMessage();
?>
</body>
</html>
37 PHP Function Cont’d…
Adding parameters
Our first function (writeMessage()) is a very
simple function. It only writes a static string. To
add more functionality to a function, we can
Example
add parameters. A parameter is just like a
<?php
variable.
function ServerSideScripts($Sname) {
echo $Sname. " " ."is Server Side Scripting
Language"."<br>";
}
ServerSideScripts("PHP");
ServerSideScripts("Python");
ServerSideScripts("ASP");
?>
38 PHP Function Cont’d…
Example
<?php
function add($x,$y)
{
$total = $x + $y;
return $total;
}
echo "Total = " . add(1,16);
?>
39 PHP Forms and User Input
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
41 PHP Forms Cont’d…
Both
Other Validations.
The below code validates the The below code validates the
email address: of the input:
$email = $_POST ["Email"]; $mobileno = strlen ($_POST ["Mo
$pattern = "^[_a-z0-9-]+(\.[_a- bile"]);
z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-] $length = strlen ($mobileno);
+)*(\.[a-z]{2,3})$^";
if (! if ( $length < 10 && $length > 1
preg_match ($pattern, $email) ) 0) {
{ $ErrMsg = "Mobile must have
$ErrMsg = "Email is not valid." 10 digits.";
; echo $ErrMsg;
echo $ErrMsg; } else {
} else { echo "Your Mobile number is: "
echo "Your valid email address .$mobileno;
is: " .$email; }
}
46 PHP Form Validation Cont’d…
Example
<!DOCTYPE HTML> test_input($_POST["name"]);
<html> }
<head> if (empty($_POST["email"])) {
<style> $emailErr = "Email is required";
.error {color: #FF0000;} } else {
</style> $email =
</head> test_input($_POST["email"]);
<body> }
<?php }
// define variables and set to function test_input($data) {
empty values $data = trim($data);
$nameErr = $emailErr = ""; $data = stripslashes($data);
$name = $email = ""; $data = htmlspecialchars($data);
if ($_SERVER["REQUEST_METHOD"] return $data;
== "POST") { }
if (empty($_POST["name"])) { ?>
$nameErr = "Name is required";
} else {
$name =
48 PHP Form Validation Cont’d…
Example Cont’d…
<h2>PHP Form Validation
Example</h2> <?php
<p><span class="error">* if(isset($_POST['submit'])) {
required field</span></p> if($nameErr == "" && $emailErr
<form method="post" action="<? == "") {
php echo echo "<h3 color = #FF0001>
htmlspecialchars($_SERVER["PHP_ <b>You have sucessfully
SELF"]);?>"> registered.</b> </h3>";
Name: <input type="text" echo "<h2>Your
name="name"> Input:</h2>";
<span class="error">* <?php echo $name;
echo $nameErr;?></span> echo "<br>";
<br><br> echo $email;
E-mail: <input type="text" }
name="email"> else {
<span class="error">* <?php echo "<h3> <b>You didn't
echo $emailErr;?></span> filled up the form correctly.</b>
<br><br> </h3>";
49 Use Get or Post
Get Method
The get method is used mainly in conjunction with database
searches and has the advantage that you can bookmark a
search result because all the data is in the URL.
Data sent by the get method are stored in the $_GET array.
Post method
The $_POST array contains data sent using the post method.
You can test whether the form has been submitted by creating
a conditional statement and passing $_POST['send'] to isset().
Thank you