PHP Basics

As in all good tutorials, we start with the (in)famous “Hello World” example. Here’s the code:

<title>Hello World</title>
// This is a comment that extends to the end of the line.

/* This is multi-line comment. Comments are ignored by PHP,
   but provide valuable information to people reading the source. */

echo "Hello World!\n";

You should save this piece of code in a file with the extension .php and upload it to your webserver. Then load the page in your browser.

If the page doesn’t say “Hello World!”, then try other extensions like .phtml. If you can’t figure out the right extension, then ask whoever installed the webserver. If that’s you, then have a look at the file called httpd.conf — if you’re using Apache that is… It should contain a line that looks like this:

AddType application/x-httpd-php .php

This line informs Apache that files ending with .php are PHP scripts. Again, please refer to the installation instructions in the PHP manual for more information.

Back to the example, which is somewhat silly. But it can be used to teach you a few things anyway.

Most of the code is just plain HTML. Only the part between <?php` and `?> is PHP-code. You use either <?php` or just `<?` to start the code and `?> to stop it again. The parser works within these two tags. Between the <?php` and `?> you put a number of statements, each of which must be ended by a ; (a semi-colon).

Between the two tags there are two comments and one statement, echo "Hello World!\n";. The comments will be completely ignored, and the statement just prints the string “Hello World” into the webpage, followed by a newline. This is what is sent to the browser. After the parser has processed the page, the final output that is sent to the browser looks this way:

<title>Hello World</title>
Hello World!

As you can see, there’s no trace of the PHP-code left in the final output. In the following code-snippets I won’t include all the HTML code, but remember that you’ll have to use HTML if you want your PHP scripts to work on the Internet. You’ll also have to include the right HTML codes in the strings you output from PHP, if you’re outputting more than just very simple strings.

You should think of a PHP-script as a normal web page written in HTML, since most of the page is still just plain-old HTML. But between the start- and end-tags (<?php` and `?>) the PHP-parsed spices the page up with possibly dynamic content. You will need variables for dynamic content, so go read about it now.


  1. Nasir:


    im having problems with first of my script that is “hello world” and the “echo” function is not showing any text at all wat shud b the problem pleas help


  2. Martin Geisler:

    I don’t know what the problem is — have you pasted the code directly as shown into a file? Call the file hello.php and upload it. Then tell me exactly what you see when you load the page in your browser.

  3. Nasir:

    please tell me how to upload my script to web server (pws windows 98) and how to load it in my web browser.I am saving my scrip as .php and all the possible settings are in place including registry and php.ini but nothing is working. when i check type of file in properties of the file it says php script but the file icon is not changed to that of Internet Explorer. but when i open the file with open with… option in IE the script portion doesnt work only html portions works. pleas help

  4. Nasir:

    hi Geisler

    its pleasure to know that u r from denmark. i also lived for four years in denmark but a long long time ago when i was a little kid in Copenhagen with my father. first of my memories are from denmark. but when i was only 7 we came back home. but only thing i can remember very cleary is the snow all around our house in winter. but here in my home its quite hot,humid,dusty and what not. wish you good luck in you exams.

  5. Martin Geisler:

    :-) That’s some nice memories to have of Denmark… Oh and thanks for the good luck to my exams! I had my second exam today and it went very well. The next and final one is on Friday.

  6. Martin Geisler:

    Disclamer: I haven’t used Win98 in a very long time and I never played with the PWS (Personal WebServer?)… I use Linux exclusively now.

    But when you describe opening the file using “Open with…” in IE then remember that you cannot open it as a file from your C drive, you have to open it as http://localhost/file.php or something like that. If you just open it from your C drive then no PHP will be executed, as you’ve seen.

    So first make sure that the PWS is working meaning that you can access files on it using an URL of the form http://.... Then download and install PHP. It says on the page that the Windows installer will automatically configure lots of different webservers, including PWS.

  7. Nasir:

    i have al little success so far and thats i installed php and now scripts are working but im stuck again when i tried to copy a code froma book. situation is that i am using an HTML form and displaying its textbox value using PHP script. for example if textbox name is “name” then i am using in php

    echo $name;

    but the variable value is displayed only in address bar not where i want it to be print.

    //code Starts

    This is my personal Web Page

    Enter Your Name Below


    //code Ends


  8. Anonymous:

    I thought the Hello World Comment was the first thing people want to get rid of..

  9. Declan:

    The hello world example is a necessary evil, I’m afraid.
    It is useful in that if used or seen one before, you can compare and contrast, while getting a good look at the syntax.

    To all visitors here completely new to PHP, can I recommend a program called XAMPP. Install, it and go to (for this example)

    http://localhost/whatever_the_filename_is_called.php in your browser

    You should now have said hello to the world. If it is polite, it will greet you back!

  10. leo:

    Good work.

  11. tokz:


  12. Zach Hill:

    Just a recommendation, So far your tutorial is good, but you could try making the example text bigger. The times new roman font is the give away that it is the examples, no need to make us squint at it :)

    Simply constructive criticism.
    Feel free to email me if you wish.


  13. trikapalanet:

    Good suggestion of Zach, with a big exemple it will be better :)

Leave a comment