0
0
PHPprogramming~10 mins

PHP tags and embedding in HTML - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - PHP tags and embedding in HTML
Start HTML file
Open PHP tag <?php
Write PHP code
Close PHP tag ?>
Continue HTML or output PHP result
End HTML file
This flow shows how PHP code is embedded inside an HTML file using PHP tags to switch between HTML and PHP.
Execution Sample
PHP
<?php
  $name = "Alice";
?>
<h1>Hello, <?php echo $name; ?>!</h1>
This code sets a PHP variable and then embeds it inside an HTML heading using PHP tags.
Execution Table
StepCode LineActionOutput/State
1<?php $name = "Alice"; ?>PHP starts, variable $name set to 'Alice'No output yet
2<h1>Hello, <?php echo $name; ?>!</h1>HTML starts, PHP tag opens inside HTMLOutput starts with <h1>Hello,
3<?php echo $name; ?>PHP outputs value of $nameOutputs 'Alice' inside HTML
4</h1>HTML closes heading tagOutput completes: <h1>Hello, Alice!</h1>
5End of fileNo more codeExecution ends
💡 Reached end of file, all PHP and HTML processed
Variable Tracker
VariableStartAfter Step 1Final
$nameundefined"Alice""Alice"
Key Moments - 2 Insights
Why do we need <?php and ?> tags inside an HTML file?
Because PHP code must be inside <?php and ?> tags to be recognized and executed by the server, as shown in execution_table steps 1 and 3.
What happens if we forget to close the PHP tag ?> inside HTML?
The server will treat the rest of the file as PHP code, causing errors or no HTML output, as the flow in concept_flow shows the need to close PHP tags before continuing HTML.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what does the PHP code output?
A"<?php echo $name; ?>"
B"Hello"
C"Alice"
D"<h1>"
💡 Hint
Check the 'Output/State' column at step 3 in execution_table
At which step does the variable $name get its value?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column in execution_table where $name is set
If we remove the closing PHP tag ?> after setting $name, what happens?
AHTML after PHP code is treated as PHP, causing errors
BPHP code runs normally and outputs HTML
CVariable $name is not set
DNothing changes
💡 Hint
Refer to key_moments about closing PHP tags and execution flow
Concept Snapshot
PHP code is embedded in HTML using <?php to start and ?> to end PHP blocks.
Variables set in PHP can be output inside HTML using echo.
Always close PHP tags before writing HTML.
PHP code runs on the server and outputs HTML to the browser.
Mixing PHP and HTML lets you create dynamic web pages.
Full Transcript
This example shows how PHP code is embedded inside an HTML file using PHP tags. First, the PHP tag <?php opens and a variable $name is set to 'Alice'. Then the PHP tag closes with ?>. Next, HTML code starts with an h1 heading. Inside the heading, PHP tags open again to echo the value of $name, which outputs 'Alice'. Finally, the heading closes and the file ends. PHP tags tell the server where PHP code starts and ends inside HTML. Without closing PHP tags, the server would treat the rest of the file as PHP code, causing errors. This method lets you mix PHP and HTML to create dynamic content.