Tutorials - Scripts > Introduction to PHP

Tutorials & FAQs: Scripts: Introduction to PHP


Introduction

PHP, or PHP: Hypertext Preprocessor, is a scripting language that can be used on your website to enhance the experience of the user. It is primarily used in conjunction with a database (usually MySQL) to create dynamic and interactive websites. Similar in function to Microsoft's ASP, PHP was created by Rasmus Lerndorf in 1994 as an Open Source language.

The language is released under the developer's own licence but is Open Source which means you can have access to the source code if you wish. You can use it and redistribute it if you wish and it is completely free! The website of the Open Source Initiative provides a more complete definition of Open Source.

The current version of PHP is v4 and is constantly being developed by the PHP team to make sure it is up-to-date and has the features that users want.

The PHP website (http://www.php.net) is an extremely good resource for developers. It contains the PHP manual which documents every single PHP function and you can access it for free!


Usage and Features

PHP can be used on any web server - it is typically used with the Apache web server but can also be used on the Microsoft server IIS as well as many other servers. You can simply download the PHP package and install it on your server at no cost.

PHP has many different features and strengths which make it stand out over its competitors Perl, ASP, KSP and ColdFusion. These features include:
  • High Performance
  • Ability to communicate with different database systems
  • Many functions and libraries for common web tasks
  • Extremely low cost
  • Ease of use
  • Easy to learn
  • Open source
PHP has been created with the web developer in mind which means it comes with default functions for almost every single script you would ever need to write - and you can write your own functions should the need arise. This is different from the Perl language which requires developers to use an external library of code. One of the main benefits of the PHP engine is the high performance value. When running as a server module, it is extremely fast - much more than Perl running as CGI.

PHP 4 includes the Zend engine which was created in version 3 of PHP. This is a brand new parsing engine which sits below the PHP modules and is optimized to significantly improve performance. For example, code written for PHP 3 will run up to 200x faster with the Zend engine running. The Zend engine is also designed to increase the flexibility of the communication with servers. The server module runs in conjunction with the server and does not have to be initialised every time a script is run. This obviously improves performance.

PHP is a server-side language. This basically means that any code you write will not be seen by the visitor. When the page is loaded, the PHP parser scans the page for any code and then executes it as it finds it. Once the script has been fully executed, the output is sent to the visitor's browser, all in a matter of seconds. That way the visitor will only ever see the HTML output of the script. This also means that client-side script cannot communicate directly with PHP scripts.

PHP is an extremely flexible and extremely well used scripting language. Used in conjunction with the MySQL database system, it could be classed as one of the most powerful web scripting languages available, if not the most powerful and I definately recommend you investigate using it!


Basics

To use these scripts you will need a PHP server and you will also need to name any files in which PHP scripts are included with a .php ending so that your server knows to execute the scripts within that page.

One great thing about PHP is that you don't have to worry about other people stealing the scripts that you've spent hours creating designed specifically for your web site. This is because the PHP will never show in the code of your web site. So you can consider it 'gone' but it's still there and working to make your site more interactive.

Every PHP script or code starts with <?php (or you can just open and close it with just <? and ?> - instead of <?php).

One of the most basic commands is the echo command. This command is PHP's way of taking text (to be displayed) from the script and printing it on the page. So, to execute the script, you'd need to write:

<?php
echo 'Text here';
?>

The above would display 'Text here' (without the apostrophe's) on the web page. Note the semicolon ; at the end of each line. This tells the parser that you have finished on that line. Omitting this will result in an error.


Variables

Variables play a large part of PHP and this is just a short article to help get you started with them.

Every variable starts with a $ (dollar sign) and you can name your variables whatever you like, just keep them short and simple so that you can type them quickly and easily when you need to. There are just a couple of things to remember, though, when you're writing variables. You can only follow the dollar sign with a letter or an underscore (_) - the letter can be uppercase or lowercase (it doesn't matter which).

The easiest way to imagine variables is like an equation. For instance, the variable is given a value (and a name) and can then be called upon for various functions.

Here is a demo use of a variable (as a mathematical sum):

<?php
$four = 4;
$two = 2;
$total = $two * $four;
echo "$total";
?>

Now if you then put the following code into your PHP page:

The page would display '8'.

If you are not used to the mathematical signs on a computer, then I've listed them below for you:
    + = Add
    - = Subtract
    * = Multiply
    / = Divide

I hope you have found this introduction useful and that it will enable you to go on to create advanced PHP applications.


Article credit - http://www.olate.com
Original Article by: mytton - Edited by: mytton