Everybody knows how the Statement works in programing languages as PHP developed from C language lets directly go for sample programs.
We have 4 Conditional Statement in PHP they are
- Switch statement
- IF statement
- Simple If
- IF – else
- IF – elseIF – else
- Nested IF
Switch Statement:-
<?php
$X=”a”;
switch ($x)
{
Case “a”;
echo “A”;
break;
Case “B”;
echo “B”;
break;
Case “c”;
echo “C”;
break;
Default:
echo”D”;
}
?>
output will be
A
Control statements (or) Loops
- while loop
- do while loop
- for loop
- for each loop
While Loop:-
Write a program for to display the first 10 natural numbers ?
<?php
echo “The first 10 natural numbers are”.”<br>”;
$x=1;
while($x<=10)
{
Echo $x.”<br>”;
$x++;
}
?>
The first 10 natural numbers are
1
2
3
4
5
6
7
8
9
10
and now just check the below program what will be the out put.
<?php
$x=1;
while($x++<=5);
echo $x.”<br>”;
?>
predict what will be the output of the Program ????
Output will be
7
Excersises
- Write a program to to display first 10 even numbers ?
- Write a program to to display the multiplication table as shown below ?
5 * 1 = 55 * 2 = 10
upto
5 * 10 = 50
- Write a program to to display odd numbers between x and y ?
- Write a program to to display to display reverse of a number ?
ex: if we taken input “1234” the out has to be “4321”
- Write a program to to display numbers in words ?
ex: if we taken number as “1234” the out has to be “ one two three four”