Tutorials

Learn web design and programming with our free video and text tutorials.

Web Designer? Design and market your own professional website with easy-to-use tools.

Introduction to PHP

You should be familiar with html/xhtml before learning php.

What is PHP?

PHP, or PHP: Hypertext Preprocessor, is a cross-platform scripting language that is primarily used to create dynamic and interactive web pages, which means that it can be used on almost every major operating system. It can be embedded into HTML and is generally run on a web servers that are running the Apache HTTP server software. It is free software and is free to download and use.

What you should already know before learning PHP

Before learning PHP, you should at least be familiar with html/xhtml.

PHP and Databases

PHP allows web developers to create scripts that interact with databases and in particular has specifically built-in features for working with MySQL databases.

MySQL is a RDBMS (relational database management system) that allows multiple users access to a number of databases.

Getting started

To be able to create PHP enabled websites, you need to either have Apache, PHP and MySQL installed on your computer or have your website(s) hosted with a website host has all 3 installed on their servers and allows you access to all of them.

If you choose to have all 3 on your computer, you can try free third-party software that will auto-install all 3 of them for you. Some third party software are: XAMPP and WAMP.

Download sources for XAMPP and WAMP

PHP Website Hosting

To find a website host that has enables you to create PHP and database driven websites, take a look at these websites.

How PHP Works

Generally, PHP works in partnership with a web server, which enables you to build interactive and dynamic web pages. A web server is the software that delivers web pages to your website's users. The PHP software works in conjunction with the web server to allow the functionality necessary to deliver the interactive and dynamic web pages.

When a website user goes to a web page with a .php extension, for example by going typing the URL into the web browser directly or by clicking a link, the request is sent to a web server. That request is then sent to the PHP interpreter.

image of how php works - php tutorial

The PHP interpreter also communicates with file systems, databases and email servers as needed, and then delivers the request to the web server to return to the web browser.

In comparison, when a website user goes to a purely HTML generated web page, the server sends HTML data to the web browser without having the code interpreted.

image of how php works - php tutorial

php.ini file

The php.ini file is the file that is used to configure how PHP runs on your computer. When PHP is sent to the PHP interpreter, it reads the php.ini file to determine what settings to use.

PHP syntax

The syntax for PHP scripting blocks start with a <?php delimiter and end with a ?> delimiter. A PHP scripting block can be placed anywhere within an HTML document. If PHP is included within an HTML document, the document must have a .php extension and not another extension, for example, it can not have a .html extension at the end of the document. If a document has a .html extension, any PHP code within that document will not be executed.

<?php
// PHP scripting block
?>

This style of PHP delimiters can also be used within an XML document.

There are other PHP delimiters that can be used, however, it is recommended that you use the standard form <?php and ?> delimiters for maximum compatibility.

Other PHP delimiters that you can use are shorthand delimiters (<? ?>), ASP style delimiters (<% %>) and placing your code inside <script> tags with the language attribute set to php.

The examples below show you how each style of PHP delimiter looks.

On servers that have shorthand support enabled you can use the scripting block delimiters <? and ?>.

<?
// PHP code
?>

You can use the ASP style delimiters <% and %> by turning on the asp_tags directive.

<%
// PHP code
%>

You can also place PHP code within the <script> and </script> tags with the language attribute set to php.

<script language="php">
// PHP code
</script>

The code below is an example of a PHP script that sends the text "Hello World!" to the web browser.

<?
echo 'Hello World!';
?>

Each PHP statement must end with a semi-colon (;). This tells the PHP interpreter that that PHP statement has ended. If a semi-colon is not included at the end of the line of code, the interpreter will assume that the PHP statement continues onto the next line.

The PHP interpreter condenses all sequential whitespace in PHP scripts to a single whitespace. This allows programmers to structure their code into a more readable format.

There are two basic statements in PHP that output text to the web browser: echo and print. The example above used the echo statement to output the text "Hello World!".

PHP comment tags

PHP allows you to add comments that are visible to you, the programmer, to PHP code without the PHP interpreter trying to execute the comments as code. The PHP comments tags are // for single-line comments and enclosing /* and */ tags for multiple-line comments.

<?php
// this is a single-line comment
?>

<?php
/*
this is a multiple-line comment
this is a multiple-line comment
*/
?>