Your first PHP script
The opening tag that makes a file PHP, echo as the way to say something, and the dot that joins pieces of text together.
What PHP is for
PHP is a language for dynamically building web pages — a request arrives, a script runs, and what the script writes becomes the response. The language is old, unglamorous and everywhere: this year it still powers most of the web, including WordPress, Wikipedia and half of the shops you have bought from.
Here the script runs on its own and you read its output directly, instead of a server sending it to a browser. Everything you learn — variables, functions, arrays, classes — is exactly the same PHP that builds those pages; only the destination of the text changes.
In a browser the same script would run behind a web server, and its output would become the body of an HTTP response. The server decides when to run it, the script writes its text, and the browser receives whatever was written — no HTML file is involved unless the script prints one. Running it here simply removes the request and shows you the stream directly.
The tag, and echo
A PHP file starts with <?php. Anything outside the tag is printed as it stands — that is how PHP started, as a templating language with escapes into code — so the opening tag is where the interesting part begins. A short script needs no closing tag at all; leaving it out avoids a classic bug where a stray blank line after it becomes output.
echo writes text: echo "Hello"; sends those characters out. It takes one piece or several separated by commas, and it is a language construct rather than a function, so it never needs parentheses.
Where the text goes
echo does not draw on the screen; it appends to an output buffer. PHP collects that text and sends it when the script finishes, or sooner if the buffer is flushed. That is why a warning printed halfway through a page can appear above content that was echoed later, and why a stray space before <?php shows up at the very top of the response.
The buffer is also why output has to come before certain other work. A script that wants to send an HTTP header must do it before anything is echoed, because once the buffer has been flushed the headers are already on their way. In a command-line script there are no headers, but the same discipline — decide, then print — keeps a script usable in both places.
Values, and joining them
A variable starts with a dollar sign: $name = "Ada";. In PHP the dot joins strings — "Hello, " . $name — and a double-quoted string can interpolate a variable directly: "Hello, $name". Two ways to say the same thing, and the language will not choose for you.
Output is a stream of characters with no line breaks of its own: two echoes run together unless you write a separator yourself. PHP_EOL is PHP's name for the right end-of-line for the system it runs on, which is both correct and portable.
Echo, variables, and the dot
<?php
// echo writes text out — several pieces can be given at once.
echo 'Hello, world!', PHP_EOL;
// Variables start with $ and take the type of what you assign.
$name = 'Ada';
$visits = 3;
// The dot joins strings …
echo 'Welcome back, ' . $name . '!' . PHP_EOL;
// … and a double-quoted string can use $name directly.
echo "This is visit number $visits." . PHP_EOL;
// Single quotes are literal: $name stays three characters and a dollar sign.
echo 'Single quotes keep $name as text.' . PHP_EOL;Run it, then make it yours
Press Run and read the output. Then add a variable of your own, print it with punctuation of your own, and change the greeting to use double quotes with the variable inside them.
The first run downloads the PHP runtime, so it takes a little longer than the ones after it. Run is disabled while it arrives — nothing happens behind your back.
The file is read in order
The interpreter starts at the top of the file and works down. Text outside <?php ... ?> is copied to the output exactly as written; inside the tag, each statement is executed and its output is appended. That is why a blank line before the opening tag can appear in a response, and why the closing tag is optional in a file that is all PHP.
A statement ends at a semicolon, not at the end of a line. PHP does not care how many lines an echo spans, which means a missing semicolon is reported on the following statement — the parser only notices something is wrong when the next line does not fit. Read the reported line, then look at the line above it.
Three habits prevent most first-script errors: end every statement with ;, keep the $ on every variable, and leave out the closing ?> in a file that is pure PHP. A missing semicolon usually points at the next line, a bare name is treated as a constant, and whitespace after ?> is sent as output before anything else.
Try these next
- Print three lines with one
echoand commas, then the same three with three separateechostatements. - Put a variable inside double quotes, then inside single quotes, and compare the two lines.
- Build an order slip that prints an item, a quantity, a unit price and a total.
An order slip
Run the slip, then change the customer and quantity. Add a second item and print a combined total.
Check what you learned
One question at a time. A wrong answer explains itself — take another go.
Part 1 of 5
What does the opening <?php tell the interpreter?
