Skip to Content
Course content

2: Embedding PHP in HTML

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I see this constantly with developers moving from frontend frameworks or JavaScript: the assumption that PHP is just another script that runs inside the browser. They try to put PHP inside an HTML attribute, like an onclick handler, expecting it to fire when a user clicks a button. It doesn't work, and it's incredibly frustrating until you realize why.

The "Client-Side" Delusion

Let's look at a piece of code that represents this mistake. Imagine you're trying to create a simple greeting that changes based on a user's name.

<button onclick="alert('Hello, <?php echo $username; ?>!')">
  Click Me!
</button>

At first glance, this looks logical. But here is the reality: by the time the user's browser ever sees that button, the PHP has already finished running on the server. The browser receives pure HTML. If the server processes that code, it sends this to the browser:

<button onclick="alert('Hello, Alice!')">
  Click Me!
</button>

Wait, that worked! So where is the misconception? The problem arises when you think you can use PHP to react to things happening after the page has loaded. You cannot use PHP to detect a mouse movement or a keypress in real-time. PHP is the chef in the kitchen; once the plate (the HTML) is sent out to the table (the browser), the chef can't change the ingredients on the fly. If you want the page to change without a refresh, you need JavaScript. PHP's job is to build the plate before it leaves the kitchen.

Blending Logic with Layout

The real power of PHP is its ability to "drop in" and "drop out" of HTML. You aren't writing a PHP script that happens to output HTML; you're writing an HTML page that has "holes" filled by PHP.

I usually recommend keeping your PHP blocks as small as possible when you're embedding them. If you find yourself writing twenty lines of PHP inside an HTML <div>, you're doing it wrong. Instead, use PHP to handle the logic and then "exit" PHP to write your HTML.

<div class="user-profile">
  <h2>User Profile</h2>
  
  <?php if ($is_premium_member): ?>
    <span class="badge">Premium Member</span>
  <?php else: ?>
    <span class="badge">Free Account</span>
  <?php endif; ?>

  <p>Welcome back, <?php echo $username; ?>!</p>
</div>

Notice how I used the if: ... endif; syntax instead of curly braces { }. In my experience, this "alternative syntax" is much cleaner when mixing languages. It prevents the "bracket nightmare" where you're scrolling up and down trying to figure out which closing brace belongs to which loop.

Also, a pro tip: if you are just printing a single variable, don't waste time with <?php echo $var; ?>. Use the shorthand echo tag: <?= $var ?>. It's shorter, it's standard, and it makes your HTML templates much more readable.




📋 Practical Task

Build a Dynamic Product Inventory Card

You are building a product display page. You have been given the following variables (assume these are already defined at the top of your file):

  • $productName = "Mechanical Keyboard";
  • $price = 129.99;
  • $stockCount = 3;
  • $onSale = true;

Your Goal: Create an HTML structure for a product card that uses PHP to dynamically change based on the data. Your solution must include:

  1. The product name and price displayed using the shorthand echo tag <?= ?>.
  2. A conditional check: If $onSale is true, display a <span> with the text "SALE!".
  3. A conditional check for stock:
    • If $stockCount is 0, display "Out of Stock" in red text.
    • If $stockCount is between 1 and 5, display "Low Stock - Only [number] left!" in orange text.
    • Otherwise, display "In Stock".

Ensure you use the alternative if: ... else: ... endif; syntax to keep your HTML clean.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.