PhPHP · Lesson 1 of 8

Hello, PHP

PHP's superpower and its quirk in one: PHP files are HTML files with code embedded in <?php ?> tags. The server runs the code, the browser sees only the result.

PHP
<?php
// hello.php — run with: php hello.php
echo "Hello, World!\n";

$name = "Ada";              // variables start with $
$age  = 17;
echo "I'm $name, age $age\n";   // variables interpolate in "..."
echo 'No interpolation in single quotes: $name' . "\n";
// . is string concatenation

Every variable starts with $ — love it or hate it, you always know what's a variable. Double-quoted strings interpolate variables; single-quoted strings are literal. Statements end with semicolons.

PHP
<!-- index.php — HTML with embedded PHP.
     Serve with: php -S localhost:8000 -->
<!DOCTYPE html>
<html>
  <body>
    <h1>Today is <?php echo date('l'); ?></h1>

    <?php if (date('G') < 12): ?>
      <p>Good morning!</p>
    <?php else: ?>
      <p>Good afternoon!</p>
    <?php endif; ?>
  </body>
</html>
◆ Note
This embed-in-HTML model is why PHP conquered the web in the 2000s: rename page.html to page.php, sprinkle in dynamic bits, done. Modern PHP apps keep logic in pure-PHP files and use templates for HTML, but the model remains request → PHP runs → HTML out.