03 February 2017

Write PHP code inside HTML file

php logo
To write PHP inside HTML, you need to use the less than and greater than brackets along with question mark with the starting one written 'php'.

For Example:

<div>
<?php 
?>
</div>



To print something inside php, we can use the fuction named echo which is predefined in php for printing.

For Example: 

<div>
<?php  echo "Hello World";
?>
</div>


To use php variable value inside html tag: We have taken a variable with name 'year' and value is set as '2014'. And the value of the variable has been used inside input text box inside the 'value' attribute .
<?php $year = 2014 ; ?> 
Input text:<input type="text" value="<?php echo $year ; ?>"/>

You have to be very careful when using php inside html. With little mistake, you may not get result.

Note: Make sure the file extension ends with .php , as php is scripting language, so it needs a server to execute the php code. 

Learn:  How to write HTML inside php

(If you found this article useful then share with your friends.)

No comments: