CT Group Pty Ltd logo

» Introduction to PHP

This article isn't an attempt to teach PHP coding but takes a step back, providing details on how to set up a PC to allow experimentation with the PHP language and giving tips on places to go to start learning the language itself.

Quick intro

PHP is a runtime language, sometimes called a scripting language. Like VBA (Visual Basic for Applications, the scripting language used in Microsoft Office) there is no need to compile an application. Taking Excel as an example, you write some VBA code, create an icon and link it to the code, click the button and then Excel interprets the VBA code as it runs it and completes the required actions. This means, however, that there needs to be a runtime environment. In the case of VBA, the Excel application is the runtime environment. You can't use your VBA code without Excel.

PHP has a number of runtime environments. There is a command line PHP application that can run the code. The PHP code could be:

 

<?php
echo "Hello World!";
?>

in a file called test.php. At the command line you could type

php test.php

and you world see

Hello World!

on screen.

However, the most common runtime environment for PHP is a LAMP or WAMP stack. LAMP is Linux, Apache, MySQL and PHP. WAMP is Windows, Apache, MySQL, PHP. Apache is a web server and when it is installed a web browser, such as Google Chrome, can be used to visit a web address like

http://localhost/

This will display a webpage that is stored on the local computer that Apache has been installed on. A PHP file can then be created with the desired code in the folder that Apache uses to store the web pages. Then visiting

http://localhost/test.php

in Chrome and will display

Hello World!

displayed on screen.

The easiest way to get PHP up and running on Windows is to install the XAMPP software. Visit www.apachefriends.org/en/xampp.html to download the software.

Virtual Machines

Installing all this server software can slow down your PC, and can use up a bit of disk space in the process. If you decide that you want to install Linux and run a LAMP stack it would require partitioning the drive, doing the install and then switching between Windows and Linux depending on what you want to do.

A much better way to test out your ideas and play around with new applications without affecting your day to day use of your PC is to install all this software into a Virtual Machine. A Virtual Machine pretends to be a whole new computer to the operating system and software that is run inside it, allowing you to install another copy of Windows in Windows, or a copy of a Linux distribution in Windows. Then you can test and install all your desired software within this virtual environment without affecting your day to day use of the PC. And if something goes wrong, just delete it and start again, as it's just a file on your PC. Trying something tricky, take a snapshot of the virtual machine, try what you want to, and if it fails just restore the snapshot and it's like you never tried it.

Installing the LAMP or WAMP software for in a Virtual Machine and testing it from there is highly recommended. Oracle's VirtualBox is a great free virtual machine (developed by Sun, don't give Oracle credit for it). Download it at www.virtualbox.org/wiki/Downloads.

You could install another copy of Windows, but each copy of Windows you need to install has to be separately licensed and activated, even if it is on the same PC. An alternative is to install a LAMP stack with Ubuntu. Ubuntu is a great Linux distribution with lots of support. A Google search will normally provide an answer to questions you have. It is also regularly updated with the latest releases of Apache, MySQL and PHP. Although a production environment would normally utilise the server version without a GUI on a server, for the purpose of testing you can use the Desktop edition which provides a GUI and allows you to install new software for editing files, etc, easily. You can download Ubuntu at www.ubuntu.com.

Once installed in the virtual machine (simply mount the ISO as a DVD device in the virtual machine) follow the steps at www.unixmen.com/install-lamp-with-1-command-in-ubuntu-1010-maverick-meerkat/ to install the LAMP server. To make files accessible to the Apache web server save them in the /var/www directory in the file system.

That gives me the environment, what about the programming?

So now you have somewhere to run the PHP code, you need to write some PHP code.

The first place to visit is w3schools.com. These guys have great intros to HTML and PHP, which will get you into the language.

There's a great site from a Melbourne company called www.sitepoint.com which has some good tutorials, etc. These deal with specific topics, and you'll often find something relevant with a search.

You may also find a physical book or the ebook equivalent a great investment, as you can read the theory at lunch time and practice it at night. Key concepts such as variable, arrays, functions and object orientated concepts such as classes and inheritance will be your first port of call. Then you can start looking at the built in classes for MySQL access, data functions, file functions, email functions and dealing with web form data.

Once you've grasped the server side you can then look at client side functions such as AJAX, which is a key concept for greater user interaction in web applications.

What to from here?

Chrome has been mentioned above as it is a great browser for developing applications in. It conforms tightly to the HTML spec so pages display the way you want them. Internet Explorer can require continual tweaking, which you can do once you've developed an application but is annoying to have to deal with through the development process. Chrome also provides great debugging tool (just press F12 to display it). Firefox has an add-on called FireBug which does similar things, but Chrome has it all inbuilt.

When you start dealing with databases you'll need a tool to make management easier, rather than having to do everything on the command line. There is a great tool called phpMyAdmin, which runs with Apache and PHP to allow easy creation and administration of databases on the server. Installing it in Ubuntu is easy, just type

sudo apt-get install phpmyadmin

at the command line. Once installed visit

http://localhost/phpmyadmin

Quite often languages are used with a framework, which is a layer on top of the language that does a lot of the leg work for you. As an example, a great framework for PHP is CakePHP. In straight PHP a request for a user list might look like:

$query = "SELECT name, login FROM users WHERE last_login >= '2013-01-01'";

$result = $db->query($query);

if($result == fales) {

  echo "An error occured";

} else {

  $data = array();

  while($row = $result->fetch_assoc()) {

    $data[] = $row;

  }

}

This results in an array called $data with the user data. However, CakePHP completes the same task with one line:

$data = $this->User->find('list', array('last_login >=' => '2013-01-01'));

Ten lines of code becomes one line of code. The CakePHP framework then takes over an runs all it's associated code to retrieve the result, reducing the amount of coding required to achieve the desired result.

Before you tackle a framework, however, complete at least a couple of basic projects in straight PHP as this will give you an idea of what the framework is doing in the background for you. There are many other frameworks as well, with CakePHP being our preferred framework after doing some research, reading some whitepapers, and testing out a couple. It's not the smallest or the fastest, but it is still fast and it is very flexible.

CSET Technologies is a member of
CT Group Pty Ltd, © 2017