Lesson 1 of 8

Hello, PHP

Write your first PHP script and a page that mixes with PHP code in <?php ?> tags. The server runs the code, and the browser only sees 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 starts with $, so variables are easy to spot. Double-quoted 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>
Finished reading?Mark it done to track your progress.

Tip: use the ← and → keys to move between lessons.