Web Development PHP Tutorial | Adv.
Level | 2021 Batch
PHP Loops
• xU Fwpg;gpl;l epge;jid G+u;j;jpahFk; tiu xU Fwpj;j njhFjp epuiy kPz;Lk; kPz;Lk;
nraw;gLj;Jtjw;fhf ,it gad;gLj;jg;gLfpd;wd.
Loops are used to execute the same block of code again and again, as long as a certain condition is met.
• PHP ,y; gpd;tUk; ehd;F tifahd Loops gadgLj;jg;gLfpd;wd.
1. while → Fwpg;gpl;l epge;jiz cz;iknad kjpg;gLk; tiu xU njhFjpapDs;
kPz;Lk; kPz;Lk; nraw;gLfpd;wJ.
loops through a block of code as long as the condition specified evaluates to true.
2. do...while → xU njhFjp FwpKiw xU jilt nraw;gLj;jg;gl;ljd; gpd;du; epge;jid
kjpg;gLP nra;ag;gLk;. epge;jidahdJ cz;ikahf ,Ue;jhy; mf; Fwpj;j
epge;jid cz;ikahf ,Uf;Fk; tiu kPz;Lk; nraw;gLj;jg;gLk;.
the block of code executed once and then condition is evaluated. If the condition
is true the statement is repeated as long as the specified condition is true.
3. for → loops through a block of code until the counter reaches a specified number.
4. foreach → loops through a block of code for each element in an array.
PHP while Loops
Syntax
while (condition is true) {
code to be executed;
}
Example 1
<?php
$x = 1;
while($x<=10) {
echo $x."<br/>";
$x++;
}
?>
Example 2
<?php
$x = 1;
while($x<=10) {
echo "Number $x <br/>";
$x++;
}
?>
Que1 → Kjy; 10 rJu vz;fis ntspaPL nra;tjw;fhd php FwpKiwapid vOJf.
Que2 → xd;W Kjy; 10 ,w;fpilg;gl;l xw;iw vz;fis ntspaPL nra;tjw;fhd php FwpKiwapid vOJf.
Prepared by: [Link] BICT(UG) - UOC | Tel (24/7): 0766776519, 0752721667, Email: Sathithraj9999@[Link]
Web Development PHP Tutorial | [Link] | 2021 Batch
PHP do…while loop
Syntax
do{
// Code to be executed
}
while(condition);
Example 1
<?php
$i = 0;
do{
$i++;
echo "$i <br>";
}
while($i<=5);
?>
Example 2
<?php
$i = 0;
do{
$i++;
echo "$i <br>";
++$i;
}
while($i<=5);
?>
PHP for loops
Syntax
for (init counter; test counter; increment counter) {
code to be executed for each iteration;
}
Example 1
<?php
for ($x=0; $x<=5; $x++) {
echo "$x <br>";
}
?>
Example 2
<?php
for ($x=10; $x>=1; $x--) {
echo "$x <br>";
}
?>
Prepared by: [Link] BICT(UG) - UOC | Tel (24/7): 0766776519, 0752721667, Email: Sathithraj9999@[Link]
Web Development PHP Tutorial | [Link] | 2021 Batch
Example 3
<?php
for ($x=1; $x<=10; $x+=2) {
echo "$x*$x = ".$x*$x."<br>";
}
?>
PHP foreach loops
Syntax
foreach ($array as $value) {
code to be executed;
}
For every loop iteration, the value of the current array element is assigned to $value and the array pointer is
moved by one, until it reaches the last array element.
Example 1 (Indexed Array)
<?php
$subject = array('ETech','BTech','ICT','SFT');
foreach($subject as $sub){
echo "Subject $sub <br>";
}
?>
Example 2 (Associative Array)
<?php
$x = array('Brand'=>'Acer', 'CPU'=>'Core i7', 'RAM'=>'16GB');
foreach($x as $a=>$b){
echo "$a: $b<br>";
}
?>
Break and Continue
Example (Break in for loop)
<?php
for ($x = 0; $x <=10; $x++) {
if ($x == 5) {
break;
}
echo "$x <br>";
}
?>
Prepared by: [Link] BICT(UG) - UOC | Tel (24/7): 0766776519, 0752721667, Email: Sathithraj9999@[Link]
Web Development PHP Tutorial | [Link] | 2021 Batch
Example (Break in while loop)
<?php
$x = 0;
while($x < 5) {
if ($x == 3) {
break;
}
echo "$x <br>";
$x++;
}
?>
Example (Continue in for loop)
<?php
for ($x = 1; $x <=5; $x++) {
if ($x == 3) {
continue;
}
echo "$x <br>";
}
?>
Example (Continue in while loop)
<?php
$x = 0;
while($x < 5) {
if ($x == 2) {
$x++;
continue;
}
echo "$x <br>";
$x++;
}
?>
Que →
<?php
$x = 0;
while($x < 5) {
if ($x != 2) {
$x++;
continue;
}
echo "$x <br>";
$x++;
}
?>
Prepared by: [Link] BICT(UG) - UOC | Tel (24/7): 0766776519, 0752721667, Email: Sathithraj9999@[Link]
Web Development PHP Tutorial | [Link] | 2021 Batch
PHP Functions
• PHP ,d; cz;ikahd rf;jpahdJ mjd; nray;ghLfspy; ,Ue;J fpilf;fpd;wJ.
• PHP ,y; 1000 ,w;Fk; Nkw;gl;l built-in functions fhzg;gLfpd;wd. Nkyjpfkhf gaduhy; tiuaiw
nra;ag;gLk; nray;ghLfs; $l (User defined functions) fhzg;gLfpd;wd.
PHP built-in functions → PHP has over 1000 built-in functions that can be called directly, from within a script, to
perform a specific task.
PHP User defined functions → gaduhy; tiuaiw nra;ag;gLk; nray;ghLfs;.
• User defined function ,d; gz;Gfs;
1. xU nray;ghnlhd;wpy; kPz;Lk; kPz;Lk; gad;gLj;jyhk;.
2. tiyg;gf;fk; Vw;wg;gLk; NghJ jhdhf ,af;fkilahJ.
3. gaduhy; miof;fg;gLfpd;w Ntisapy; khj;jpuk; ,af;fg;gLk;.
User defined function
Syntax
function functionName() {
code to be executed;
}
A function name must start with a letter or an underscore. Function names are NOT case-sensitive.
Example 1
<?php
function printMsg() {
echo "PHP";
}
printMsg(); // call the function
?>
Example 2
<?php
function calc(){
for ($i=1; $i<=5; $i++){
echo "$i x 2 = " . 2*$i."<br>";
}
}
calc();
?>
Functions with Argument
Example 1
<?php
function fun1($age) {
echo "Age $age";
}
fun1(22);
?>
Prepared by: [Link] BICT(UG) - UOC | Tel (24/7): 0766776519, 0752721667, Email: Sathithraj9999@[Link]
Web Development PHP Tutorial | [Link] | 2021 Batch
Example 2
<?php
function printSub($sub,$batch) {
echo "$sub Subject for $batch Students <br>";
}
printSub('ICT',2021);
printSub('SFT',2021);
printSub('BST',2021);
printSub('ET',2021);
?>
Example 3
<?php
// Defining function
function printSub($sub, $name='Raj'){
echo "$sub with $name <br>";
}
printSub('Python');
printSub('PHP','Sathith');
?>
Example 4
<?php
// Defining function
function getSum(){
for($i=1; $i<5; $i++)
{
return $i+$i;
}
}
echo getSum();
?>
Example 5
<?php
function getSum($no,$range){
for($i=$no; $i<$range; $i++)
{
return $i+$i;
}
}
echo getSum(2,5);
echo getSum(5,8);
echo getSum(5,10);
?>
Prepared by: [Link] BICT(UG) - UOC | Tel (24/7): 0766776519, 0752721667, Email: Sathithraj9999@[Link]
Web Development PHP Tutorial | [Link] | 2021 Batch
Example 6
<?php
function fun1(){
$no = "Function 1";
echo $no;
}
fun1();
echo $no;
?>
Example 7
<?php
$no = "Global";
function fun1(){
$no = "Function 1";
echo $no;
}
fun1();
echo $no;
?>
Note →
PHP Arrays
1. Indexed Array
2. Associative Array
3. Multidimensional Array
Associative Array
<?php
$name = array('Raj' => 'Python', 'Sathith' => 'PHP', 'Gunathilaka'=>'HTML');
echo "Raj will start ".$name['Raj'].' tutorial class';
?>
or
<?php
$name['Raj']="Python";
$name['Sathith']='PHP';
$name['Gunathilaka']='HTML';
echo "Raj will start ".$name['Raj'].' tutorial class';
?>
Next → Sorting Array, PHP Form Handling
Prepared by: [Link] BICT(UG) - UOC | Tel (24/7): 0766776519, 0752721667, Email: Sathithraj9999@[Link]