PHP Basics


There is a good chance you already know a bit about what PHP can dothat is probably why you have picked up this book. PHP is hugely popular, and rightly so. Even if you haven't come across an existing user singing its praises, you've almost certainly used a website that runs on PHP. This lesson clarifies what PHP does, how it works, and what it is capable of.

PHP is a programming language that was designed for creating dynamic websites. It slots into your web server and processes instructions contained in a web page before that page is sent through to your web browser. Certain elements of the page can therefore be generated on-the-fly so that the page changes each time it is loaded. For instance, you can use PHP to show the current date and time at the top of each page in your site, as you'll see later in this lesson.

The name PHP is a recursive acronym that stands for PHP: Hypertext Preprocessor. It began life called PHP/FI, the "FI" part standing for Forms Interpreter. Though the name was shortened a while back, one of PHP's most powerful features is how easy it becomes to process data submitted in HTML forms. PHP can also talk to various database systems, giving you the ability to generate a web page based on a SQL query.

For example, you could enter a search keyword into a form field on a web page, query a database with this value, and produce a page of matching results. You will have seen this kind of application many times before, at virtually any online store as well as many websites that do not sell anything, such as search engines.

The PHP language is flexible and fairly forgiving, making it easy to learn even if you have not done any programming in the past. If you already know another language, you will almost certainly find similarities here. PHP looks like a cross between C, Perl, and Java, and if you are familiar with any of these, you will find that you can adapt your existing programming style to PHP with little effort.

Server-Side Scripting

The most important concept to learn when starting out with PHP is where exactly it fits into the grand scheme of things in a web environment. When you understand this, you will understand what PHP can and cannot do.

The PHP module attaches to your web server, telling it that files with a particular extension should be examined for PHP code. Any PHP code found in the page is executedwith any PHP code replaced by the output it producesbefore the web page is sent to the browser.

File Extensions The usual web server configuration is that somefile.php will be interpreted by PHP, whereas somefile.html will be passed straight through to the web browser, without PHP getting involved.


The only time the PHP interpreter is called upon to do something is when a web page is loaded. This could be when you click a link, submit a form, or just type in the URL of a web page. When the web browser has finished downloading the page, PHP plays no further part until your browser requests another page.

Because it is only possible to check the values entered in an HTML form when the submit button is clicked, PHP cannot be used to perform client-side validationin other words, to check that the value entered in one field meets certain criteria before allowing you to proceed to the next field. Client-side validation can be done using JavaScript, a language that runs inside the web browser itself, and JavaScript and PHP can be used together if that is the effect you require.

The beauty of PHP is that it does not rely on the web browser at all; your script will run the same way whatever browser you use. When writing server-side code, you do not need to worry about JavaScript being enabled or about compatibility with older browsers beyond the ability to display HTML that your script generates or is embedded in.

PHP Tags

Consider the following extract from a PHP-driven web page that displays the current date:

 Today is <?php echo date('j F Y');?> 

The <?php tag tells PHP that everything that follows is program code rather than HTML, until the closing ?> tag. In this example, the echo command tells PHP to display the next item to screen; the following date command produces a formatted version of the current date, containing the day, month, and year.

The Statement Terminator The semicolon character is used to indicate the end of a PHP command. In the previous examples, there is only one command, and the semicolon is not actually required, but it is good practice to always include it to show that a command is complete.


In this book PHP code appears inside tags that look like <?php ... ?>. Other tag styles can be used, so you may come across other people's PHP code beginning with tags that look like <? (the short tag), <% (the ASP tag style) or <SCRIPT LANGUAGE="php"> (the script tag).

Of the different tag styles that can be used, only the full <?php tag and the script tag are always available. The others are turned off or on by using a PHP configuration setting. We will look at the php.ini configuration file in Lesson 23, "PHP Configuration."

Standard PHP Tags It is good practice to always use the <?php tag style so your code will run on any system that has PHP installed, with no additional configuration needed. If you are tempted to use <? as a shortcut, know that any time you move your code to another web server, you need to be sure it will understand this tag style.


Anything that is not enclosed in PHP tags is passed straight through to the browser, exactly as it appears in the script. Therefore, in the previous example, the text Today is appears before the generated date when the page is displayed.



    Sams Teach Yourself PHP in 10 Minutes
    Sams Teach Yourself PHP in 10 Minutes
    ISBN: 0672327627
    EAN: 2147483647
    Year: 2005
    Pages: 151
    Authors: Chris Newman

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net