0
2.5kviews
Explain Control structures in PHP

Mumbai University > Information Technology > Sem 4 > Web Programming

Marks: 10M

Year: Dec 2014

1 Answer
0
6views

These are

  • Selection Statements
  • Loop Statements

1) Selection Statements:

  • Selection Statements are used to make decisions. These are similar to control structures in C.
  • The if statement, if…else statement or if… else if statements are used in selection statements.
  • Switch Case is also used for multiple choices.

    <html>
    <head>
    <title> Selection Demo</title>
    </head>
    <body>
    <?php
    Print “<h2> Selection Statement<h2>”;
    $a=10;
        	$b=20;
    $c=30;
        	if($a>$b)
        		if($a>$c)
        			print “\ltb\gt\ltI\gta is largest number\lt/I\gt\lt/b\gt”;
        		else
        			print “\ltb\gt\ltI\gt c is the largest number\lt/I\gt\lt/b\gt”;
        		else
        		if($b>$c)
        			print “\ltb\gt\ltI\gt b is the largest number \lt/I\gt\lt/b\gt”;
        		else
        			print “\ltb\gt\ltI\gt c is the largest number\lt/I\gt\lt/b\gt”;
            ?\gt
            \lt/body\gt
            \lt/html\gt
    
    **Output:**
    
    Selection Statement
    
    C is the largest number
    
    
    2) **Loop Statements**
    
    - The loops statements consists of while, do while and for statements. Loops statements are used to perform repetitive tasks.
    
     While Example:
    
            \lt?php
            $i=1;
    print “The numbers are…”
    print “<br/>”;
    while($i\lt=5)
            {	
            print $i;
    print “<br/>
    $i++;
            }
            ?\gt
    
    **Output:**
    
    The numbers are…
    
    
    1
    
    2
    
    3
    
    4
    
    5
    
    **do ..while Example:**
    
        \lt?php
        $i=1;
    

    print “The numbers are…”; print “
    ”; dp { print $i; print “\ltbr/\gt”; $i++; }while($i\lt=5 ); ?\gt **for Example:** \lt?php print “The numbers are..”; print “\ltbr/\gt”; for($i=1;$i\ltn) { print $i; print “
    ”; $i++; } ?>

Please log in to add an answer.