Embedding Php code in your web pages

      No Comments on Embedding Php code in your web pages

Embedding php is pretty easy.One basic advantage of PHP is the it can be embedded alongside HTML. For the code to do anything, you just need to pass it through a php engine for interpretation.

Default syntax

The default syntax contain php code starting from and ends with ?> . Every php statement ends with a delimiter ';'.

<h3>Welcome</h3>
<?php 
echo "Some dynamic code";
?>
<p>Some static code</p>

echo is similar to a print statement.

Short Tags
Though less motivated, a php code can be wriiten as

<?
echo "php code";
?>

Caution: Short tags delimiters should not be used while making a php driven software intended to redistribution.

Srcipt

Another way for embedding php in your code is using srcipt

<script language="php">
print "This is another php example";
</script>

ASP style

Microsoft ASP provides a similar delimiting strategy as used by php. The delimiting strategy forms a specific pattern, starting the dynamic code using <% and closing the dynamic code using %>
Example:

<% 
print" another php code";
%>

Caution:This ASP style is no longer used form PHP 5.3.

Embedding multiple code blocks

You can use multiple php codes in a single page.For instance see this code:

<head>
<title><?php echo "i am title"; ?></title>
</head>
<body>
<h3>welcome</h3>
<?php 
echo "hii";
?>
<p>some text goes here </p>
<?php 
echo "php again";
?>
</body>

Commenting on your code

Single line comment
Comment often do not require more than a single line.PHP supports C++ single line comment preface by a double slashed (//).
Example:

<?php 
//title:hello
echo "code";
?>

Shell syntax
PHP also supports C++ shell syntax in comments, using (#)
Example:

<?php 
#title:hello
echo "code";
?>

Multi line comments
PHP also supports multi line comments as same C++. The multi line comments begin with (/*) and end with (*/). This block can have many lines of comment.
Example:

<?php 
/* hi i am a multi line comment
I can hold many line 
comment more */
echo "code";
?>

Leave a Reply

Your email address will not be published. Required fields are marked *