The PHP is a server side scripting language and is used to submit the web page to the client browser. Hence it is a standard method of embedding the PHP code within the XHTML document. That means we can use the XHTML tags in PHP while displaying the output. The print function is used to create simple unformatted output. For example: The string can be displayed as follows print "I am proud of my <b>country</b>" The numeric value can also be displayed using the print. For example -print(100); It will display the output as 100. PHP also makes use of the printf function used in C. For example printf ("The student %d has'%f marks".$roll_no.$marks );
Following is a simple PHP document which makes use of the statements for displaying the output. <html> <head> <title> Output Demo</title> </head> <body> <? php print "<h2>Welcome to my Website </h2>"; print "<hr/>“; $ roll_no =l; $name==”AAA”; printf ("<b>The roll number: %d</b>",$ roll_no ); print < br /><b>"; printf ("The name: % s",$name ); print "</b>"; ?></body></html>
2.11 Flow Control and Loop 2.11.1 if Statements The if statement, the if ... else statement or if ... elseif statements are used as selection statements. This selection is based on some condition. output: C is the largest number <html> <head> <tit1e>Selection Demo</title> </head> <body> <? php print "<h2>Selection Statement </h2>"; $a=10; $b=20; $c=30; if($a>$b) if($a>$c) print "<b><I>a is the largest number </I></b>"; else print "<b><I> c is the largest number</I></b>"; else if($b>$c) print "<b><I> b is the largest number</I></b>"; else print "<b><I>c is the largest number</I></b>"; ?></body></html>
2.11.2 Switch Statement Similar to if statement the switch statement can also be used for selection. Following is a simple PHP script for demonstrating switch statement.
<? php $today = getdate ( ); Switch($today['weekday'] ) { case "Monday": print "Today is Monday"; break; case "Tuesday":print "Today is Tuesday"; break; case "Wednesday":print "Today is Wednesday"; break; case "Thursday":print "Today is Thursday"; break; case "Friday":print "Today is Friday"; break; case "Saturday":print "Today is Saturday"; . break; case "Sunday":print "Today is Sunday"; break; default: print "Invalid input"; }?> output : today is Friday
2.11.3 Loop Statements The while, for and do-while statements of PHP are similar to JavaScript, Following is a simple PHP script which displays the first 10 number <? php $ i =l; print "The numbers are ..."; print "< br />"; while($ i < = 10) { print $ i ; print "< br />"; $ i ++; } ?> Output: the numbers are 1 2 3 4 5 6 7 8 9 10
do ... while <? php $ i =1; print "The numbers are .,.": print "< br />"; , do { print “$ i ”; print "< br />"; $ i ++; } while($ i <=10); ?> For <? php print "The numbers are .,.": print "< br />"; for($ i = 1;$ i <10;i++) { print $ i ; print "< br />"; } ?>
2.11.4 Examples based on Control Statement a)Write a PHP script to display the squares and cubes of 1to 10 numbers. Sol. : <html> <head> <title> Sqaure and Cube Table </title> </head> <body> <center> <? php print "<table border =1>"; print "< tr >"; print "< th >Number</ th >"; print "< th >Square</ th >"; print ".< th >Cube</ th >"; print "</ tr >"; for($ i = 1;$ i < = 10;$ i + +) { print "< tr >"; print "<td>$ i "; print "</td>"; print "<td>"; print $ i *$ i ; print "</td>"; print "<td>";
b)Write a PHP script to compute the sum and average of N numbers . <html > <head> <title> Sum and Average </title> </head> <body> <center> <? php $sum=0; for($ i = 1;$ i < = 10;$ i ++ ) { $sum+=$ i ; } $ avg =$sum/10; print ''The Sum is: $sum"; print "< br />"; print ''the Average is: $ avg "; ?.> </center></body></html> Output: the sum is : 55 The average is: 5.5
c)Write PHP programs to print whether current year is leap year or not. <html> <head> <title> Leap Year Demo</title> </head> <body> <? php $year=2016; print "< br />"; if($year%4= = 1) { printf ("Year %d is not a leap year",$year ); } else { printf (''Year %d is a leap year",$year ); } ?> </body></html> output : year 2016 is a leap year .
d)Write PHP programs to print whether given number is odd or even. <html> <head> <title>Even Odd Demo</title> </head> <body> <? php for($ i =l;$ i < = 10;$ i + +) { $num=$ i ; print "< br />"; if($num%2= = 1) { printf ("Number % dis Odd",$num ); } else { printf ("Number % dis Even",$num ); } } ?> </body> </html >
e)Write PHP script to compute the sum of positive integers upto 30 using do-while statement. <html > <head> <title>Sum of Integers</title> </head> <body> <? php $sum=0; $ i =1; do { $sum =$sum+$ i ; $ i ++; }while($ i < = 30); printf ("The sum of first 30 positive integers is % d",$sum ); ?> </body></html> output: the sum of positive integers is 465
f) Write PHP script to compute factorial of ' n'using while or for loop construct. <html> <head> <title> Factorial Program</title > </head> <body> <? php $n = 5; $factorial = 1; for ($ i =$n; $ i >=l; $ i --) { $factorial = $factorial * $ i ; } echo "Factorial of $n is $factorial"; ?> </body> </html> Output: Factorial of 5 is 120
g) Write PHP script to display Fibonacci of length 10 . <html> <head> < title> Fibonacci Series < /title > </head> <body> <? php $ i =l; $j=l; print "<b> Fibinacci Series br />": printf ("%d, % d",$i,$j ); for($count= 1;$count<9;$count+ +) { $k=$ i +$j; $ i =$j; $j=$k; printf (", % d",$k ); } ?></body></html> Output Fibinacci series 1,1,2,3,5,8,13,21,34,55
i ) With the use of PHP, switch case and if structure perform the following and print appropriate message. i ) Get today's date ii) If date is 3, it is dentist appointment. iii) If date is 10, go to conference. Iv) If date is other than 3 and 10, no events are scheduled . <! DOCTYPEhtml > <html> <body> <? php echo "Today date is " . date("d/m/y") . "< br >"; if((date("d") <3) || (date("d") > 10)). echo "No Event!!!"; else if((date(“d”)3)&&(date(“d")< 10)) echo "No Event!!!"; else { switch( date("d")) { case 3 : echo "Dentist Appointment"; break; case 10 : echo "Go to Conference"; break; } } ?></body></html> Output: Today date is 25/12/15No event!!!
J)Write a PHP code to display the following pattern 1 01 101 0101 1 0101 <html ><head> </head> <body> <? php for($ i =0;$ i <7;$ i + +) { for($j = l;$j <$ i ;$j ++) { if(($ i +$j)%2= =0) { printf ("0"); } else { printf ("1"); }} print "< br />"; }?> </body></html>