All lessons

Your First Program

5 min · PHP

Write and run your very first PHP program, right here on the page.

PHP · Your First Program0/5 cleared

Enemy 1 of 5

How a PHP script runs

Every chunk of PHP logic has to be wrapped in a <?php tag. That's not decoration — it's the signal that tells the interpreter "treat everything from here on as executable code, not plain text." PHP was originally built to live inside HTML pages, so the language needs an explicit switch to flip between "just print this text" mode and "run this code" mode. Outside of <?php ... ?>, anything in a .php file is sent straight to the output untouched, which is why forgetting the opening tag is a classic first bug: your code shows up as literal text on the page instead of running.

echo is PHP's basic tool for writing text to the output. It's technically a "language construct" rather than a function, which is why you can write echo "Hello"; without parentheses — something that would look unusual in a language like JavaScript or Python. Whatever string you hand it gets printed exactly as written, character for character.

Look closely at the \n inside the string. On screen it's a backslash followed by the letter n, but PHP reads it as a single special character: a newline. Without it, everything you echo lands on the same line, because echo — unlike some languages' print functions — never adds line breaks automatically.

Also notice the semicolon at the end of the line. Every PHP statement needs one, and a missing semicolon is probably the single most common beginner mistake. The error PHP reports usually points at the line *after* the mistake, so when you see a confusing parse error, get in the habit of checking the line above it first.

You
Binary Treant

Loading editor...

Ln 1, Col 1Spaces: 4
Sign up to fightTarget output: Hello, World!