What is PHP?

At the end of this post you will be able to:

  • Identify what PHP is
  • Identify the tools and set-up required
  • Create a basic PHP script

PHP – Definition

PHP (Hypertext Preprocessor) is a server side scripting language.  Straight from the horse mouth the php.net manual states:

PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.
Source: http://php.net/manual/en/intro-whatis.php

Which looks like this:

<?php
 echo "This is my first script";
?>

What does PHP actually do?

  • Processes code on the server side
  • Sends content to the client and receives data from the client (user)
  • PHP is similar to C-based languages such as Perl and PEAR

What’s the difference between JavaScript and PHP?

JavaScript exists in the browser and is loaded client side.  For example when you write JavaScript and view source you can see it in the Document Object Model (DOM).  If you try out the example above and try to view source you will not see the code just the output.

Why use PHP?

  • Like any server side languages they do not rely on browser variations
  • PHP can access serverside resources such as databases
  • Because data is processed on the server it reduces the load on the client (browser)

What do you need to run PHP and build PHP applications?

To run PHP you need:

  • A server such as Apache (http://apache.org/)
  • PHP processor (http://php.net/downloads.php)
  • If you want to interact with a database you will need a database server such as MySQL (http://mysql.com)

You don’t need to roll your own!

Doing all the above by hand can be a pig to do!  The good news is there are some pre-prepared tools that can set up the above for you:

  • WAMP Server (Windows): http://www.wampserver.com/en/
  • XAMP: http://sourceforge.net/projects/xampp/
  • MAMP (Mac): https://www.mamp.info/en/
  • If you are using a Linux box and something such as a Debian based environment there are some resources for building a Linux, Apache, MySQL, PHP environment (LAMP): http://howtoubuntu.org/how-to-install-lamp-on-ubuntu

PHP – Using Server Resources

Once you have set up your environment why not try the example below?  This simple script demonstrates how you can make use of your server’s resources in this case returning the system data and time.  Try it!

Today’s date (according to this web server) is <?php //Format server date 
echo date('l, F dS Y.'); ?>

What is going on here?

Here we have displayed the date according to the server.  You will notice we are using some predefined parameters

'l, F dSY'

These are a type of regular expression to format dates and times. You can find more information about them and many more here: http://php.net/manual/en/function.date.php

Commenting PHP Code

Its always good practice to comment your code to describe what is going on or to temporarily comment out your code for debugging purposes:

Use the following:

// For single line quotes
/* for multiple line quotes */

What have we explored in this post?

  • We have identified what PHP is
  • Identified the tools required to set up a PHP environment
  • Created a basic PHP script to see PHP in action
No Comments

Post a Comment