Your First PHP Script
Published: November 29th, 2008 by: Andrew
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:
<?php /* 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 be on multiple 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.