HTML is important part of website . I want to execute HTML code inside PHP code without echo. It's easy to echo HTML code but when we have lots of HTML code and want use inside PHP loop , condition or any other PHP code then it's hard to echo HTML code . I know this is very basic question of PHP but I want to know every single method to run HTML code inside PHP code without echo HTML code . How I can use HTML and CSS code inside PHP code without echo ?
I share my experience. I was trying to create a website with HTML and CSS and I was echo block of HTML containg PHP variable . I haven't heard about that we can directly execute HTML code inside PHP after closing PHP . It was hard to echo HTML code but after that I got a solution to use HTML code inside PHP block of code .
Yes , it's possible to execute block of HTML code inside PHP without echo any HTML block also you can place your PHP variable in HTML code .
Let's see an example
<?php
If(!isset($var)){
?>
<div class="css_class">
Variable is not set . It's your HTML code
</div>
<p>This is HTML paragraph .In this way, You can add block of code inside PHP without echo </p>
<?php
}
?>
The HTML execution process can be done by separating by PHP opening <?php and closing ?> tags . If you want to run HTML code without echo then you should separate HTML code.
Example 1-
<?php
//PHP code here
?>
<h2> This is HTML heading <?php echo $var; ?></h2>
<?php
//PHP code here
?>
You can use HTML code if you separate HTML and PHP code inside PHP tags(<?php ?> ).
Example 2 -
<?php
$i=1995;
while($i<=2001)
{
?>
<h2>Year:-<?php echo $i;?></h2>
<?php
$i++;
}
Output -
Year:-1995
Year:-1996
Year:-1997
Year:-1998
Year:-1999
Year:-2000
Year:-2001
In this example, we are displaying years from 1995 to 2001 and separating HTML code without echo inside PHP code.
HTML code can be executed by including a file.
Create a file like - myfile.php
myfile.php
<h2>This is HTML code. </h2>
<p>This is paragraph. </p>
Now, include() file in PHP .
<?php
include('myfile.php');
$var='Hello';
?>
In these ways , you can execute HTML code inside PHP without echo.
