Basic PHP Syntax

To create your first PHP page, you’ll start exactly as you would if you were creating an HTML document from scratch. Understanding the reason for this is vitally important: Web browsers are client applications that understand HTML; PHP is a server-side technology, which cannot be run in the client. To bridge this gap, PHP will be used on the server to generate HTML that’s run in a Web browser.

There are 2 main differences between a standard HTML document and a PHP document.

First, PHP scripts should be saved with the .php file extension (for example, index.php).

Second, you place PHP code within <?php and ?> tags, normally within the context of some HTML:

<body><h1>This is HTML.</h1>
<?php PHP code! ?>
<p>More HTML</p>

Relocating users

PHP also provides a method to move a user from one page to another in your Web site without requiring the user to click a link or a button. You can send a message to the Web server that tells it to send a new page by using the PHP header statement. The format of the header function that sends the user to a new page is as follows:

header("Location: URL");

The header statement sends the message, Location: URL, to the Web server. In response, the file located at URL is sent to the user’s browser. Either of the following statements are valid header statements:

header("Location: newpage.php");


The header function has a major limitation. The header statement can only be used before any other output is sent. You can’t echo output — such as some HTML code — to the Web page and then send a message requesting a new page in the middle of the script.

Refreshing the current page

To refresh the current page just use:

$curPage = $_SERVER['PHP_SELF'];


Post A Comment

Anti-Spam Quiz: