The echo statement must end with a semicolon ; to separate statements.
Step 2: Verify PHP tags and code completeness
The opening tag <?php and closing tag ?> are correct and present.
Final Answer:
Missing semicolon after echo statement -> Option C
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
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.
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.
Final Answer:
<?php if (date('H') < 12) { echo 'Good morning!'; } else { echo 'Good afternoon!'; } ?> -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.