The manual Page
Version française
   
index | glossary | news | downloads | links ]
  PHP
PHP basics
syntax
controle structures
MySQL
Oracle
source code
 
news
glossary
links
downloads
 
credits
contact
 
 
search
 
last update
19/02/2003
Valid HTML 4.0!
Valid CSS!
Hit-Parade
Mesurez votre audience


  Embedding PHP in HTML pages

Introduction

This page deals with embedding PHP in HTML pages, that is to say how to write dynamical web pages with PHP.

PHP is indeed very convenient to create such dynamical pages, pages that are built according to external fickle factors. Typically can be used to write search engines: the external fickle data is represented both by the keywords provided by a user and references contained in a database. Usually, when people speak about dynamical pages, we refer to databases, but PHP can also handle other kinds of data.

Principle

Generally, dynamical pages are based on a static core (for instance all web pages have the same structure to reflect a given look and feel). PHP and all other languages used for the same purpose like ASP or Perl just add a "smart" component in these pages. PHP is then used to dynamically create HTML code in the middle of a page, according to given factors. However, pages may not contain any static core but is it quite rare, because a web page should always contain static HTML code such as headers.

The important point is the following: at the time of delivering a web page to a client, a server reads this page. It is then possible to "specialize" this process by asking the server to perform given tasks. This mechanism is used for pages that contain PHP code: we ask the server, when it reads the web page, to interpret and execute the embedded PHP code. Standard web servers are not able to understand PHP, they must be prepared for that.

However, a web page with embedded PHP is after all an ordinary resource for the web server: it is a text file like any other one. In order a server to interpret PHP, we must tell it this page contains PHP. We do this by using a dedicated file extension: ordinary web pages have the .htm or .html extension; pages with embedded PHP must have a .php or .php3 extension. Be careful: this extension is typical to a web server: before writting a page with PHP, one must know what extension to use.

Besides, we must identify sections of the page that are written in PHP. We do this with dedicated tags, as we will see in the next section.

First example

When reading the page, a server must be able to recognize PHP. As we have just said, we need to use special tags:

<HTML>
<HEAD>
	<TITLE><?echo $title;?></TITLE>
</HEAD>

<BODY>
...
</BODY>
</HTML>

In this example, the server displays the value of the $title variable.

We can notice that it is useless to completely write the web page in PHP (i.e. to start the page with the <? tag and finish it with the ?> tag and give HTML tags with something like echo "<BR>";). We simply use PHP where we need it. We may then only use once in the middle of the page to display the current date. On the contrary, we may use it at several locations to write dynamical forms...

There are several ways to embed PHP in an HTML page:

  • <? echo "Hello world!"; ?>
  • <?php echo "Hello world!"; ?>
  • <% echo "Hello world!"; %>
  • <SCRIPT LANGUAGE="PHP"> echo "Hello world!"; </SCRIPT>

The first solution is maybe the most simple and common way to embed PHP. However it may pose compatibility problems in particular with XML since this latter language also uses these tags. To avoid such problems, it is safer to use the second solution (<?php ... ?>).

The differents syntaxes

It is obviously possible to write several PHP statements between PHP tags by separating them with semicolons (";"):

<?
	echo "Hello World!\n";
	echo "Second line\n";
?>

PHP is a very flexible language: it is legal to switch back and forth between HTML and PHP at any time:

We are going to create a list of 10 entries::<BR>
<UL>
<?
// Let's use a 'for' loop
	for ($i = 0; $i < 10; $i++) {
?>
	<li>list entry
<? } ?>
</UL>

Click here to see the result.

To understand how this works, we should consider that PHP takes over the complete web page and uses it as a single set of instructions. This also explains the variable scope.

Including files

An important feature of PHP is its ability to include files. This mechanism is widely used in this web site. It is then possible to limit the amount of PHP code by "sharing" a resource file (a file that would contain useful functions for instance) between several PHP files. For thos of you who know the C language, it is similar to #include. Here is an example:

<?
	$title = "Title for my page";
	include("header.inc");
?>

header.inc is the following:

<HTML>
<HEAD>
	<TITLE><? echo $title; ?></TITLE>
</HEAD>

This example illustrates two important concepts in PHP: variables ($title in this example) defined in the including file are available in the included file. Then, every included file is considered as an HTML file and not as a PHP file. This means that if PHP code is embeded in the included file, it must be embeded between PHP tags.

Typically, this example may be used when all pages of a web site have the same header section; the header section is only written once in the header.inc file, and the title is specified in each page with the $title variable.

printable format printable format



Copyright © 2000-2002 themanualpage.org - This site is submissive to the terms of the GNU GPL and FDL licences.