PHP Simple Syntax

Basically, a PHP file is a text file with the extension .php which consists of:

  • Text
  • HTML tags
  • PHP Scripts

To work with PHP you need to know HTML but we are going to generate dynamic web pages with PHP. PHP is not difficult! On the contrary, PHP is often very similar to plain English.

Let's get started with your first PHP page.

PHP Hello World! Example

Start by making an simple HTML document, but name the file index.php and save it in the root of the site:

The HTML code should look like this:

<html>
<head>
<title>My first PHP Hello World page</title>
</head>
<body>
</body>
</html>

Now, we need to tell the server when the PHP will start and end. In PHP scripting language you use the tags <?php (start) and ?> (end) to mark the start and end of the PHP codes that the server must execute.

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>

But it will be interesting when you look at the HTML code in the browser (by selecting "view source"):

<html>
<head>
<title>My first PHP page</title>
</head>
<body> 
<h1>Hello World!</h1>
</body>
</html>

PHP Date and Time Example

Let's make the server write something else. We could, for example, ask it to display the current date and time:

<html>
<head>
<title>My first PHP page</title>
</head>
<body> 
<?php 
echo <h1>Hello World! Date and Time</h1> . date("r");
?> 
</body>
</html>

Now the corresponding HTML code looks like:

<html>
<head>
<title>My first PHP page</title>
</head>
<body> 
Sun, 12 Oct 2014 00:10:02 + 0200
</body>
</html>