Introduction

PHP is a server-side scripting language: it runs on a web server, produces plain HTML (or JSON, or whatever else you tell it to), and sends the result to a browser that never sees a single line of PHP code.

Server-side versus client-side

Everything in the HTML, CSS, and JavaScript courses on this site runs in the browser — a visitor's machine does the work. PHP is the opposite: it runs on the server, before the page ever reaches anyone. A visitor requests page.php, the server runs the PHP code inside it, and only the finished HTML output travels over the network. Open "View Source" on a PHP-powered page and you'll never see the PHP itself — just whatever HTML it produced.

PHP tags and a first script

PHP code lives inside <?php ?> tags, which can be dropped into an otherwise ordinary HTML file. The echo keyword sends text to the output:

PHP hello.php
<?php
echo "Hello, world!";
?>
Output
Hello, world!

Every statement ends with a semicolon, same as JavaScript or C. The closing ?> tag is technically optional at the end of a file that's pure PHP — many real-world files leave it off entirely to avoid a stray blank line leaking into the output — but you'll see it included for clarity through the early lessons here.

Mixing PHP with HTML

Because a PHP file is really an HTML file that happens to have some server-side instructions embedded in it, you can jump in and out of <?php ?> tags freely, right in the middle of markup:

PHP greeting.php
<!DOCTYPE html>
<html>
<body>
  <h1>
    <?php echo "Welcome to the site"; ?>
  </h1>
  <p>Today's date, rendered by the server, is <?php echo "March 4th"; ?>.</p>
</body>
</html>
HTML sent to the browser
<!DOCTYPE html>
<html>
<body>
  <h1>
    Welcome to the site
  </h1>
  <p>Today's date, rendered by the server, is March 4th.</p>
</body>
</html>

Notice what the browser actually receives: no <?php, no echo — just plain HTML with the dynamic pieces already filled in. That substitution happened entirely on the server before the response was sent.

Comments

PHP supports both C-style comments and shell-style comments — pick whichever reads more naturally:

PHP comments.php
<?php
// a single-line comment
# also a single-line comment
/* a
   multi-line comment */
echo "Comments never appear in the output";
?>
Output
Comments never appear in the output
Note: to actually run any of this, you need PHP installed and either a local dev server (php -S localhost:8000 from a terminal, then visiting the page in a browser) or a full stack like XAMPP/MAMP. Double-clicking a .php file and opening it directly in a browser will just show you the raw source — nothing runs without a PHP interpreter sitting between the file and the browser.