Aug 13, 2011
admin
Comments Off

Embedding HTML in PHP

What is PHP?

PHP is a server-side run open source scripting language. Generally the web server hosts this scripting language. The way JavaScript is used to create dynamic content for web pages running on client side, PHP is used for the same purpose of providing dynamism for a website, but running on server side. Currently 75% of the web-servers running have PHP deployed within them. PHP works as a filter taking PHP instructions or text as input and providing HTML as output. PHP is used for many other applications as of today such as server-side scripting. Embedding HTML in PHP is another application for which PHP is being used today.

Staying away from codes for as long as possible, the PHP only works by processing things that are enclosed within the valid identifiers which are <?php and ?>

 

<?php

…PHP code or HTML content…

?>

 

Embedding Control Structures

 

PHP can also be used to for webpage flow control. PHP introduces conditional or repetition-control structures for doing so. Since PHP only processes code that is contained within the code blocks and ignores everything outside it, it can be used to the advantage to the user.

 

<?php if(conditions) { ?>

… HTML CODE …

<?php } ?>

 

Suppose, the HTML code within this PHP code blocks is to be displayed only under the specific conditions which are specified within the “if statement”. This snippet can do exactly this. Display this HTML code once the conditions are met. Similarly while and for loop structures can also be embedded.

Two different ways can be described to use HTML on the PHP page. One way is to keep every bit of HTML out of PHP and using HTML codes between PHP codes but not necessarily within the PHP code. The second method uses HTML code within the PHP code blocks. For this kind of coding, commands such as echo is used. These are the two ways of embedding HTML in PHP.

Suppose a sentence is to be displayed in a simple HTML code contains a variable called var which contains the string “technical support”. A sample code to show the use of HTML outside of PHP code is as follows:

 

<?php

$var = “technical support”; ?>

<u>I am influenced by <?php echo $var ?></u>

Using the second way to use echo commands to use HTML without going out and coming back into PHP is as follows:

 

<?php

$var = “technical support”;

echo “<u>I am influenced by $var</u>” ?>
The output to both these snippets of code will be: I am influenced by technical support.

What is obtained by using PHP along with basic HTML is that, without any PHP there was no control over the display of static web pages. But by the use of PHP, the same static and boring web page can contain dynamic content which was produced by not putting much effort. The ease of using PHP is such that web content management systems are written in PHP. PHP is also used for developing applications for client-side GUI and command line scripting.

 

Comments are closed.