Beginner Fundamentals

Installing and Running PHP

To start coding in PHP you need the PHP interpreter installed on your machine. Once installed, you can run scripts from the command line or serve them through a web server.

Installing PHP

  • Windows: download from the official site or use a bundle like XAMPP.
  • macOS: install with Homebrew using brew install php.
  • Linux: use your package manager, for example sudo apt install php.

Check the installation with:

php --version

Running from the command line

Save your code in a file and run it with the php command.

php script.php

This executes the file and prints the output in the terminal.

Embedding PHP in HTML

PHP can be mixed with HTML. Everything outside the PHP tags is sent as-is.

<!DOCTYPE html>
<html>
<body>
    <h1>My Page</h1>
    <p><?php echo "Generated by PHP"; ?></p>
</body>
</html>

Built-in server

For quick testing, PHP includes a local server.

php -S localhost:8000

Open http://localhost:8000 in the browser to see your pages.