Saturday, 6 April 2024

The anatomy of a PHP Page(or) Embedding PHP Code in Your Web Pages

A PHP script is a file (ending with a .php extension) consisting of text, HTML, and PHP instructions interspersed throughout the file. 

The PHP instructions are contained within two HTML style tags; is the closing tag. Everything between these two tags is interpreted by the PHP module (also called interpreter) and converted to regular text and HTML before being sent back to the requesting browser.

There are four delimitation variants:

  1. Default Syntax 
  2. Short-Tags 
  3. Script 
  4. ASP Style 
  5. Short-circuit syntax

Embedding Multiple Code Blocks :

Commenting Your Code: 

  1. Single-Line C++ Syntax
  2. Shell Syntax 
  3. Multiple-Line C Syntax (/*………*/)

Default Syntax 
The default delimiter syntax opens with 
<?php 
echo “ First PHP program”;  
?>

Output: First PHP program

Short-Tags:
<?
print "This is another PHP example.";
?>

Output: This is another PHP example.

Script
<script language="php">
print "This is another PHP example.";
</script>

ASP Style
<%
print "This is another PHP example.";
%> 

Short-circuit syntax:  
<?= 
"This is another PHP example.";
?>  

This is functionally equivalent to both of the following variations: 
<? 
echo "This is another PHP example.";     
?> 
<?php 
echo "This is another PHP example.";    
?>


Embedding Multiple Code Blocks:
<html>
<head>
<title><?php echo "Welcome to my web site";?></title>
</head>
<body>
<?php
$date = " April 06, 2024";
?>
<p>Today's date is <?=$date;?></p>
</body>
</html>

Commenting Your Code

1. Single-Line C++ Syntax
PHP supports C++
single-line comment syntax, which is prefaced with a double slash (//), like this:
<?php
// Title: My first PHP script
// Author: Narendra
echo "This is a PHP program.";
?>

2.Shell Syntax
PHP also supports an alternative to the C++ style single-line syntax, known as shell syntax, which is prefaced with a hash mark (#). 
<?php
# Title: My first PHP script
# Author: Narendra
echo "This is a PHP program.";
?>

3. Multiple-Line C Syntax
PHP also offers a multiple-line variant that can open and close the comment on different lines. 
Here’s an example:
<?php
/*
Title: My first PHP script
Author: Narendra
*/
echo "This is a PHP program.";
?>

Outputting Data to the Browser
The simplest of dynamic web sites will output data to the browser, and PHP offers several methods for doing so.
1. print() Statement 
2. echo() Statement
3. printf() Statement 
4. sprintf() Statement 

print() Statement
The print() statement outputs data passed to it . Its prototype looks like this:  int print(argument)
Example
<?php     
print("<b>welcome to PHP</b>");
?>

echo() Statement
<?php
echo " welcome to PHP ";
?>

printf() Statement 
The printf() statement is ideal when you want to output a blend of static text and dynamic information stored within one or several variables.
Example
<?php
printf("Bar inventory: %d bottles of tonic water.", 100);   ?>
Output:
Bar inventory: 100 bottles of tonic water.

sprintf() Statement 
The sprintf() statement is functionally identical to printf() except that the output is assigned to a string rather than rendered to the browser.
Example
<?php
$cost = sprintf("$%.2f", 43.2);
echo $cost;
?> 

Output
$43.20



HTML5 Input Types

 HTML5 added several new input types:

<input type="color"/>

<input type="date"/>

<input type="datetime-local"/>

<input type="email"/>

<input type="month"/>

<input type="number"/>

<input type="range"/>

<input type="search"/>

<input type="tel"/>

<input type="time"/>

<input type="url"/>

<input type="week"/>


New Tags in HTML5

 <audio>

<bdi> Bi-Directional Isolation

<canvas> 

<datalist> 

<details> 

<dialog>

<embed>

<figcaption> tag defines a caption for a <figure> element

<figure> 

<footer> 

<header> 

<mark> 

<meter> 

<nav> tag defines a set of navigation links

<output>

<progress> 

<source> 

<video>  

<wbr> (Word Break Opportunity) 

<article> 

<aside> 

<main> 

<menuitem> 

<picture> 

<ruby> tag specifies a ruby annotation.

<rp> 

<rt>

<section> 

<summary> 

<time> 

<track> tag specifies text tracks for media elements <audio> and <video>

 

Multi page form in PHP with Example

A multi page form in PHP are used to retain values of a form and can transfer them from one page to another . 

multipage.php

<html>

<body>

<div style="width: 500px; text-align: left;">

<form action="multipage2.php" method="post">

<p>Page 1 Data Collection:</p>

<input type="hidden" name="submitted" value="yes" />

Your Name: <input type="text" name="yourname" maxlength="150" />

 <br /><br />

<input type="submit" value="Submit" style="margin-top: 10px;" />

</form>

</div>

</body>

</html>

OUTPUT







 multipage2.php

<html><body>

<div style="width: 500px; text-align: left;">

<form action="multipage3.php" method="post">

<p>Page 2 Data Collection:</p>

Selection:

<select name="yourselection">

<option value="nogo">make a selection...</option>

<option value="Choice1">Choice1</option>

<option value="Choice2">Choice2</option>

<option value="Choice3">Choice3</option>

</select><br /><br />

<input type="hidden" name="yourname" value="<?php echo $_POST['yourname']; ?>" />

<input type="submit" value="Submit" style="margin-top: 10px;" />

</form>

</div>

</body></html>

OUTPUT






multipage3.php

<html>

<body>

<div style="width: 500px; text-align: left;">

<form action="multipage4.php" method="post">

<p>Page 3 Data Collection:</p>

Your Email: <input type="text" name="youremail" maxlength="150" /><br />

<input type="hidden" name="yourname" value="<?php echo $_POST['yourname']; ?>" />

<input type="hidden" name="yourselection" value="<?php echo $_POST['yourselection']; ?>" />

<input type="submit" value="Submit" style="margin-top: 10px;" />

</form>

</div>

</body>

</html>

OUTPUT






multipage4.php


<html>

<head><title>Page 4</title></head>

<body><div style="width: 500px; text-align: left;">

<?php

//Display the results.

echo "Your Name: " . $_POST['yourname'] . "<br />";

echo "Your Selection: " . $_POST['yourselection'] . "<br />";

echo "Your Email: " . $_POST['youremail'] . "<br />";

?>

<a href="multipage.php">Try Again</a>

</div></body></html>

OUTPUT