0% found this document useful (0 votes)
65 views81 pages

UNit 1 Expressions and Control Statements in PHP

The document provides an overview of web-based application development using PHP, covering its history, characteristics, features, and advantages. It includes instructions for installing PHP via XAMPP, writing and running PHP scripts, and understanding PHP variables, data types, and operators. Additionally, it outlines the course outcomes and prerequisites for learning PHP.

Uploaded by

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

UNit 1 Expressions and Control Statements in PHP

The document provides an overview of web-based application development using PHP, covering its history, characteristics, features, and advantages. It includes instructions for installing PHP via XAMPP, writing and running PHP scripts, and understanding PHP variables, data types, and operators. Additionally, it outlines the course outcomes and prerequisites for learning PHP.

Uploaded by

Geeta Birle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

WEB BASED APPLICATION

DEVELOPMENT WITH
PHP(22619)

Mrs.ShindeA.S
Lecture In Information Technology
Competency

Develop simple web based application using


PHP Language
Course Outcomes:

⚫ Develop simple program using control statement.


⚫ Perform operations based on arrays and strings.
⚫ Develop programs by applying object oriented
concepts.
⚫ Design simple Web pages using form controls.
⚫ Perform database operations using MYSQL and PHP.
Introduction to PHP
⚫ PHP is also known as Hypertext Pre-processor.
⚫ PHP is simple and easy to learn language.
⚫ PHP is an interpreted language.
⚫ PHP is an object-oriented language.
⚫ PHP is an open-source and server-side scripting language,which is
mainly used for developing dynamic web pages and applications.
⚫ PHP is scripting language that can be executed at the server-side.
⚫ PHP is well suited for web development.
⚫ PHP is faster than other scripting languages, for example,ASP
and JSP.
⚫ PHP can be embedded into H T M L.
⚫ The syntax of the PHP language is similar to the C language.
History of PHP
⚫ PHP was created by Rasmus Lerdorf in 1994 but appeared in
the market in 1995.
⚫ Since the first version release in 1994,there have been more
versions released for PHP upgradation purposes.
⚫ Versions of PHP
✓ PHP 1.0
✓ PHP 2.0
✓ PHP 3.0
✓ PHP 4.0
✓ PHP 5.0
✓ PHP 6.0
✓ PHP 7.0.
Characteristics of PHP
⚫ It is simple and easy to use.
⚫ It is efficient and able to connect the database and load the
application faster.
⚫ It provides more security to the web application with the help of
using P H P frameworks.
⚫ It is more familiar for the developers,and online support is being
provided for beginners.
⚫ It shows flexibility and the ability to get integrated with
another programming language.
⚫ It is open-source and free of cost.
⚫ It is lightweight, and number of frameworks available to use
for developing the web application.
PHP Features
⚫ PHP is very popular language because of its simplicity and open source.
There are some important features of PHP given below:
PHP Features
➢ Performance:
PHP script is executed much faster .PHP uses its own memory,so
the server workload and loading time is automatically reduced,which
results in faster processing speed and better performance.
➢ Open Source:
PHP source code and software are freely available on the web.All its
components are free to download and use.
➢ Familiarity with syntax:
PHP has easily understandable syntax.Programmers are comfortable
coding with it.
➢ Embedded:
PHP code can be easily embedded within HTML tags and script.
➢ Platform Independent:
PHP is available forWINDOWS, MAC, LINUX & UNIX operating
system. A PHP application developed in one OS can be easily
executed in other OS also.
➢ Database Support:
PHP supports all the leading databases such as MySQL, SQLite, ODBC, etc.
➢ Error Reporting -
PHP has predefined error reporting constants to generate an error notice or
warning at runtime..
➢ Loosely Typed Language:
PHP allows us to use a variable without declaring its datatype
➢ Web servers Support:
PHP is compatible with almost all local servers used today like Apache,
Netscape, Microsoft IIS, etc.
➢ Security:
PHP is a secure language to develop the website. It consists of multiple layers
of security to prevent threads and malicious attacks.
➢ Control:
Different programming languages require long script or code, whereas PHP
can do the same work in a few lines of code. It has maximum control
over the websites like you can make changes easily whenever you want.
➢ A Helpful PHP Community:
It has a large community of developers who regularly updates documentation,
tutorials, online help, and FAQs. Learning PHP from the communities is one of
the significant benefits.
Prerequisites
➢ Before learning PHP,you must have the basic knowledge
of HTML,C S S
➢ H T M L - HTML is used to design static webpage.
➢ C S S - CSS helps to make the webpage content more effective and
attractive.
➢ PHP is a programming language used to enhance websites built
with HTML
Advantages of PHP
➢ Open Source
➢ Platform Independent
➢ Simple and Easy
➢ Database
➢ Fast
➢ Maintenance
➢ Support
➢ Testing
➢ Security
➢ Stable
Install PHP

