Have an account? Sign in
Login  Register  Facebook
This Page is Under Construction! - If You Want To Help Please Send your CV - Advanced Web Core (BETA)
Quick Table of Contents
Embedding PHP Code in Your Web Pages
One of PHP’s advantages is that you can embed PHP code directly alongside HTML. For the code to do
anything, the page must be passed to the PHP engine for interpretation. But the web server doesn’t just
pass every page; rather, it passes only those pages identified by a specific file extension (typically .php) But even selectively passing only certain pages to the engine
would nonetheless be highly inefficient for the engine to consider every line as a potential PHP
command. Therefore, the engine needs some means to immediately determine which areas of the page
are PHP-enabled. This is logically accomplished by delimiting the PHP code. There are four delimitation
variants.
[Edit] Default Syntax
The default delimiter syntax opens with <?php and concludes with ?>, like this:

PHP Code — test.php

<h3>Welcome!</h3>
<?php
echo "<p>Some dynamic output here</p>";
?>
<p>Some static output here</p>

Save that code as test.php and execute it you’ll see the output as follows



[Edit] Short-Tags Syntax
For less motivated typists, an even shorter delimiter syntax is available. Known as short-tags, this syntax
forgoes the php reference required in the default syntax. However, to use this feature, you need to
enable PHP’s short_open_tag directive. An example follows:
<?
print "This is another PHP example.";
?>
Caution Although short-tag delimiters are convenient, do not use them when creating PHP-driven software intended for redistribution. This is because this feature could potentially be disabled within the php.ini file. When short-tags syntax is enabled and you want to quickly escape to and from PHP to output a bit of dynamic text, you can omit these statements using an output variation known as short-circuit syntax:
<?="This is another PHP example.";?>
This is functionally equivalent to both of the following variations:
<? echo "This is another PHP example."; ?>
<?php echo "This is another PHP example.";?>
[Edit] Script Syntax
Certain editors have historically had problems dealing with PHP’s more commonly used escape syntax
variants. Therefore, support for another mainstream delimiter variant, <script>, is offered:
<script language="php">
print "This is another PHP example.";
</script>
[Edit] ASP Style
Microsoft ASP pages employ a delimiting strategy similar to that used by PHP, delimiting static from
dynamic syntax by using a predefined character pattern: opening dynamic syntax with <%, and
concluding with %>. If you’re coming from an ASP background and prefer to continue using this escape
syntax, PHP supports it. Here’s an example:
<%
print "This is another PHP example.";
%>
Keep in mind that just because you can do something doesn’t mean you should. The ASP Style and
Script delimiting variants are rarely used and should be avoided unless you have ample reason for doing
so.
[Edit] Embedding Multiple Code Blocks
You can escape to and from PHP as many times as required within a given page. For instance, the
following example is perfectly acceptable:
<html>
<head>
<title><?php echo "Welcome to my web site!";?></title>
</head>
<body>
<?php
$date = "July 26, 2010";
?>
<p>Today's date is <?=$date;?></p>
</body>
</html>
As you can see, any variables declared in a prior code block are remembered for later blocks, as is
the case with the $date variable in this example.