Your First PHP Programme

Here is a simple PHP page:

<?php

echo "Hello World";

?>

The first line says we are stating a PHP program, the next line does some work, and third line indicates the end of the program.

Let’s look at the second line that does the work:

echo "Hello World";

This line, or statement as we say in programming, has three parts. The first part echo is a PHP command. The echo command is very useful in PHP because it does something very simple. It takes some information from us and prints it to the webpage. So for example, the following PHP page produces a simple webpage that says Hello World. Try running it yourself and see if it works for you.

<?php

echo "Hello World";

 

?>

 

  arrow

 

 


The “Hello World” part of the statement is a string. We know this because it is between two double quotes. Anything that you put between two double quotes will be treated as a string in PHP. And by a string in programming we basically mean some text, a list of characters. In our example the “Hello World” string is the argument to the echo command. Argument is a word that means something specific in programming. Sometimes the word parameter is used instead of argument which gives us a better hint as to its meaning. Basically an argument is some information you give to a command in order to do its work. The important thing to understand is that the argument can be different every time you use a command - as in life we can always find new things to argue about :-).  In the case of echo we just put the argument after the command name. This is known as passing the argument to the command.

The last part of our first PHP statement is a semicolon (;). This is just a command to let the PHP processor know that we are finished our statement. It’s important to put tjhat semicolon there though. In our example it won’t make a difference if we omit the semicolon, as it is a simple program on only one line. But suppose we were to try and add a second statement. Here is a PHP programme with statements on two different lines, but without a semicolon after the first statement. Try running this programme yourself to see what happens:
 

<?php

echo "Hello World"

echo "Goodbye World"

?>

 

arrow

 


If you’ve gotten this far, and have seen something like the above in your browser, congratulations you’ve authored your first bug! (a proud programming moment). The PHP processor is letting us know that we have done something wrong and that it can’t understand the program. It tries to give us some feedback to help us correct the error. Often this feedback can be pretty cryptic but you can see that in this case it is telling us broadly what is wrong:  on line 4 of hello.php we omitted a semicolon. However this is not exactly correct as the error is actually on line 2 not 4. The processor gets confused when we don’t tell it exactly what to do and it often cannot pinpoint the exact line of the error (or even the cause) but it does make a guess at what might be wrong and tries to tell us.

Because PHP error messages often only give an indication of where we went wrong it’s important to test small sections of your code as you go along. This is especially true if you are a beginner. You should program by just changing one line of your code at a time. The idea is to change or add a new line of code and then test the page in your browser. If you get an error you know its on the line you just changed. Change, test, change, test. Of course this is a slow way to develop code, and if you are more experienced you might write tens of lines of code before testing to see if they work. Nonetheless developing small bits of working code incrementally or iteratively is one of the core ideas of software engineering. Some important modern software development methodologies such as test-driven development come from this basic idea.

To fix the above code you need to add a semi-colon after the “Hello World” string. If you do this, and test the correct page, you should see something like the following:



You may be thinking that this is not exactly what you were expecting. It would be nice if Goodbye World was on a new line. To do this we can just add some HTML into the first string to specify a line-break. Adding the line break tag <br> to our code gives:
 

<?php  

echo "Hello World<br>" ;

echo "Goodbye World"  

?>

 

arrow

 


Have a look at the source of resulting web page (right click the webpage from the browser and View Source) and you should get a better idea of what is happening here. You should see something like:
 

Hello World<br>Goodbye World

You can see that the text is all on one line. Once the browser renders the page tit will add the line break but we can add one to the source file also if we so wish by adding a new line \n instruction to the PHP code:

<?php  

echo "Hello World<br>\n" ;

echo "Goodbye World"  

?>

Now test this page, view its source and you should see something like:

Hello World<br>

Goodbye World

The backslash we added to the first string is called an escape character or sequence. Escape sequences can be used in double quoted PHP strings to specify things like non-printing characters such as a tab \t or a linebreak \n.

Test your code as you write it. If you write a line of code - test it. If you change a line - test it.
A PHP error message is just guessing where you went wrong (because it doesn’t know exactly what you intended).
Make sure to test your PHP files through the web server on your computer and not from the filesystem i.e. don’t try and open the file directly, instead you need to open a browser to your local web server address. You should see http://localhost/somethingsomething in the address bar of your browser when you are testing and not file:///C:/Documents/Somethingsomething