Friday, 11 January 2013

PHP Tutorial - Your first program is not hello world

Ok so i'm going to assume that you have already got php installed  if not then for linux use a package manager to install each of the sections your will need PHP, Apache and Mysql server.  For windows install a complete package like XAMPP for Mac OSX install MAMP.

Php is a scripting language so the .php files are all text so to run the script you must compile the script so that the computer can run the steps that you have in your code.  Most programming languages compile once and give you bytecode files which are normally then linked into an executable.  for example the java language is compiled into .class files (bytecode) which you then server to a JVM Java virtual machine which takes the bytecode and links it together so that it can run on what ever operating system you have a JVM for.  C compiles to .o files then is linked into exe files which you then server and  run.  With php you just upload your code to a server and it gets compiled & linked each time the page is called.

Ok the first program that we are going to write is to check out what things in the php.ini have been loaded correctly.  Don't assume that because you have put the correct line in the PHP.ini that its installed.  This program will also allow you to find out why a program that works on your dev box doesn't work on your live environment.

Ok php is a scripting language now with most scripting languages you just write your code in the text file and run it

-- filename : hello.bat
echo "hello world"
-- /end file

With php it was written to be embedded in html pages so you need to  wrap your scripts in processor tags that tell the php interpreter php.exe



It is possible to configure php to accept short processore tags <? & ?> you save 3 bytes of information doing it this way.  "DON'T EVER DO THIS" because you will find that you'll move your application to a different provider who hasn't and won't configure php to allow it.  For compatibility with most servers use the long version.

So your first program is not an hello world program we are going to write a program to tell us which extensions that are in php.ini have been enabled.


Thats it! yeah that simple,  the PHP guys have written this function to report what has been configured correctly.  Have a look at the results of the file you will see things like the version of PHP that you are running.

In the future tutorials I hope will we will cover image creation, database access using PDO,  Fetching content from another server via CURL & SOAP.

The next tutorial is going to be how to do different hello world programs in php and not the following hello world program.  So we are going to need the basic image library "GD", the "PDO" library and "CURL" so scroll down the configuration page to check if those modules are available.  If they are not available then configure php.ini and restart your webserver to test if they become available.

 echo "hello world";