Basically, a PHP file is a text file with the extension
.php which consists of:
- Text
- HTML tags
- PHP Scripts
You already know what text and HTML tags are. So let's look a little more at PHP scripts.
PHP Scripts
The goal is that you become accustomed to looking up and finding
answers to your questions. PHP is so extensive that you can't to learn
all facets in this tutorial. But PHP is not difficult! On the contrary,
PHP is often very similar to plain English.
Let's get started with your first PHP page.
Example: Hello World!
Start by making an ordinary HTML document, but name the file
page.php and save it in the root of the site:
- If you use XAMPP, the path for the root is "c:\xampp\htdocs\page.php" on your computer (which is now a server). Read more abot saving PHP files in XAMPP.
- If you have a website on a host that supports PHP, you simply upload/ftp the file to your web host.
The HTML code should look like this:
<html>
<head>
<title>My first PHP page</title>
</head>
<body>
</body>
</html>
PHP is all about writing commands to a server. So let's write a command to the server.
First, we need to tell the server when the PHP will
start and
end. In PHP you use the tags
<?php and
?> to mark the start and end for the PHP codes that the server must execute (on most servers it will be suficient to use just
<? as start tag, but
<?php is the most correct to use the first time PHP is used.)
Now try to add the following simple code snippet to your HTML code:
<html>
<head>
<title>My first PHP page</title>
</head>
<body>
<?php
echo "<h1>Hello World!</h1>";
?>
</body>
</html>
When we look at the PHP document in a browser, it should look like this:
But it gets interesting when you look at the HTML code in the browser (by selecting "view source"):