PHP programming - Introduction
Frank Yap
 
In this section we will have closer look at the HTML embedding and spend time installing XAMPP in your PC.
 
Resources

 
Lab Exercises

 
Exercise 1: Understand PHP embedding in HTML code

Understand the following PHP embedding in HTML code.
 
The following PHP code, yes it is php code in a php file, generates the output as shown below.
However we will usually call it HTML code because the main body is in HTML code.
 
<!DOCTYPE html> 
<html lang="en"> 
 
<head> 
<title>PHP Lab</title> 
</head> 
 
<body> 
<div> 
<h3>Simple HTML page</h3> 
</div> 
<div> 
<?php echo "<h2> Hello World! </h2>"; ?> 
</div> 
</body> 
 
</html>
 

Output ---
 

Simple HTML page

Hello World!

 


Since this is a PHP file the PHP parser generates the output. It outputs HTML code as written and sends it to a browser.
A browser does not display tags. So we expect the display like below.
 
PHP Lab      - displayed on window frame

Simple HTML page

<?php echo "<h2> Hello World! </h2>"; ?>
 


When generating outputs the PHP parser recognizes the tags <?php and ?>, and does not display the tags. So the display would be
 

Simple HTML page

echo "<h2> Hello World! </h2>";
 


The PHP does not display PHP statements such as
echo " ";
 
but displays the outputs of echo or print, and thus the remaining displayed elements are
 

Simple HTML page

<h2> Hello World! </h2>
 
 


which results in display
 

Simple HTML page

Hello World!


 
The easiest way to see the HTML code embedded by PHP is ignoring every PHP statements except the final outputs from echo or print statements of PHP.
 

Exercise 2: Another example of PHP embedding in HTML code

Understand the following PHP embedding in HTML code.
 

The following code generates the output as shown below.
The arguments of the date() function are
    F:month, j:date in month, S:"st" for 1st,"nd" for 2nd,"rd" for 3rd,"th" for 4th,etc. Y:year.
 
<div> 
<h3>Simple HTML page</h3> 
</div> 
<div> 
<?php echo "<h2> Hello World! </h2>"; ?> 
<?php date_default_timezone_set("America/New_York"); ?> 
<p>I started to learn PHP on <b><?php echo date("F jS, Y"); ?></b>.</p>  
</div>
 

Output ---
 

Simple HTML page

Hello World!

I started to learn PHP on November 18th, 2017.

 

Examine the code and understand PHP output could be embedded anywhere in the document.
 
 
Homework Exercise

 
1. Install XAMPP in your PC.
    Visit http://www.apachefriends.org/en/xampp.html and follow the download and installation instructions for your PC platform.
    If the Microsoft Visual C++ 2008 redistributable package is said to be required then install it first.
2. After installation start the XAMPP and then start Apache server from XAMPP control panel.
3. Create testdir in Apache document root (xampp/htdocs in Windows) folder.
    Capture the HTML code on top of the page and store in text file test.php and store in testdir folder.
    Then use http://localhost/testdir/test.php URL in a browser to test the code in test.php.
4. Verify that the outputs are correct.
5. For XAMPP operation on Linux follow the instructions shown in XAMPP website.
    XAMPP for Linux page contains informations including on the Apache DocumentRoot directory.
6. If things work well practice more PHP embedding in the code.