⚫ To install PHP,we will install X A MPP (Cross,


Apache,MySQL, PHP,Perl) software stack.
⚫ It is available for all operating systems.
How to install XAM PP server on windows
⚫ Step 1: Click on the link https://www.apachefriends.org/download.html
provided to download the X A M P P server according to your window
requirement.
⚫ Step 2: After downloading XAMPP, double click on the
downloaded file and allow XAMPP to make changes in your system.
A window will pop-up,where you have to click on
the Next button.
⚫ Step 3: Here, select the components,which you want to install
and click Next.
⚫ Step 4: Choose a folder where you want to install the XAMPP in
your system and click Next.
⚫ Step 5: Click Next and move ahead.
⚫ Step 6: XAMPP is ready to install,so click on
the Next button and install the XAM PP.
⚫ Step 7: A finish window will display after successful installation.
Click on the Finish button.
⚫ Step 8: Choose your preferred language.
⚫ Step 9: XAMPP is ready to use. Start theApache server and
MySQL and run the php program on the localhost.
⚫ Step 10: If no error is shown, then XAMPP is running successfully.
Write and Run PHP on X A M M P
Write syntax of PHP
⚫ Generally,a PHP file contains HTML tags and some PHP scripting
code.
⚫ It is very easy to create a simple PHP example.
⚫ To do so, create a file and write HTML tags + PHP code and save
this file with .php extension.
⚫ All PHP code goes between the php tag.
⚫ It starts with <?php and ends with ?>.
⚫ The syntax of PHP tag is given below:

<?php
//your code here
?>
Example

Step 1: Create a simple PHP program like Hello World.

<?php
echo " Hello World";
?>
Step 2: Save the file with hello.php name in the htdocs folder,
which resides inside the xampp folder
Step 3: Run the X A MPP server and start theApache and MySQL.
Step 4: Now, open the web browser and type localhost
http://localhost/hello.php on your browser window.
Step 5:The output for the above hello.php program will be shown
as the screenshot below:
Variables
⚫ Variables are "containers" for storing
information.
⚫ The variables are declared to store
information.
⚫ Declaring P H P Variables
⚫ In PHP, a variable starts with the $ sign, followed
by the name of the variable:
$txt = "Hello world!";
$x = 5;
$y = 10.5;
Rules for PHP variables:
Write down rules for declaring PHP variable.

1. A variable starts with the $ sign, followed by the name of


