Why should we use PHP and what are the benefits of using PHP.Also how dynamic html can be generated by embedding html inside php constructs. PHP is a powerful language and can be used to render very dynamic html. The term dynamic html implies that html code which may vary depending on various conditions.You will see that the power of php is that the html written inside the php code can be made very dynamic by including the html in programming constructs like if loops etc.
Let us assume that we have the php file test.php.The following code explains the usage of php and html.
You may save this code in test.php and try and see the output your self.
Keeping html and php separate
<html> <h1>This is html outside any Php</h1> </html> <?php // we begin php to give a value to a variable // get today's day $today = date("D"); //$message is blank to start with $message=""; switch ($today) { case 'Mon': $message="Today Is Monday !"; break; case 'Tue': $message="Today Is Tuesday !"; break; case 'Wed': $message="Today Is Wednesday !"; break; case 'Thu': $message="Today Is Thursday !"; break; case 'Fri': $message="Today Is Friday !"; break; case 'Sat': $message="Today Is Saturday !"; break; case 'Fri': $message="Today Is Sunday !"; break; default: $message=""; } // and we end the php code ?> <html> <h1>This is again html outside php.But now the following html will be dynamic !</h1> <h1>The following line contains dynamic php inside html construct</h1> <h1><?php echo $message ?> !</h1>
Now we mix html inside php constructs
<?php $today=date("D"); if (stristr($today,'Mon')) { ?><h1>Today is Monday !</h1> <?php } else if (stristr($today,'Tue')) { ?><h1>Today is Tuesday !</h1> <?php } else if (stristr($today,'Wed')) { ?><h1>Today is Wednesday !</h1> <?php } else if (stristr($today,'Thu')) { ?><h1>Today is Thursday !</h1> <?php } else if (stristr($today,'Fri')) { ?><h1>Today is Friday !</h1> <?php } else if (stristr($today,'Sat')) { ?><h1>Today is Saturday !</h1> <?php } else if (stristr($today,'Sun')) { ?><h1>Today is Sunday !</h1> <?php } // ending php finally ?>
You should note that how html is being put inside programming constructs of php. PHP can be closed any where, html code can come in and thereafter php can resume. The code does not break !
Another way to play around with html in php
<?php $html_string = '<p style="width:400px;border:solid 1px;border-color:#000000;padding:10px 10px 10px 10px ;background-color:#EBF2BC;">This is a multiple line html string with a <a href="https://techarticleshub.in">test link</a> to techarticleshub.in.<br/>Also as you can see this paragraph also has a style<br/>And the beauty is that all this is a PHP string<br/></p>'; echo $html_string ; echo '<p>Again Direct Echo - This is an html string with a <a href="https://techarticleshub.in">test link</a> to techarticleshub.in.<br/></p>'; ?>
In the above code you can see that the beauty of using the single quote,i.e ‘ , to embedd the string containg the html is that you do not need to use any escape sequences “\” for double quotes ” inside the string. so html can be stored in a string easily.
So I attempted to explain the basics of why do we use php and how to play around with html in php.
That is all.
If you liked this article please do leave a reply and share it with friends.
Thanks.