SlideShare a Scribd company logo
2.10 Displaying Messages on
Web Page
• 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>";
print pow($i,3);
print "</td>";
print "</tr>";
}
print "</table>";
?>
</center>
</body></html>
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
h)Construct a PHP script to compute the squareRoot,
Square, Cube and Quad of 10 numbers.
html>
<head>
<title > SQUARE CUBE QUAD
DEMO</title>
</head>
<body>
<?php
print "<b>Table<br/>";
print "<table border='l'>";
print
"<tr><th>num</th><th>Sqr</th>
<th>Cube</th><th>
Quad</th></tr>";
for($count=l;$count< = 10;$count+
+)
{$sq=$count*$count;
$cube=$count*$count*$count;
$quad
=$count*$count*$count*$count
;
print
"<tr><td>$count</td><td>$sq</
td><td>$cube</td><td>$quad</
td></tr>";
}print "</table>";
?></body></html>
Output
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>

More Related Content

PDF
How to run PHP code in XAMPP.docx (1).pdf
PPTX
php programming.pptx
PPTX
Php.ppt
PPT
Php introduction
PPT
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PPT
introduction to php web programming 2024.ppt
PPT
PPT
Class 2 - Introduction to PHP
How to run PHP code in XAMPP.docx (1).pdf
php programming.pptx
Php.ppt
Php introduction
PHP - DataType,Variable,Constant,Operators,Array,Include and require
introduction to php web programming 2024.ppt
Class 2 - Introduction to PHP

Similar to web essentials - simple message flow and loo.pptx (20)

PPTX
Unit 5-PHP Declaring variables, data types, array, string, operators, Expres...
PPT
Php mysql
PPT
Php Lecture Notes
PPTX
Php intro by sami kz
PPTX
Chapter 4 server side Php Haypertext P.pptx
PPT
PPTX
PHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
PDF
Wt unit 4 server side technology-2
ODP
PDF
Winter%200405%20-%20Beginning%20PHP
PDF
Winter%200405%20-%20Beginning%20PHP
PPT
Prersentation
PPT
CPAP.com Introduction To Coding: Part 2
PPT
slidesharenew1
PPT
My cool new Slideshow!
PPTX
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
PDF
Php Tutorials for Beginners
PDF
php AND MYSQL _ppt.pdf
PPT
PHP-01-Overview.pptfreeforeveryonecomenow
PPT
PHP - Introduction to PHP Fundamentals
Unit 5-PHP Declaring variables, data types, array, string, operators, Expres...
Php mysql
Php Lecture Notes
Php intro by sami kz
Chapter 4 server side Php Haypertext P.pptx
PHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
Wt unit 4 server side technology-2
Winter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHP
Prersentation
CPAP.com Introduction To Coding: Part 2
slidesharenew1
My cool new Slideshow!
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php Tutorials for Beginners
php AND MYSQL _ppt.pdf
PHP-01-Overview.pptfreeforeveryonecomenow
PHP - Introduction to PHP Fundamentals
Ad

More from Jayaprasanna4 (20)

