Bird
Raised Fist0
PHPprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which of the following is the correct way to start a PHP code block inside an HTML file?
easy
A. <?php>
B. <?php
C. <php?
D. <?php/>

Solution

  1. Step 1: Recognize PHP opening tag syntax

    The correct PHP opening tag is <?php without the closing > inside the tag itself.
  2. Step 2: Compare options with correct syntax

    <?php matches the correct opening tag exactly, while others have extra characters or wrong order.
  3. Final Answer:

    <?php -> Option B
  4. Quick Check:

    PHP code starts with <?php [OK]
Hint: PHP code always starts with <?php [OK]
Common Mistakes:
  • Using <php? instead of <?php
  • Adding extra > inside the opening tag
  • Confusing closing tag with opening tag
2. Which of these is the correct way to embed PHP code inside an HTML file?
easy
A. <?php echo 'Hello'; ?>
B. <php echo 'Hello'; ?>
C. <? echo 'Hello'; ?>
D. <?php echo 'Hello' ?>

Solution

  1. Step 1: Identify correct PHP tags and syntax

    The full PHP opening tag is <?php and must be closed with ?>. The statement ends with a semicolon.
  2. Step 2: Check each option for syntax correctness

    <?php echo 'Hello'; ?> uses <?php and closes with ?> and has a semicolon after echo statement, which is correct.
  3. Final Answer:

    <?php echo 'Hello'; ?> -> Option A
  4. Quick Check:

    Full PHP tags with semicolon = correct syntax [OK]
Hint: Use full <?php ... ?> tags with semicolon [OK]
Common Mistakes:
  • Omitting semicolon after PHP statements
  • Using <php instead of <?php
  • Using short tags <? without php (may be disabled)
3. What will be the output of this code snippet embedded in an HTML file?
<html>
<body>
<?php
  echo "Today is ".date('l').".";
?>
</body>
</html>
medium
A. Today is Monday.
B. Today is l.
C. Today is date('l').
D. Syntax error

Solution

  1. Step 1: Understand PHP date function usage

    The date('l') function returns the full name of the current day of the week, e.g., Monday, Tuesday.
  2. Step 2: Analyze echo statement output

    The echo concatenates "Today is " with the day name and a period, so output will be like "Today is Monday." depending on the current day.
  3. Final Answer:

    Today is Monday. -> Option A
  4. Quick Check:

    date('l') returns day name, so output is Today is Monday. [OK]
Hint: date('l') returns day name, output shows full sentence [OK]
Common Mistakes:
  • Treating date('l') as a string literal
  • Missing concatenation operator
  • Expecting syntax error from correct code
4. Identify the error in this PHP embedded in HTML code:
<html>
<body>
<?php
  echo "Hello World"
?>
</body>
</html>
medium
A. Echo statement syntax error
B. Incorrect PHP opening tag
C. Missing semicolon after echo statement
D. Missing closing PHP tag

Solution

  1. Step 1: Check PHP statement syntax

    The echo statement must end with a semicolon ; to separate statements.
  2. Step 2: Verify PHP tags and code completeness

    The opening tag <?php and closing tag ?> are correct and present.
  3. Final Answer:

    Missing semicolon after echo statement -> Option C
  4. Quick Check:

    Statements need semicolon; missing here causes error [OK]
Hint: Always end PHP statements with semicolon [OK]
Common Mistakes:
  • Forgetting semicolon after echo
  • Confusing short tags with full tags
  • Assuming closing tag is optional
5. You want to embed PHP in HTML to display a greeting that changes based on the time of day. Which code snippet correctly uses PHP tags and logic inside HTML?
hard
A. <?php echo if (date('H') < 12) { 'Good morning!'; } else { 'Good afternoon!'; } ?>
B. <? if (date('H') < 12) echo 'Good morning!'; else echo 'Good afternoon!'; ?>
C. <php if (date('H') < 12) { echo 'Good morning!'; } else { echo 'Good afternoon!'; } ?>
D. <?php if (date('H') < 12) { echo 'Good morning!'; } else { echo 'Good afternoon!'; } ?>

Solution

  1. Step 1: Check PHP opening and closing tags

    <?php if (date('H') < 12) { echo 'Good morning!'; } else { echo 'Good afternoon!'; } ?> uses full <?php ... ?> tags correctly. <? if (date('H') < 12) echo 'Good morning!'; else echo 'Good afternoon!'; ?> uses short tags which may be disabled. <php if (date('H') < 12) { echo 'Good morning!'; } else { echo 'Good afternoon!'; } ?> uses <php which is invalid. <?php echo if (date('H') < 12) { 'Good morning!'; } else { 'Good afternoon!'; } ?> misuses echo with if statement.
  2. Step 2: Verify PHP syntax and logic

    <?php if (date('H') < 12) { echo 'Good morning!'; } else { echo 'Good afternoon!'; } ?> correctly uses if-else with echo inside braces. <?php echo if (date('H') < 12) { 'Good morning!'; } else { 'Good afternoon!'; } ?> tries to echo an if statement directly, which is invalid syntax.
  3. Final Answer:

    <?php if (date('H') < 12) { echo 'Good morning!'; } else { echo 'Good afternoon!'; } ?> -> Option D
  4. Quick Check:

    Full PHP tags + proper if-else syntax = correct [OK]
Hint: Use full <?php tags and proper if-else syntax [OK]
Common Mistakes:
  • Using short tags <? which may be disabled
  • Incorrect PHP opening tag like <php
  • Trying to echo an if statement directly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard