Basic PHP – The Hello World Program

This is a simple program to start you off using php scripting. This small program prints out “Hello World” to your browser using php rather than plain html.

Type the following into a text editor and save the file as test.php

echo "hello World";

If you point your browser to this file you will see Hello World! displayed in your browser, well done you have just completed your first php program!

PHP Common Errors!

PHP scripts open with a <?php and close with ?> these are often forgotten and missing them out will cause a failure!

All php statements end with a semi colon (;) PHP does not notice white space so it will continue to read until it encounters a ; or a php closing tag ?> You will often see the following error if you leave a semi colon out of your php statement.

Parse error: expecting `’,” or `’;” in /test.php on line 6

If you see this error go back through your code and look for that missing semi colon!

PHP Variables

Variables are bits of memory that are used to store information. All PHP variables begin with a dollar sign ($)other rules of PHP variables are:

  • As mentioned php variables begin with a $. This tells php that we are declaring a variable
  • PHP variables can be any length although you should name variables so you can remember then!
  • Variables can contain letters, numbers and underscores
  • PHP variables cannot begin with a number
  • Be careful of capitals!, The variable $Myvariable is different from a variable called $myvariable

You may also be interested in my Article about adding values to php variables

Whilst learning php I found the PHP for dummies book invaluable and I highly recommend it! PHP & MySQL® For Dummies® (For Dummies)