First PHP Program


To test the web server, we are going to write our first PHP code or syntax, by creating a new web project in the root directory of our web server, see the step below.


Step1. Go to the following location from your computer C:\wamp\www Or click www directory from the WAMP icon on the taskbar. If you using XAMPP on Windows the root directory is C:\xampp\htdocs\, if you a linux user you physical root directory would be /home/user/lampstack/apache2/htdocs
root

Step2. Create a new folder name it test in this directory.

Step3. Open the folder and create a file inside it name index.php
Although PHP code can be integrated into HTML code, but the file must carry the .PHP file extension. This is why I insist that the file should have .php extension instead of the usual .htm or .html because it will contain the HTML as well as PHP code as we have below.

<HTML>
<HEAD>
<TITLE> Testing PHP </TITLE>
</HEAD>
<BODY>
<?php
print "Today's date is: ". date('d-m-Y');
?>
</BODY>
</HTML>

Note that the’ Y’ in date('d-m-Y')is in capital letter, this will make the year to display in full, as 2022 rather than 22



So open the index.php file either in your text editor such as notepad, notepad++, VS Code, Sublime Text or anyone you have installed and enter code above into it. Note: I bolded the PHP line so that you can notice it, also the screenshot above is a Dreamweaver view of the code and it indicate PHP codes in different red-dominated colours, which can be very helpful.

To see the result of this PHP code, start your browser and type http://localhost/test at the address bar (the server will map to the index or homepage automatically), or better still point to the page directly by typing http://localhost/test/index.php See the result.



Server-side difference
Recall in the introductory chapters of the book, that in server-side architecture that the server-side code; in this example PHP, is process on the server and the server return the result in pure HTML in response to the user’s request. To understand what I mean try to view the source of this page from the browser see the result.  

In internet explorer click from the menu bar View > Source See the screenshot below.



Note: I press ALT on keyboard to display my menu bar; you may not have to do this if the version of your browser (Internet explorer) down not shows the menu bar by default.

Now see the result of this below, and you will notice that it does not contain any raw server-side code (PHP) but the process result in text or html.



This means that you cannot double click a PHP page to preview it on the browser as you’ve been doing with HTML, you must request for it just like you do with every other website online, such as http://www.google.com the only difference between this and the later is that, ours is on a local server, while the later is on a remote server accessible via the internet.

This also have a security advantage, because your sensible programs or code is hiding from every visitor or user of your website or web application.

Now we can dive deeper into programming with PHP

 

Next »


Home    |    Learn Programming   |    Learning Management System   |    Computer Based Testing System    |    Contact Us