UNIT - 4
Server-Side scripting with PHP: A
Comprehensive Introduction:
Outline
Overview of PHP and its Role in Web Development,
PHP Syntax and Configuration,
Variables and Constants,
Data Types in PHP
Operators: Arithmetic, Logical, Assignment, and Bitwise,
Flow Control Statements:
Simple If, If-Else, Else-If Clause, Switch Statements
Looping Constructs:
While, Do-While, For, and Break Statements,
Outline
• Understanding PHP Arrays,
• String Manipulation and Functions,
• Handling File Uploads,
• Working with Dates and Time zones,
• Using Regular Expressions for Pattern Matching,
• Exception Handling in PHP.
Overview of PHP and its Role in Web
Development
PHP is an acronym for "PHP: Hypertext Preprocessor".
PHP is a widely-used, open source scripting language.
PHP scripts are executed on the server.
PHP is free to download and use.
PHP was developed in 1994 by Apache group.
What is a PHP File?
PHP files can contain text, HTML, CSS, JavaScript, and PHP code.
PHP code is executed on the server, and the result is returned to the
browser as plain HTML.
PHP files have extension ".php".
PHP Operators
Operators are used to perform operations on variables and values.
Arithmetic operators
Logical operators
Assignment operators
Bitwise operators
PHP Arithmetic Operators
The PHP arithmetic operators are used with numeric values to
perform common arithmetical operations, such as addition,
subtraction, multiplication etc.
PHP Logical Operators
The PHP logical operators are used to combine conditional
statements.
PHP Assignment Operators
The PHP assignment operators are used with numeric values to write
a value to a variable.
The basic assignment operator in PHP is "=". It means that the left
operand gets set to the value of the assignment expression on the
right.
Bitwise operators
In PHP, bitwise operators are used to perform operations on binary
numbers at the bit level. These operators are particularly useful when
dealing with flags or binary data manipulation.
1. AND (&):
The & operator compares each corresponding bit of two operands
and returns 1 if both bits are 1, otherwise, it returns 0.
2. OR (|):
The | operator compares each corresponding bit of two operands
and returns 1 if at least one of the bits is 1.
3. XOR (^):
The ^ operator compares each corresponding bit of two operands
and returns 1 if the bits are different, otherwise, it returns 0.
4. NOT (~):
The ~ operator inverts the bits of the operand (i.e., changes 1 to 0 and 0
to 1).
5. Left Shift (<<):
The << operator shifts the bits of the left operand to the left by the
number of positions specified by the right operand. The left shift
effectively multiplies the number by 2^n (where n is the number of
positions shifted).
6. Right Shift (>>):
The >> operator shifts the bits of the left operand to the right by the
number of positions specified by the right operand. The right shift
effectively divides the number by 2^n (where n is the number of
positions shifted
Flow Control Statements:
Simple If
If-Else
Else-If Clause
Switch Statements
if statement - executes some code if one condition is true
if...else statement - executes some code if a condition is
true and another code if that condition is false
if...elseif...else statement - executes different codes for
more than two conditions
switch statement - selects one of many blocks of code to
be executed.
The if Statement:
The if statement executes some code if one condition is true.
Syntax:
if (condition) {
// code to be executed if condition is true;
}
Example:
1.
if (5 > 3) {
echo "Have a good day!";
}
Output "Have a good day!" if 5 is larger than 3:
2.
$t = 14;
if ($t < 20) {
echo "Have a good day!";
}
Output "Have a good day!" if $t is less than 20:
The if...else Statement
The if...else statement executes some code if a condition is true and another code
if that condition is false.
Syntax:
if (condition) {
// code to be executed if condition is true;
} else {
// code to be executed if condition is false;
}
Example
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
Else-If Clause
The else-if (written as elseif in PHP) is used to check multiple conditions
sequentially. It helps control the flow of a program by executing certain code
blocks when specific conditions are true. If none of the if or elseif conditions are
met, the else block is executed as a fallback.
Syntax of else-if in PHP:
if (condition1) {
// Code block for condition1
} elseif (condition2) {
// Code block for condition2
} elseif (condition3) {
// Code block for condition3
} else {
// Code block if none of the above conditions are true
}
Example
<?php
// Define the student's score
$score = 78; // You can change this value to test different conditions
// Check the grade using if-elseif-else
if ($score >= 90) {
echo "Grade: A";
} elseif ($score >= 80) {
echo "Grade: B";
} elseif ($score >= 70) {
echo "Grade: C";
} elseif ($score >= 60) {
echo "Grade: D";
} else {
echo "Grade: F"; // For any score below 60
}
?>
Switch Statements
The switch statement is used to perform different actions
based on different conditions.
Use the switch statement to select one of many blocks of
code to be executed.
Syntax:
switch (expression) {
case label1:
//code block
break;
case label2:
//code block;
break;
case label3:
//code block
break;
default:
//code block
}
Looping Constructs:
While
Do-While
For
Break Statements,
Looping Constructs:
when you write code, you want the same block of code to run over and
over again a certain number of times. So, instead of adding several almost
equal code-lines in a script, we can use loops.
Loops are used to execute the same block of code again and again, as
long as a certain condition is true.
In PHP, we have the following loop types:
while - loops through a block of code as long as the specified condition is
true
do...while - loops through a block of code once, and then repeats the loop
as long as the specified condition is true
for - loops through a block of code a specified number of times
foreach - loops through a block of code for each element in an array
While
The while loop - Loops through a block of code as long as the
specified condition is true.
The PHP while Loop
The while loop executes a block of code as long as the specified
condition is true.
The while loop does not run a specific number of times, but checks
after each iteration if the condition is still true.
The condition does not have to be a counter, it could be the status of
an operation or any condition that evaluates to either true or false.
Example
Print $i as long as $i is less than 6:
$i = 1;
while ($i < 6) {
echo $i;
$i++;
}
Do while Loop
The do...while loop - Loops through a block of code once,
and then repeats the loop as long as the specified condition
is true.
The PHP do...while Loop
The do...while loop will always execute the block of code
at least once, it will then check the condition, and repeat
the loop while the specified condition is true.
Example
Print $i as long as $i is less than 6:
$i = 1;
do {
echo $i;
$i++;
} while ($i < 6);
The for loop
Loops through a block of code a specified number of times.
The PHP for Loop
The for loop is used when you know how many times the script should
run.
Syntax:
for (expression1, expression2, expression3) {
// code block
}
This is how it works:
expression1 is evaluated once
expression2 is evaluated before each iteration
expression3 is evaluated after each iteration
Example:
Print the numbers from 0 to 10:
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
Example Explained
The first expression, $x = 0;, is evaluated once and sets a
counter to 0.
The second expression, $x <= 10;, is evaluated before each
iteration, and the code block is only executed if this
expression evaluates to true. In this example the expression is
true as long as $x is less than, or equal to, 10.
The third expression, $x++;, is evaluated after each iteration,
and in this example, the expression increases the value of $x
by one at each iteration.
The break statement
The break statement can be used to jump out of different kind of
loops.
Break in For loop
The break statement can be used to jump out of a for loop.
Example
Jump out of the loop when $x is 4:
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
break;
}
echo "The number is: $x <br>";
}