Unit 2-clg
Unit 2-clg
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
<?php
$x = 0;
<?php
$x = 6;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
PHP for Loop
The for loop is used when you know in advance how many times the script should run.
Syntax
for (init counter; test counter; increment counter) {
code to be executed for each iteration;
}
Parameters:
•init counter: Initialize the loop counter value
•test counter: Evaluated for each loop iteration. If it evaluates to
TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
•increment counter: Increases the loop counter value
PHP foreach Loop
The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.
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.
<!DOCTYPE html>
<html>
<body>
<?php
$colors = array("red", "green", "blue", "yellow");
</body>
</html>
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 5) {
break;
}
echo "The number is: $x <br>";
}
?>
PHP Continue
The continue statement breaks one iteration (in the loop),
if a specified condition occurs, and continues with the next iteration in the loop.
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
Create an Array in PHP
The count() function is used to return the length (the number of elements) of an array:
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
?>
PHP Indexed Arrays
There are two ways to create indexed arrays:
The index can be assigned automatically (index always starts at 0), like this:
$cars = array("Volvo", "BMW", "Toyota");
or the index can be assigned manually:
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
<?php
?>
// Second way to create an indexed array
$name_two[0] = "ZACK";
$name_two[1] = "ANTHONY";
$name_two[2] = "RAM";
$name_two[3] = "SALIM";
$name_two[4] = "RAGHAV";
?>
// Second way to create an associative array
$name_two["zack"] = "zara";
$name_two["anthony"] = "any";
$name_two["ram"] = "rani";
$name_two["salim"] = "sara";
$name_two["raghav"] = "ravina";
<?php
$numbers = array(4, 6, 2, 22, 11);
rsort($numbers);
?>
Sort Array (Ascending Order), According
to Value - asort()
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
asort($age);
?>
Sort Array (Ascending Order),
According to Key - ksort()
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
ksort($age);
?>
exit( ) Function
if($a==$b)
{ //terminating script with a message using exit()
exit('variables are equal');
}
else
{
//terminating script with a message using exit()
exit('variables are not equal');
}
?>
die() Function:
In PHP, die() is the same as exit(). A program’s result will be an empty screen.
Use die() when there is an error and have to stop the execution.
Syntax:
Die(message);
Or
Die();
Return keyword
The return keyword ends a function and, optionally,
uses the result of an expression as the return value of the function.
If return is used outside of a function,
it stops PHP code in the file from running. If the file was included using include,
include_once, require or require_once,
the result of the expression is used as the return value of the include statements.
<?php
function add($x) {
return $x + 1;
}