Current practice: PHP templates can move in and out of PHP mode freely. Prefer clear template markup and escape dynamic values at the point where they are rendered.
Current example: mix PHP and HTML safely
<?php$title='Account';$safeTitle=htmlspecialchars($title, ENT_QUOTES,'UTF-8');?><h2><?=$safeTitle?></h2><p>This markup is ordinary HTML outside the PHP block.</p>
We can integrate HTML code inside PHP script to display the pages in web browser after proper formatting. After all our browser does not understand PHP and it can only understand HTML tags. So to Keep HTML tags inside PHP script we need to make it compatible to PHP. This is very much required when we have to include PHP script and HTML both in same page.
There are two ways to do this. One way is to echo or print the html tags inside the PHP code and the
second way is to close the PHP script execution area and return to HTML mode to display HTML tags.
Displaying HTML tags outside PHP script tags
Without keeping both together we can keep both in different areas so PHP engine does not come across HTML tags. However this is not practicable and is not a good idea for developing complex applications. Here is a sample code for this.
<?Php// PHP script area ?><br> this is " our HTML area " we can use any tags or chars as we like "
By printing the same HTML code from inside the PHP code we can get same result and the same rendered result while keeping the template logic explicit.
But in this case we have to take care some points as double quotes inside echo
command must be quoted consistently or escaped correctly. The code below will generate error as we have kept double quotes inside the PHP script area
<?Phpecho"<table width="100%" border='0' cellspacing='1'
cellpadding='0'>";// Double quotes inside this line not allowed.?>
There are two ways to solve this . One is to use single quotes in place of double quotes or to use PHP escape command ( \ ) to tell PHP not to consider the
character next to it while evaluating. Here is the correct code using escape
command.
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery,
and web development at plus2net. The tutorials focus on clear explanations, working
examples, and code that readers can test and adapt while learning.