the variable
2. A variable name must start with a letter or the
underscore character
3. A variable name cannot start with a number
4. A variable name can only contain alpha-numeric
characters and underscores (A-z,0-9,and _ )
5. Variable names are case-sensitive ($age and $AGE are
two different variables)
6. A variable name should not contain spaces. If a variable
name is more than one word, it should be separated with
an underscore ($first_name), or with capitalisation
($firstName).
7. Variables used before they are assigned have default values.
8. Variables can, but do not need, to be declared before assignment.
PHP automatically converts the variable to the correct data type,
depending on its value.
9. Variables in PHP do not have intrinsic types -a variable does not
know in advance whether it will be used to store a number or a
string of characters
Example 1
Example 2
<?php
$x = 5;
Output:
9
$y = 4; 9
echo $x + $y;
?>
Example 3
PHP data types
List any four data types of PHP
⚫ Variables can store data of different types,and different data types
can do different things.
⚫ PHP supports the following data types:
1. String
2. Integer
3. Float (floating point numbers - also called double)
4. Boolean
5. Array
6. Object
7. NULL
8. Resource
✓ String
A string is a sequence of characters, like "Hello world!".
A string can be any text inside quotes.You can use single or double quotes:
$x = "Hello world!";
$y = 'Hello world!';
✓ Integer
An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647.
$x = 5985;
✓ Float
A float (floating point number) is a number with a decimal point or a number in exponential
form.
$x = 10.365;
✓ Boolean
A Boolean represents two possible states:TRUE or FALSE.
$x = true;
$y = false;
✓ Array
An array stores multiple values in one single variable.
$college = array(“PLGP",“GOV",“LATUR");
✓ NULL
Null is a special data type which can have only one value: NULL.
A variable of data type NULL is a variable that has no value assigned to it.
$x = null;
Object
⚫ Classes and objects are the two main aspects of object-oriented
programming.
⚫ A class is atemplate for objects, and an object is an instance of a
class.
⚫ When the individual objects are created,they inherit all the
properties and behaviors from the class,but each object will have
different values for the properties.
⚫ Objects are the instances of user-defined classes that can store
both values and functions.They must be explicitly declared.
<?php
class Student
{
function info()
{
$s_name = "Alinka Shinde";
echo "My Name is :" .$s_name;
}
}
$obj = new Student();
$obj -> info();
?>
Resource

⚫ Resources are not the exact data type in PHP.


⚫ It is the storing of a reference to functions and resources external
to PHP.
⚫ A common example of using the resource data type is a database
call.
PHP variable scope
⚫ In PHP,variables can be declared anywhere in the script.
⚫ The scope of a variable is the portion of the program/script within
which it is defined and can be accessed.
⚫ PHP has three types of variable scopes:
1. Local variable
2. Global variable
3. Static variable
Local Variable
⚫ The variables that are declared within a function are called local
variables and can only be accessed within that function.
⚫ Local variables cannot be accessed outside the function,as they
have local scope.

<?php
function local_var()
{
$num = 45; //local variable
echo "Local variable declared inside the function is:". $num;
}
local_var();
?>
Global Variable
⚫ The global variables are the variables that are declared outside the
function.
⚫ These variables can be accessed anywhere in the program.
⚫ To access the global variable within a function,use the global
keyword before the variable.

<?php
$x = 5;
function myTest()
{
global $x;
echo "Variable x inside function is:$x";
}
myTest();
echo "Variable x outside function is:$x";
?>
Static variables
⚫ Normally,when a function is completed/executed,all of its variables
are dele
⚫ Sometimes we need to store a variable even after completion of
function executed.
⚫ To do this,We use the static keyword before the variable to define
a variable,and this variable is called as static variable.

<?php
function static_var()
{
static $num1 = 3;
$num2 = 6;
$num1++;
$num2++;
echo "Static:" .$num1 ."</br>";
echo "Non-static:" .$num2 ."</br>";
}
static_var();
static_var();
?>
Expressions and operators
⚫ Expressions are the most important building blocks of PHP.
⚫ In PHP,almost anything you write is an expression.
⚫ The simplest yet most accurate way to define an expression is
"anything that has a value".
⚫ The most basic forms of expressions are constants and variables.
⚫ Example
$x=100
Operators
⚫ PHP Operator is a symbol i.e used to perform operations on
operands.
⚫ operators are used to perform operations on variables or values.
⚫ For example:
⚫ $num=10+20;//+ is the operator and 10,20 are operands and
$num is variable
⚫ PHP Operators can be categorized in following forms:
1. Arithmetic operators
2. Assignment operators
3. Comparison operators
4. Increment/Decrement operators
5. Logical operators
6. String operators
7. Array operators
8. Conditional assignment operators
Arithmetic Operators
⚫ The PHP arithmetic operators are used to perform common
arithmetic operations such as addition, subtraction, etc. with
numeric values.

Operato Name Example Explanation


r

+ Addition $a + $b Sum of operands


- Subtraction $a - $b Difference of operands
* Multiplication $a * $b Product of operands
/ Division $a / $b Quotient of operands
% Modulus $a % $b Remainder of operands
** Exponentiation $a ** $b $a raised to the power $b
Assignment Operators

⚫ The assignment operators are used to assign value to different


variables.The basic assignment operator is "="

Operator Name Example Explanation

= Assign $a = $b The value of right operand is assigned to


the left operand.

+= Add then Assign $a += $b Addition same as $a = $a + $b

-= Subtract then Assign $a -= $b Subtraction same as $a = $a - $b

*= Multiply then Assign $a *= $b Multiplication same as $a = $a * $b

/= Divide then Assign $a /= $b Find quotient same as $a = $a / $b


(quotient)
%= Divide then Assign $a %= $b Find remainder same as $a = $a % $b
(remainder)
Comparison Operators
⚫ Comparison operators allow comparing two values,such as
number or string.Below the list of comparison operators are given:

Operator Name Example Explanation


== Equal $a == $b Return TRUE if $a is equal to $b

!== Not identical $a !== $b Return TRUE if $a is not equal to $b, and they are not
of same data type

!= Not equal $a != $b Return TRUE if $a is not equal to $b

<> Not equal $a <> $b Return TRUE if $a is not equal to $b

< Less than $a < $b Return TRUE if $a is less than $b

> Greater than $a > $b Return TRUE if $a is greater than $b

<= Less than or equal to $a <= $b Return TRUE if $a is less than or equal $b

>= Greater than or equal to $a >= $b Return TRUE if $a is greater than or equal $b
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.

Operato Name Example Explanation


r

& And $a & $b Bits that are 1 in both $a and $b are set to 1, otherwise 0.
| Or (Inclusive or) $a | $b Bits that are 1 in either $a or $b are set to 1
^ Xor (Exclusive or) $a ^ $b Bits that are 1 in either $a or $b are set to 0.
~ Not ~$a Bits that are 1 set to 0 and bits that are 0 are set to 1

<< Shift left $a << $b Left shift the bits of operand $a $b steps
>> Shift right $a >> $b Right shift the bits of $a operand by $b number of places
Increment/Decrement operators
✓ Increment and decrement operators are also known as unary
operators because they operate on a single operand.
✓ The increment operator (++) adds 1 to its operand
✓ The decrement operator (–) subtracts one.

Operator Meaning
++ Increment Operator

-- Decrement Operator

Operator Name Description


++$x Pre-increment Increments $x by one, then returns $x
$x++ Post-increment Returns $x, then increments $x by one
--$x Pre-decrement Decrements $x by one, then returns $x
$x-- Post-decrement Returns $x, then decrements $x by one
Pre-increment Post-increment

Pre-Decrement Post-Decrement
Example
<?php
echo "<h3>Postincrement</h3>";
$a = 5;
echo "a++ = " .$a++ ."<br>";
echo "a = " .$a ."<br>";
echo "<h3>Preincrement</h3>";
$a = 5;
echo "++a = " .++$a ."<br>";
echo "a =" .$a ."<br>";
echo "<h3>Postdecrement</h3>";
$a = 5;
echo "a-- = " .$a-- ."<br>";
echo "a = " .$a ."<br>";
echo "<h3>Predecrement</h3>";
$a = 5;
echo "--a = " .--$a ."<br>";
echo "a = " .$a ."<br>";
?>
Logical Operators
PHP Logical operators are generally used when any action
depends on two or more conditions.

Name Operator What does it do? Example

It returns true if both the operands(or expressions) returns


And and or && $a && $b
true.
It returns true if any one out of the two operands(or
Or or or || $a || $b
expressions) returns true, or both return true.

It returns true if any one out of the two operands(or


Xor xor $a xor $b
expressions) returns true, but not when both return true.

This is a unary operator. It returns true, if the operand(or


Not ! !$a
expression) returns false.
String Operators

Operator Name Example Result


Concatenation of
. Concatenation $txt1 . $txt2
$txt1 and $txt2
Concatenatio Appends $txt2 to
.= $txt1 .= $txt2
n assignment $txt1

<?php
$a = “Alinka";
$b = “Shinde";
echo $a.$b
$a .= $b
echo $a;
?>
Conditional assignment operators
Operator Name Example Result
Returns the value of $x.
The value of $x
?: Ternary $x = expr1 ? expr2 : expr3 is expr2 if expr1 = TRUE.
The value of $x
is expr3 if expr1 = FALSE

<?php
$a = 10;
$b = $a > 15 ? 20 : 5;
Value of b is 5
print ("Value of b is " . $b);
?>

<?php
$age = 20;
print ($age >= 18) ? "Adult" : "Not Adult";
?>
Adult
constants
⚫ Constants are variables whose value cannot be changed. In other
words, once you set a value for a constant, you cannot change it.
⚫ In PHP,there are two ways to define a constant:
1. Using the define() method.
2. Using the const keyword.
⚫ we don't have to use $ symbol with the constant's name.
⚫ Using define()
⚫ Below is the syntax for using the define() function to create a constant.
⚫ define(name,value,case-insensitive)
➢ Parameters:
➢ name: Name of the constant
➢ value: Value of the constant
➢ case-insensitive: Specifies whether the constant name is case
sensitive or not. It's default value is false, which means, by default,the
constant name is case sensitive.
<?php
define(A,“Welcome.");
echoA;
?>
⚫ Using the const Keyword

⚫ We can also define constants in PHP using the const keyword.


⚫ But we can only use the const keyword to define scalar constants,i.e.only
integers, floats,booleans and strings,while define() can be used to define
array and resource constants as well.

<?php
const A = “WELCOME.";
echo A;
?>

When we define a constant using the const keyword, the constant


name is always case sensitive.
Decision Making Control
Statements
⚫ While writing programs/scripts, there will be scenarios where you
would want to execute a particular statement only if some
condition is satisfied.In such situations we use Conditional
statements.
⚫ In PHP,there are 4 different types of Conditional Statements.
➢ if statements
➢ if...else statements
➢ if...elseif...else statements
➢ switch statement
The if statement

if(condition)
{
//code to be executed if 'condition' is true
}

<?php
$age = 16;
if($age < 18)
{
echo "You are not allowed to Vote";
} ?>
The if...else statement
if(condition)
{
//code to be executed if 'condition' is true
}
else
{
//code to be executed if 'condition' is false
}
Example 1

<?php
$age = 20;
if($age>=18)
{
echo "You are allowed to Vote";
}
else
{
echo “Not eligible for vote";
}
?>
Example 2

<?php
$first_number = 17;
$second_number = 2;
if ($first_number > $second_number)
{
echo "$first_number is greater than $second_number";
}
else
{
echo "$second_number is greater than $first_number";
}
?>
if-elseif-else statement
if (expression1)
{
//code is executed if the expression1 evaluates toTRUE
}
elseif (expression2)
{
//code is executed if the expression2 evaluates toTRUE
}
elseif (expression3)
{
//code is executed if the expression3 evaluates toTRUE
}
else
{
//code is executed if the expression1,expression2 and expression3
evaluates to FALSE,a default choice
}
Example 1

<?php
$x=0;
if ($x == 2)
{
echo “$x is equal to 2”;
}
else if ($x == 1)
{
echo “$x is equal to 1” ;
}
else
{
echo "x is not equal to 1 & 2";
}
?>
Example 2
<?php
$age = 50;
if ($age < 30)
{
echo "Your age is less than 30!";
}
elseif ($age > 30 && $age < 40)
{
echo "Your age is between 30 and 40!";
}
elseif ($age > 40 && $age < 50)
{
echo "Your age is between 40 and 50!";
}
else
{
echo "Your age is greater than 50!";
}
?>
Switch statement
⚫ The switch-case statement is an alternative to the if-elseif-else
statement.
⚫ The switch-case statement tests a variable against a series of values
until it finds a match, and then executes the block of code
corresponding to that match.
⚫ The switch statement works with keywords: case and break ,default.
⚫ The case keyword is used to test a label against a value from the
round brackets.
⚫ The break statement is used to stop the automatic control flow into
the next cases and exit from the switch case.
⚫ The default statement contains the code that would execute if none
of the cases match.
⚫ PHP allows you to use number, character, string, as well as functions in
switch expression.
⚫ You can use semicolon (;) instead of colon (:).It will not generate any
error.
⚫ Syntax
switch (expression)
{
case value1:
code to be executed if expression = value1;
break;
case value2:
code to be executed if expression = value2;
break;
case value3:
code to be executed if expression = value3;
break; ….
default:
code to be executed if expression is different
from all labels;
}
Example 1

<?php
$i=2;
switch ($i)
{
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
}
?>
Example 2

<?php
$days= "sunday";
switch ($days) {
case "sunday":
echo "Today Is Sunday !";
break;
case "monday":
echo "Today Is Sunday !";
break;
case "tuesday":
echo "Today Is Tuesday!";
break;
case "wednesday":
echo "Today Is Wednesday!";
break;
case "thursday":
echo "Today Is Thursday!";
break;
case "friday":
echo "Today Is Friday!";
break;
case "saturday":
echo "Today Is Saturday!";
break;
default:
echo "You Have EnterWrong Days !";
}
?>
Example 3
<?php
$num=20;
switch($num)
{
case 10:
echo("number is equals to 10");
break;
case 20:
echo("number is equal to 20");
break;
case 30:
echo("number is equal to 30");
break;
default:
echo("number is not equal to 10, 20 or 30");
}
?>
Example 4
<?php
$ch = 'U';
switch ($ch) case 'E':
{ echo "Given character is vowel";
case 'a': break;
echo "Given character is vowel"; case 'I':
break; echo "Given character is vowel";
case 'e': break
echo "Given character is vowel"; ; case 'O ':
break echo "Given character is vowel";
; case 'i': break;
echo "Given character is vowel"; case 'U':
break; echo "Given character is vowel";
case 'o': break;
echo "Given character is vowel"; default:
break; echo "Given character is
case 'u': consonant";
echo "Given character is vowel"; break;
break }
; case ?>
'A':
echo "Given character is vowel";
break;
Explain the use of break and continue statements
break statement
⚫ The break statement breaks the execution of the current for,
while, do-while,switch,and for-each loop.
⚫ If you use break inside inner loop, it breaks the execution of inner
loop only.
⚫ It breaks the current flow of the program at the specified condition
and program control resumes at the next statements outside the
loop.
Example:
⚫ Syntax:
<?php
if (condition) for($i=1;$i<=10;$i++)
{ {
echo "$i <br/>";
break;
if($i==6)
} {
break;
}
}
?>
continue statement
⚫ The PHP continue keyword is used to halt the current iteration of
a loop but it does not terminate the loop.
It is used to skip the execution of a particular statement inside the
loops.
Syntax :
if (condition) { continue; }
Example:
<?php
for( $i=1;$i<=10;$i++)
{
if( $i == 4 )
continue;
echo "$i <br />";
}
?>
<?php
$num = 0;
while ($num < 10)
{
$num++;
if (($num % 2) == 0)
continue;
echo "$num ";
}
echo "\n";
?>
Loop control statements
Explain different loops in PHP with example

⚫ Loops in PHP are used to execute the same block of code block of
code again and again,as long as a certain condition is true.
1. while
2. do...while
3. for
4. foreach
while loop
⚫ The while loop is also called an Entry control loop because the
condition is checked before entering the loop body.This means that first
the condition is checked.If the condition is true, the block of code will be
executed.
⚫ Syntax
while(condition)
{
//code to be executed
}
Example
<?php
$n=1;
while($n<=20)
{
echo "$n<br/>";
$n++;
}
?>
<?php
$num = 2;
while ($num < 12)
{
$num += 2;
echo $num,"\n";
}
?>
do..while
⚫ do-while statements are similar to While statements, except
that the condition is tested at the end of each iteration,rather than
at the beginning.
⚫ This means that the Do...While the loop is guaranteed to run at
least once.
⚫ Syntax :
do
{
code to be executed;
}
while (condition);
<?php
$x = 5;
do
{
echo "Welcome to PLGPL! </br>";
$x++;
} while ($x < 10);
?>

<?php
$x = 1;
do {
echo "1 is not greater than 10.";
echo "</br>";
$x++;
} while ($x > 10);
echo $x;
?>
Difference between while and do-while loop

while Loop do-while loop


The while loop is also named as entry The do-while loop is also named as exit
control loop. control loop.
The body of the loop does not execute if The body of the loop executes at least
the condition is false. once, even if the condition is false.
Condition checks first, and then block of Block of statements executes first
statements executes. and then condition checks.
This loop does not use a semicolon to Do-while loop use semicolon to terminate
terminate the loop. the loop.
for loop
⚫ This type of loops is used when the user knows in advance, how
many times the block needs to execute.These type of loops are
also known as entry-controlled loops.
⚫ There are three main parameters to the code, namely the
initialization,the test condition and the counter.
⚫ Syntax:
for (initialization ;condition;increment/decrement)
{
//code to be executed
}
<?php
for ($x = 0;$x <= 10; $x++)
{
echo "The number is:$x <br>";
}
?>
<?php
for ($num = 1;$num <= 10; $num += 2)
{
echo "$num \n";
}
?>
for-each loop
Write syntax of for each loop foreach
⚫ The for-each statement is used to loop through arrays.
⚫ For each pass, the value of the current array element is assigned to
$value and the array pointer is moved by one and in the next pass
next element will be processed.
⚫ Syntax
foreach ($array as $value)
{
code to be executed;
}
<?php
$days= array("Sunday", "Monday", "Tuesday");
foreach($days as $value)
{
echo “$value <br>";
}
?>

You might also like