Searching...
Tuesday 11 June 2013

PHP Basics - Part 1 :: Embedding PHP Code in Your Web Pages

php

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. The web server passes only those pages identified by a specific file extension (typically .php). But every line in that certain page is not considered 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 (escaping) the PHP code. 





There are four delimitation (escape syntax) variants, as follows:


Default Syntax

The default delimiter syntax opens with <?php and concludes with ?>, like this:

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

Output:
Welcome!
Some dynamic output here
Some static output here


Short-Tags

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 (remember?). 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.";?>

Script

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>

ASP Style

If you’re coming from an ASP background and prefer to continue using their delimiting strategy/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.

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.


2 comments:

 
Back to top!