PHP Notes BCA NEP Unit-2
PHP Notes BCA NEP Unit-2
Syntax
if (condition)
{
// code to be executed if condition is true;
}
Example:
PHP - 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
PHP - The if...elseif...else Statement
● The if...elseif...else statement executes different codes for more than
two conditions.
Syntax
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
// code to be executed if first condition is false and this condition is
true;
} else {
// code to be executed if all conditions are false;
}
Example
Nested If
● You can have if statements inside if statements, this is called nested if
statements.
Example:
● An if inside an if:
The PHP switch Statement
● 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
}
Example
The break Keyword
● When PHP reaches a break keyword, it breaks out of the switch block.
● This will stop the execution of more code, and no more cases are tested.
● The last block does not need a break, the block breaks (ends) there
anyway.
Example
If no cases get a match, the default block is executed:
The ? Operator:(Ternary Operator)
● The ternary operator in php is a concise way to write conditional
statements that improve code readability and effectiveness.
● It is an alternative to the “if-else” conditional statement of php.
● The ternary operator is a shortcut operator used for shortening the
conditional statements.
● It decreases
● the length of the code performing conditional operations.
● Syntax:
Condition ? statement1 statement2;
● Example:
<?php
$age=17;
$type=$age>=16? "Adult" : "child";
echo $type;
?>
PHP Loops
● Often 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.
Example:
Print $i as long as $i is less than 6:
Note: remember to increment $i, or else the loop will continue forever.
● 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
Stop the loop when $i is 3:
The continue Statement
● With the continue statement we can stop the current iteration, and
continue with the next:
Example
Stop, and jump to the next iteration if $i is 3:
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:
The break Statement
● With the break statement we can stop the loop even if the condition is
still true:
Example
● Stop the loop when $i is 3:
The continue Statement
● With the continue statement we can stop the current iteration, and
continue with the next:
Example
● Stop, and jump to the next iteration if $i is 3:
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
}
Example
Example
● Example:
Indexed array:
<?php
$student=array(20,30,40);
print_r($student);
?>
Associative array:
<?php
$f1=array("name"=>"abc","age"=>80);
print_r($f1);
?>
Indexed array:
<?php
$a1=[10,30,20,40];
print_r($a1);
?>
Associative array:
<?php
$f1=["name"=>"abc","age"=>80];
print_r($f1);
?>
<?php
$f1=range(1,5);
print_r($f1);
?>
<?php
$value=array_fill(0,3,"Default");
print_r($value);
?>
Accessing array elements in php:
● To access an array item, you can refer to the index number for indexed
arrays, and the key name for associative arrays.
● Example:
Indexed array:
Associative array:
● Associative array: Use the key name when the function is an item in an
associative array.
● Execute function by referring to the key name:
Loop Through an Associative Array:
● Associative array: To loop through and print all the values of an
associative array, you can use a foreach loop, like this:
Change Value
● To change the value of an array item, use the index number:
Loop Through an Indexed Array
● To loop through and print all the values of an indexed array, you
could use a foreach loop, like this:
Change Value
● To change the value of an array item, use the key name:
Loop Through an Associative Array
● To loop through and print all the values of an associative array,
you could use a foreach loop, like this:
<?php
$matrix=array(array(1,2,3),
array(4,5,6),
array(7,8,9));
echo $matrix[0][1];
echo "<br>";
echo $matrix[0][0];
echo "<br>";
?>
● We can access the array elements by using nested for loop to print row
and column like matrix format.
● Example: Indexed Array:
<?php
$matrix=array(array(1,2,3),
array(4,5,6),
array(7,8,9));
for($i=0;$i<3;$i++)
{
for($j=0;$j<3;$j++)
{
echo $matrix[$i][$j]." ";
}
echo "<br>";
}
?>
Associative array:
<?php
$students=array(
array("name"=>"john","age"=>30),
array("name"=>"alice","age"=>20),
array("name"=>"eve","age"=>50)
);
print_r($students);
echo "<br>";
foreach($students as $row)
{
foreach($row as $key=>$value){
echo " $key:$value ";
}
echo "<br>";
}
?>
Updating the array elements:
● We can update the array elements by using the array index value.
● Example:
<?php
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
echo "before update the array elements<br>";
for($row=0;$row<3;$row++)
{
for($col=0;$col<3;$col++)
{
echo $cars[$row][$col]." ";
}
echo "<br>";
}
echo "after update the array elements<br>";
$cars[0][1]=20;
for($row=0;$row<3;$row++)
{
for($col=0;$col<3;$col++)
{
echo $cars[$row][$col]." ";
}
echo "<br>";
}
?>