Neticulis.com > Programming > Learning PHP
>Tutorial Lesson 1: Embedding PHP


Introduction:

Before you can do anything with PHP you need to know where to put it! PHP can be placed directly into any html document, however, when you save the file it must be saved with the .php extension,

Step 1: Create a new blank HTML page.
For the first lesson you need to create a blank html page (with all the required html tags, <html>,<body>, etc... ). php code can go anywhere in the document, but must be enclosed by: <?php ?> , but for this lesson we will be placing the code inside the body tag as seen in Step 2.

Step 2: Insert the PHP embed tag.
Place the php embed tag inside the <body> tag of your blank html file:
1
2
3
4
5
6
7
<html>
<body>
<?php

?>

</body>
</html>
Note: You can see the embed tag for php on line 3 and 5, it has 2 parts, the start and the end, all of your php code should go inside this tag.

Step 3: Insert some PHP code.
Insert your php code, since this is the first lesson we will use the PHP print command to display the words "Hello World!" in the browser.
1
2
3
4
5
6
7
<html>
<body>
<?php // My first PHP file!
print "Hello World!"; // Displays the words Hello World! on the screen.
?>
</body>
</html>
first you will notice // My First PHP file! - this is a comment. A comment is just something to help you out when you read your code, comments can start with two slashes as seen above. print is one of the many commands in php. it will simply display whatever comes directly after it. In this case, its "Hello World!", it is important that we put our text in quotes, since it is a string. This will be covered later on, but for now just know that if your displaying text, you should put it in quotes. You will also notice the ; symbol at the end, that lets php know that we want the line to end. Then we added a comment to the end, so we can remember what this line of code does. This is totally optional, but once you write larger programs its a great idea to comment it.

Step 4: Save the file and test it out.
Save your first php program as hello.php and upload it to your web host (your host must be able to run php! Most these days do.). Now load it in your web browser and you should see the words "Hello World!" show up! You can see it in action here.

Dont stop now, continue on to Lesson 2 - How to use Variables!

Copyright 2005-2008 Neticulis.com