PHP: Making your first PHP page
Creating a PHP page is very similar to creating an HTML page. There are two main differences between an HTML page and a PHP page, your PHP page will need to have a .php extension instead of .html, and you will use PHP code in your PHP page.
Before we go on you will need to know what 'delimiter' means:
delimiter character
A character or string used to separate, or mark the start and end of, items of data in, e.g., a database, source code, or text file.
The PHP delimiters we need to use in our PHP page are <?php to start our PHP code and ?> to end our PHP code. We need to use these delimiters to separate our PHP code from our HTML because otherwise the server will simple interpret it as text in our code and it won't execute it.
So a basic PHP page would look like this:
<head>
<title>My First PHP Page!</title>
</head>
<body>
<?php
Our PHP code will be here
?>
</body>
</html>
But what good is that, It doesn't do anything. We will now add some PHP code to print some text onto the page. For this we will use the echo function.
<head>
<title>My First PHP Page!</title>
</head>
<body>
<?php
echo("This text was printed using PHP!");
?>
</body>
</html>
Notice that there is a semicolon (;) at the end of the line of PHP code. If you don't have this the code will not work properly and your server will return an error.
You can also print HTML tags using PHP:
<head>
<title>My First PHP Page!</title>
</head>
<body>
<?php
echo("<p><b>This text is bold and it was printed using PHP!</b></p>");
?>
</body>
</html>
But all of this can still be done using only HTML, why would we want to use PHP? Because PHP can deliver dynamic content by interpreting user input and adapting the page to the users requirements.
How do we get user input? We use HTML forms as user inputs and use PHP to read the information entered and respond by changing the page output. I will discuss processing forms using PHP in another tutorial.
If you have any questions or comments feel free to post them or email me. You can find my email on the contact page.