First PHP Program

Subject: Web development using PHP and MySQL
To program our PHP web application, we need to first create a new web project in the root directory of our web server.

Create a folder for our website in the root
Step1.
For windows users, go to the XAMPP server root directory on your computer C:\xampp\htdocs\
if you are a linux user, your physical root directory would be /home/user/lampstack/apache2/htdocs

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. The file should have .php extension instead of the usual .htm or .html because it may contain HTML as well as PHP code. Copy the code in example 1 to the index.php file

Example 1

<!DOCTYPE html>
<html lang="en">
<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.

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

PHP Code
PHP code is often written or mixed with HTML and other frontend technologies, but it must be written within the PHP start and end directive
 <?php code here ?>
as seen in example 1.

Each line of PHP code is terminated by semicolon ;

PHP uses curly-bracket to indicate the start and end of a control flow such as loop (for loop and while loop) and decision (if, else if and else).

The file extension for PHP files is .php, although the file may contain HTML tags and codes of other frontend technologies.

Server-side processing
Because PHP is a Server-side or Back-End programming language, this means that it is processed by the PHP Interpreter in the server and the server return the result in plain HTML to the client (web browser). To understand this, try to view the source of this page from the browser see the result.  

In click from the menu bar View > Source OR right-click the previewed page on the browser and click "Source".

You will be surprised to see that the result does not have any PHP code but the process result in text or html.

This means that you cannot double click a PHP page to preview it on the browser unlike HTML,CSS or JS (Server-side/back-end vs. Client side/front-end), you must request for it and the server will process and respond just like you do with every other website online, such as https://www.tealearn.org. The only difference between this and the later is that, our example here 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.

Run PHP code by requesting via the browser




View source on the browser





By: Benjamin Onuorah

Comments

No Comment yet!

Login to comment or ask question on this topic


Previous Topic Next Topic

Supported by