PHP

Conditional Statement

conditional statements in PHP

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

  1. Switch statement
  2. IF statement
    1. Simple If
    2. IF – else
    3. IF – elseIF – else
    4. 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

  1. while loop
  2. do while loop
  3. for loop
  4. 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

  1. Write a program to to display first 10 even numbers ?
  2. Write a program to to display the multiplication table as shown below ?

    5 * 1 = 55 * 2 = 10

    upto

    5 * 10 = 50

  3. Write a program to to display odd numbers between x and y ?
  4. Write a program to to display to display reverse of a number ?

    ex: if we taken input “1234” the out has to be “4321”

  5. 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”

You Might Also Like