This post will go through coding and serving your very first php script, aimed as a tutorial for beginners, so don’t worry if you know nothing about PHP – this is the place to start! As is the accepted norm within the world of codez, we will be putting together the ubiqutous ‘Hello World’ script. If you have a php server set up on your pc, laptop or pc, great. If not, you will need to do that before following this tutorial, You can find out how to do this here.
Your First PHP Script
Create a new file, ‘index.php’ and save it in your site folder in htdocs. If you haven’t been following the tutorial or have your own server set up, then save the index.php wherever your particular server can find it. Now type the following in the file:
<?php echo "Hello World";
Save that file, make sure your server is started, and enter the following url in your browser:
Remember, you may not need the ‘my_site’ part of the url, or you may have called it something different depending on your set up.
You will see the following:
What’s it all Mean?
Great! So what’s going on? Well, this is probably the simplest program you can put together, so there are only a few things we need to go through, and lets do that line by line ( all two of them… ).
The first line:
Tells the server that we have a php file – basiacally we are letting the server know that everything following this line is going to be in PHP, so it needs to pass what it finds here to the PHP interpreter. The interpreter is another program that will read through or parse the PHP code and action the script you have written. It will do this by converting your script into language that the computer will understand, in essence, interpreting what you have written. Everything will go through the PHP interpreter until we reach the following line:
At this point, we are telling the interpreter that we have finished with our PHP script now, so pass control back to the server. You will notice we don’t have one of these closing statements in our code. It’s not considered best practice to close your PHP if that is all we have in the file.
The second line, or statement is where the ‘magic’ happens:
echo "Hello World";
‘Echo’ is a php statement that tells the php interpreter that we want to output something. In the context of within a web server, we are outputting to the browser. What we want to output is contained within quotes.
You will notice that there is a semi-colon at the end of the statement. This tells the interpreter that we have finished with this statement and it’s time to go on to the next. If you forget to put this semi-colon in, the interpreter will throw a wobbler, ie, it will flag an error and halt the script. I will guarantee that sometime in your coding career you will have at least one instance of a missing colon causing an disproportionate and frustrating amount of bug hunting! So get into he habit early of always checking your semi-colons :)
You can output pretty much anything with your echo statment, including html tags. Try the following:
echo "<h1>Hello World!</h1>";
You will now see your hello world in fantastic header size glory.
Now, you may be thinking, what’s the point? Why not just save the file as an html file? Well lets mix things up a little and use some variables. A variable can be thought of a storage location for a value. Think of it as a location in the computer’s memory that can be given a name. You can then put certain things in this location, such as a number or some text. Then, by referencing the name of the storage location, the variable, you will get back the value you have put there. An example will help clarify this concept.
Alter your php script to the following:
<?php $text = "Hello World!"; echo $text;
Now if you save this, and refresh your browser, you will see the same text as before, ‘Hello World’ output on your screen. What we do in the first statement of the code is create a variable called ‘$text’ and give it the value “Hello World!”. Then, when we use the echo statement to output the $text variable, the interpreter will go and see what we have put in the $text location, and return this. To add a further example, add the following two lines:
<?php $text = "Hello World!"; echo $text; $text = "Goodbye Cruel World!"; echo $text;
Now when you save a refresh you will see the following:
Goodbye Cruel World!
After initally returning the value ‘Hello World’, we change the value of $text to be ‘Goodbye Cruel World!”. Now when we output $text, we output the new value. Hopefully you are starting to see how useful and powerful scripting can be to developing websites; and we haven’t even scratched the service yet!
We will continue looking at scripting and start doing some funky things with variables and HTML. I will try and demonstrate how they fit together to make dynamic websites. It may not all fit in the next tutorial, but at least you can see what direction we are taking, and hopefully you are looking forward to seeing how it all fits together! Until next time…