0% found this document useful (0 votes)
45 views4 pages

DB Lab 9

This document provides an overview of key concepts in PHP including basic syntax, conditional statements, arrays, and loops. It introduces PHP syntax like tags, variables, data types, echo statements, comments, and concatenation. Conditional statements like if/else are demonstrated. Indexed and associative arrays are shown along with sort, rsort, and count functions. Finally, while, for, and foreach loops are explained and examples are provided.

Uploaded by

Rana Awais
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)
45 views4 pages

DB Lab 9

This document provides an overview of key concepts in PHP including basic syntax, conditional statements, arrays, and loops. It introduces PHP syntax like tags, variables, data types, echo statements, comments, and concatenation. Conditional statements like if/else are demonstrated. Indexed and associative arrays are shown along with sort, rsort, and count functions. Finally, while, for, and foreach loops are explained and examples are provided.

Uploaded by

Rana Awais
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
You are on page 1/ 4

Lab 9

Learning objectives:
 Learn to use basic PHP syntax.
 Learn to use PHP conditional statements.
 Learn to use PHP arrays.
 Learn to use PHP loops.

9.1 Basic PHP Syntax


Default extension of PHP file is “.php”.
Save a text file as “Lab9.php” in “../xampp/htdocs” folder.
To run the script type “localhost/Lab9.php” in the browser’s address bar.
Use php start “<?php” and end “?>” tags any where in the document.
PHP function of “echo” is used to output text.
PHP statements terminates with a semicolon.

Show the “Hello World!” output using PHP echo function.

<?php
Echo "Hello World!";
?>

Comments in PHP
Single-line and multi-line comments are same as in C++.

<?php
// single-line comment
echo "Hello World!";
/*
Multi-line comment
*/
?>

PHP Variables
A variable in PHP starts with $ sign and data types are determined at run time.

<?php
$a;
$a = "Hello World!";
echo $a;

$a = 5;
echo $a;

$b = 20;
echo $a + $b;
?>
Strings are concatenated using . operator.
<?php
$a = 5;
$b = 20;
$c = $a+$b;
echo "Sum of a and b is " . $c;
?>

Variable names can be used inside the string.


<?php
$a = 5;
$b = 20;
$c = $a+$b;
echo "Sum of $a and $b is " . $c;
?>

The <br> tag is used to insert a single line break (new line).

<?php
$a = 5;
$b = 20;
$c = $a+$b;
echo "Sum of $a <br> and $b <br> is " . $c;
?>

9.2 PHP Conditional Statements


In PHP we have the following conditional 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

Syntax is same as in C++ and for reference only we will use an example of if .. else block.

<?php
$a = 5;
$b = 20;
if ($a < $b)
{echo "a is smaller";}
else
{echo "b is smaller";}

?>
9.3 PHP Indexed Arrays
An array stores multiple values under a single variable name. In PHP array() function is used to create
an array.

Indexed is assigned manually

<?php
$arr[0] = 1;
$arr[1] = 2;
$arr[2] = 3;
$arr[3] = 4;
$arr[4] = 5;
echo $arr[4];
?>

Indexed is assigned automatically

<?php
$arr = array (1, 2, 3, 4, 5);
echo $arr[0] . $arr[1] . $arr[2] . $arr[3] . $arr[4];
?>

PHP sort() function is used to sort an array in ascending order.

<?php
$arr = array (6, 2, 5, 11, 0);
sort($arr);
echo $arr[0]."<br>".$arr[1]."<br>".$arr[2]."<br>".$arr[3]
."<br>".$arr[4];
?>

PHP rsort() function is used to sort an array in descending order.

<?php
$fruits = array ('apple', 'orange', 'coconut', 'grapes');
rsort($fruits);
echo $fruits[0].", ".$fruits[1].", ".$fruits[2].",
".$fruits[3];
?>

PHP count() function is used to return the number of elements in an array.

<?php
$fruits = array ('apple', 'orange', 'coconut', 'grapes');
echo "The number of fruits are: " . count($fruits);
?>

9.4 PHP Loops


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

PHP while Loop

<?php
$a = 1;

while($a <= 5)
{
echo "$a <br>";
$a++;
}
?>

PHP for Loop

<?php
for ($a = 5; $a >= 1; $a--)
{
echo "$a <br>";
}
?>

PHP foreach Loop


The foreach loop works only on arrays can loop through each key/value pair in an array. The value of
the current array element is assigned to a variable and the array pointer moves one by one till the last
element of array.

<?php
$student = array("Alex", "Noah", "Cole", "Smith");

foreach ($student as $s)


{
echo "$s <br>";
}
?>

You might also like