Confused where to start PHP in HTML and why?

How to switch from HTML to PHP in a file and why.

PHP is used to make your HTML more dynamic. Dynamic would mean data that you would want to get updated by itself or show up by itself without you hard coding in HTML for example: If you change your name or profile on facebook, it would change everywhere, even on your older posts. For instances like these you want to replace a hard coded HTML to a PHP code,

For example see this HTML:

<h3> Hello Natasha,  </h3>

<p>From all of us at Bridge Inc. we want to thank you for your donation, Natasha 
please refer anybody in need of support services to us</p>

This HTML uses the the name Natasha, it is a simple email that the company wants to send out to multiple people and needs to make the code more dynamic, to do this the they will make a variable $name. To create a variable you have to start your <?php tag and end it ?> before you start HTML, Natasha is a string and will go inside quotes.

<?php $name= "Natasha"; ?>

<h3> Hello Natasha,  </h3>
 <p>From all of us at Bridge Inc. we want to thank you for your donation, Natasha 
please refer anybody in need of support services to us</p>

Then you replace all instances where used the name Natasha with the variable $name, you need to use php tags around it as <?php $name  ?>. You also need to use ‘echo’ to actually print the variable out. PHP only prints out data that it is told to print out otherwise the data just stays there. The replacements will now look like <?php echo $name;  ?> . You will also use semi-colon (;) at the end of each statement. Let’s see how it looks:

<?php $name= "Natasha"; ?>

<h3> Hello <?php echo $name;?>,  </h3>
<p>From all of us at Bridge Inc. we want to thank you for your donation, <?php echo $name;?> please
 refer anybody in need of support services to us</p>

Now if the company wants to change the name to Tom, they will only need to change the name variable to have the new name printed everywhere else:

<?php $name= "Tom"; ?>


You can also use HTML inside PHP, You can use this method when you have more PHP codes and little HTML to use in the middle, the above method can still work for you, this is another way to do it. Let us see how I would write the above paragraph if I wanted it all written inside PHP tags, You will open a PHP tag in the start and closing tag in the end of the paragraph. You will have to use the echo statement and quotes around the whole HTML, for the same reason that since we are inside PHP it will not print anything on the screen unless it’s asked to. Notice that you this way you do not use your php tags and echo around $name every time.

<?php $name= "Natasha";

echo "<h3> Hello $name,  </h3>
<p> From all of us at Bridge Inc. we want to thank you for your donation, $name 
please refer anybody in need of support services to us</p>" ; ?>

Double quotes around your statement will read your variables and replace it to the value of the variable but if you use single quotes you will have to use concatenation character “.” around your variables, you also end your quotes around it.

<?php $name = "Natasha";

echo '<h3> Hello '. $name . ',  </h3>
<p> From all of us at Bridge Inc. we want to thank you for your donation, ' . $name . ', 
please refer anybody in need of support services to us</p>' ; ?>

Let us break down what part is HTML and what part is PHP in this statement
Key : HTML  PHP 


 <?php $name = "Natasha";

echo '<h3> Hello '. $name . ',  </h3>
<p> From all of us at Bridge Inc. we want to thank you for your donation, ' . $name . ', 
please refer anybody in need of support services to us</p>' ; ?>