PHP Basic Concepts

Introduction to PHP

HTML (HyperText Mark-up Language) is the language browsers use on client-side computers to display web pages. PHP is a Hypertext Preprocessor; a programming language on the server that creates HTML to add to the page before it is sent back to the client machine.

A PHP parser resides on a server. When the server receives a request for a web page with any PHP on it at all, the entire page is sent to the PHP parser, the PHP code is run and HTML is returned to the server. When the server is finished with its own processing, the entire HTML bundle is sent back to the requesting client computer. The client machine never sees the PHP code, only the resulting HTML. (This all happens in the blink of an eye.) This is typical of all server-side programming languages.

To tell the server there is PHP code in your program, you must use a file-naming convention of '*.php'. Without this extension, the server doesn't know to first send the page to the PHP parser.

For PHP code to work, the file must reside on a server, and that server must have a PHP parser. Most computers available to you do not have these combinations. Thus, you can not design a web page with PHP coding in it on your office computer and expect it to display, unless you first install PHP. For this course, I will assume you have PHP on your desktop, or within your LAN, or via FTP, or something. You must load all your work into a directory that will run PHP.

Open a new HTML file in your favorite HTML editor, (I use HTML-Kit), and add the code shown below in blue (hint: you can cut from below and paste in your own file):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Test Page</title> </head> <body> Today's date is: <?php echo date('j F Y'); ?> </body> </html>

Now save the file in your site folder using some name you will remember (like test.php or something similar), and open it in your browser. You should see something like this:

Today's date is: 20 April 2024

You just used the echo command and the date function. Echo tells the PHP parser to convert whatever follows into HTML and display it on the screen. The date function reads today's date and formats it in whatever way you like; in this case it is formatted to show the day of the month first, then the name of the month, then the year. The dynamic nature of this function is that whenever this page is displayed, it will show whatever the server considers to be the current date.

Note how the PHP code is identified. Anything written between <php and  ?> is interpreted as PHP code. Thus, you can add dynamic content wherever you like in your file.

A single statement of code in PHP ends with a semi-colon ( ; ). "Statements" of PHP code can be written on the same line--they must just each end in semi-colons. As in HTML, PHP ignores whitespace and allows for comments:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Test Page</title> </head> <body> Today's date is: <?php echo date('j F Y'); ?><br /><br /> The URL for this page is: <?php // This comment won't appear on the page or affect the code in any way echo 'http://'.$_SERVER["SERVER_NAME"].$_SERVER["PHP_SELF"]; ?> </body> </html>

displays:

Today's date is: 20 April 2024

The URL for this page is: http://sandersongs.com/PHPsqlCourse/PHP01.php

If you reference the footer below and to the right, you will see this code in action. It is repeated on all pages of this tutorial.


SQL Review   < <  PREVIOUS   Table of Contents NEXT  > >   Variables

Developed with HTML-Kit
Sandersongs Web Tutorials
Contact the Webmasterwith comments.
©2024, by Bill Sanders, all rights reserved.
This domain had 3,193 different visits in the last 30 days.
http://sandersongs.com/PHPsqlCourse/PHP01.php
This page was last modified on our server on 11 Jul 2021
and last refreshed on our server at 3:36 am, UTC
This file took 0.03212 seconds to process.