Online
 
Thursday, 20 November 2008
 
 

PHP - Embedding PHP in HTML | Print |  E-Mail
 

PHP - Embedding PHP in HTML

You embed PHP code into a standard HTML page. For example, here's how you can dynamically generate the title of an HTML document:

<html><head><title><?echo $title?></title>
</head>...


The <?echo $title?> portion of the document is replaced by the contents of the $title PHP variable. echo is a basic language statement that you can use to output data.

There are a few different ways to embed your PHP code. As you just saw, you can put PHP code between <? and ?> tags:

<? echo "Hello World"; ?>

This style is the most common way to embed PHP, but it is a problem if your PHP code needs to co-exist with XML, as XML may use that tagging style itself. If this is the case, turn off this style in the php.ini file with the short_open_tag directive. Another way to embed PHP code is within <?php and ?> tags:

<?php echo "Hello World"; ?>

This style is always available and is recommended when your PHP code needs to be portable to many different systems. Embedding PHP within <script> tags is another style that is always available:

<script language="php" > echo "Hello World";
</script>

One final style, in which the code is between <% and %> tags, is disabled by default:

<% echo "Hello World"; %>

You can turn on this style with the asp_tags directive in your php.ini file. The style is most useful when you are using Microsoft FrontPage or another HTML authoring tool that prefers that tag style for HTML-embedded scripts.

You can embed multiple statements by separating them with semicolons:

<?php
echo "Hello World";
echo "A second statement";
?>

It is legal to switch back and forth between HTML and PHP at any time. For example, if you want to output 100 <br /> tags for some reason, you can do it this way:

<?php for($i=0; $i<100; $i++) { ?>
<br />
<?php } ?>

Of course, using the str_repeat( ) function here would make more sense.

When you embed PHP code in an HTML file, you need to use the .php file extension for that file, so that your web server knows to send the file to PHP for processing. Or, if you have configured your web server to use a different extension for PHP files, use that extension instead.

When you have PHP code embedded in an HTML page, you can think of that page as a PHP program. The bits and pieces of HTML and PHP combine to provide the functionality of the program. A collection of pages that contain programs can be thought of as a web application.

 

 

This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a comment.
Users' Comments (0)

Comment an article
  Name
  E-mail
   Title
Available characters: 4000
 Notify me of follow-up comments
This image contains a scrambled text, it is using a combination of colors, font size, background, angle in order to disallow computer to automate reading. You will have to reproduce it to post on my homepage
Enter what you see:

No comment posted

Jumbo Coklat
 
Top! Top!