PHP Tutorial 101: Hello World!

PHP - Hypertext PreProcessorSo, here it is, my first tutorial ever!
I will try to explain how to create your first PHP „script“.

Requirements for the PHP Tutorials:
– at least good knowledge about HTML
– Webspace or local server with PHP Support
– Additional: knowledge about CSS
—————————————–

Well, let’s start with the script itself, I will explain everything later:

<?PHP
echo "Hello World!<br />";
print 'Hello World!<br />';

echo "<a href=\"http://www.ruelicke.net\">Hello World link</a><br />";
print '<a href="http://www.ruelicke.net">Hello World link</a>';
?>

Create a HTML file, insert the code above into the <body> and save it. Then rename the HTML file from foo.html to foo.php
Upload the file and access it.

If everything is working, then you should see „Hello World“ (without the quotes) 4 times. Congratulations, looks like you just wrote your first PHP script.
But what happens there?
Well, I don’t know if you remember MS-DOS or if you ever used it 😉
Anyway, in DOS there were two commands, named ECHO and PRINT. Basically you can say that using either of the commands could return a string input to the screen or to a peripheral (e.g. a printer).
In PHP it means almost the same, except both commands return a string to the screen/website.

Now let’s have a closer look at the script itself:

<?PHP
[...]
?>

Every PHP-Script has to start and end with these two lines.
It tells the server that this script has to be processed before it can be viewed by the visitor.
You could use „<?“ and „?>“ only, but to avoid future compatibility problems, we use „<?PHP“ as starting and „?>“ as closing tag.

Hm…didn’t I say something about it has to be processed by the server before you can see the „Hello World“? Well, PHP actually means „Hypertext Preprocessor“, you could say that the server reads the PHP code and turns it into something you can see. To avoid making this tutorial too boring I just refer to Wikipedia’s PHP entry. Maybe I will write a short article about the history of PHP at a later date, but we will see 🙂

Now let’s go on…

echo "Hello World!<br />";
print 'Hello World!<br />';

I assume you see that I used double-quotes for the echo and single-quotes for the print function. Actually it does not really matter, which kind of quote you use for both, as long as the starting and the ending quote are the same AND the function is closed with a semicolon.
Maybe you wonder why I used the different quotes, of course I have a reason…

Let’s assume you want to have „Hello World“ as a link to your own website:
Would you use the echo with the double-quotes you would either get into problems or your HTML would not validate.
Neither is something we want to happen, so we have to do something special, if not annoying if you have a lot of code where you need to do it:

echo "<a href=\"http://www.ruelicke.net\">Hello World link</a><br />";

As you see I „commented“ the double-quote which is used after „href=“ with a backslash. Remove those slashes and run your script, I’m pretty sure you will run into a PHP bug. Of course, you could replace the double-quotes you use for the echo by single-quotes and everything is fine, but since I prefer using the print function (as it actually prints the string to the screen/website) I use it with single-quotes and have no problems at all:

print '<a href="http://www.ruelicke.net">Hello World link</a>';

Well, I assume you still see no difference between „echo“ and „print“, so let’s go on with your „Hello World“ script and use some variables:

<?PHP
$message = "Hello World!";

echo "Message of the day: $message <br />";
print 'Message of the day: '.$message.'<br />';
?>

This script returns the variable „$message“, which we gave the content „Hello World!“.
Note that all variables in PHP have to start with the dollar symbol: $ and the line also has to be ended with the semicolon.
I assume you looked carefully at the „echo“ and the „print“ code, and you spotted the difference.
As you see I just wrote the „$message“ right into the echo quotes. When using „echo“, it will return variables right away, so you don’t need to write some extra code or so…
Well, since a friend told me it would be better (and easier) to use „print“, I prefer the „print“ function. I don’t know if you listen to me, but I recommend you use „print“. Anyway, it is your decision which function you use.
If you use „print“, please note that you need to insert the variable by inserting it with „quote dot variable dot quote“: ‚.$variable.‘ except the variable is at the beginning or the end of the print function:

print 'some string '.$variable.' some more string';
print $variable.' some string';
print 'some string'.$variable;

Update: Final example of this tutorial

I hope you understood everything so far, in the next Tutorial 102 we will play with some variables, so stay tuned for updates.
If you have any questions, feel free to comment, I will try to answer as fast as possible 🙂

Ein Gedanke zu „PHP Tutorial 101: Hello World!

  1. Would you be interested in writing for The Neave Online Publication? I love your writing style and I feel like you would fit in perfectly with the other writers.

Schreibe einen Kommentar