PDF
web programming javascriptconditionalstatements.pdf
PDF
hyper text markup language ppt-100605011058-phpapp02.pdf
PPTX
web essentials - Working principle of a Website.pptx
PPTX
software project in MONTE CARLO SIMULATION.pptx
PPT
Cost effort in softwrae project management.ppt
PDF
software project management cocomomodel.pdf
PPT
software project management Activity planning.ppt
PDF
software project management montecarloscheduleanalysis.pdf
PPT
casestudy on distributionnetworkformichaelshardwaregroupgate.ppt
PPT
ethical hacking-mobile hacking methods.ppt
PPT
ethical hacking in wireless-hacking1.ppt
PDF
Human computer Interaction ch1-the human.pdf
PPT
computer Networks Error Detection and Correction.ppt
PPT
HUman computer Interaction Socio-organizational Issues.ppt
PPT
human computer Interaction cognitive models.ppt
PPT
World wide web and Hyper Text Markup Language
PPT
CI-Monte-Carlo.ppt
PPT
Activity planning.ppt
PPT
Cost effort.ppt
PPT
Activity planning.ppt
web programming javascriptconditionalstatements.pdf
hyper text markup language ppt-100605011058-phpapp02.pdf
web essentials - Working principle of a Website.pptx
software project in MONTE CARLO SIMULATION.pptx
Cost effort in softwrae project management.ppt
software project management cocomomodel.pdf
software project management Activity planning.ppt
software project management montecarloscheduleanalysis.pdf
casestudy on distributionnetworkformichaelshardwaregroupgate.ppt
ethical hacking-mobile hacking methods.ppt
ethical hacking in wireless-hacking1.ppt
Human computer Interaction ch1-the human.pdf
computer Networks Error Detection and Correction.ppt
HUman computer Interaction Socio-organizational Issues.ppt
human computer Interaction cognitive models.ppt
World wide web and Hyper Text Markup Language
CI-Monte-Carlo.ppt
Activity planning.ppt
Cost effort.ppt
Activity planning.ppt
Ad

Recently uploaded (20)

PDF
Abrasive, erosive and cavitation wear.pdf
PPTX
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
PPTX
"Array and Linked List in Data Structures with Types, Operations, Implementat...
PPTX
Feature types and data preprocessing steps
PDF
COURSE DESCRIPTOR OF SURVEYING R24 SYLLABUS
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PDF
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
PPTX
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
PDF
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
PPTX
Nature of X-rays, X- Ray Equipment, Fluoroscopy
PPTX
AUTOMOTIVE ENGINE MANAGEMENT (MECHATRONICS).pptx
PDF
distributed database system" (DDBS) is often used to refer to both the distri...
PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PDF
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
PDF
Categorization of Factors Affecting Classification Algorithms Selection
PDF
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
PDF
UNIT no 1 INTRODUCTION TO DBMS NOTES.pdf
PDF
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
PPTX
introduction to high performance computing
PPTX
Fundamentals of Mechanical Engineering.pptx
Abrasive, erosive and cavitation wear.pdf
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
"Array and Linked List in Data Structures with Types, Operations, Implementat...
Feature types and data preprocessing steps
COURSE DESCRIPTOR OF SURVEYING R24 SYLLABUS
III.4.1.2_The_Space_Environment.p pdffdf
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
Nature of X-rays, X- Ray Equipment, Fluoroscopy
AUTOMOTIVE ENGINE MANAGEMENT (MECHATRONICS).pptx
distributed database system" (DDBS) is often used to refer to both the distri...
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
Categorization of Factors Affecting Classification Algorithms Selection
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
UNIT no 1 INTRODUCTION TO DBMS NOTES.pdf
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
introduction to high performance computing
Fundamentals of Mechanical Engineering.pptx

web essentials - simple message flow and loo.pptx

  • 2. • 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);
  • 3. 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>
  • 4. 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>
  • 5. 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.
  • 6. <?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
  • 7. 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
  • 8. 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/>"; } ?>
  • 9. 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>";
  • 10. print pow($i,3); print "</td>"; print "</tr>"; } print "</table>"; ?> </center> </body></html>
  • 11. 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
  • 12. 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.
  • 13. 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 >
  • 14. 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
  • 15. 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
  • 16. 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
  • 17. h)Construct a PHP script to compute the squareRoot, Square, Cube and Quad of 10 numbers. html> <head> <title > SQUARE CUBE QUAD DEMO</title> </head> <body> <?php print "<b>Table<br/>"; print "<table border='l'>"; print "<tr><th>num</th><th>Sqr</th> <th>Cube</th><th> Quad</th></tr>"; for($count=l;$count< = 10;$count+ +) {$sq=$count*$count; $cube=$count*$count*$count; $quad =$count*$count*$count*$count ; print "<tr><td>$count</td><td>$sq</ td><td>$cube</td><td>$quad</ td></tr>"; }print "</table>"; ?></body></html>
  • 19. 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!!!
  • 20. 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>