Book navigation |
Your First PHP ProgrammeHere 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";
?>
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" ?>
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:
<?php echo "Hello World<br>" ; echo "Goodbye World" ?>
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
|