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



No comments:

Post a Comment