Your First PHP Script

Published: November 29th, 2008 by:

Since PHP is a scripting language, creating your first PHP script is as easy as creating and saving a text file. In this article, you are going to be shown how simple it is to create your first PHP script.

Here is the code we will be working with. Every line will be explained in detail:

    /* output our intro message */
    echo 'It works!';

Copy that code in a text file, save it as text.php, and then upload it to your PHP-enabled web server, and you should see “It works!” in your web browser when you call up that script.

Lines 1 & 4: PHP Tags

All PHP code has to be within PHP tags, like this: <?php (php code goes here) ?>

As you can see, the PHP tags can be in the same line in the code as I just did, or they can be on different lines, as in the first example. We need to surround all PHP code in these tags because it is possible to mix PHP & HTML in the same file.

Line 2: Comment

It’s best to get into the habit of commenting your code now. There are several ways to comment your code:

/* this comment can 
lines */

# single-line comment

// single-line comment

Lines 1-5 shows the best way write a comment that covers multiple lines. Lines 7 & 8 only work on one line as the example shows.

Line 3: ‘echo’ Statement

The echo statement is what sends data to the browser. There is another statement that accomplishes the same thing: print. There are tiny differences between the two statements, but for our introductory article, it’s safe to assume that they are pretty much the same.

So there you have it, a simple script, explained. Read more articles tagged for beginners for more great PHP articles for beginners.

Leave a Reply

Wordpress doesn't like it when you post PHP code. Go save your code at pastebin, and post the link here.

About the Author

Andrew has been coding PHP applications since 2006, and has plenty of experience with PHP, MySQL, and Apache. He prefers Ubuntu Linux on his desktop and has plenty of experience at managing CentOS web servers. He is the owner of Wells IT Solutions LLC, and develops PHP applications full time for anyone that needs it as well as does desktop computer support locally in the local area. He spends most of his free time exploring new programming concepts and posting on The Webmaster Forums.