Your First Script
PHP scripts can run in two places: in a terminal, with the php command, and on a web server. We'll start in the terminal, where it's easiest to see exactly what a script does. In this lesson you'll write a script, run it, print text and values, and learn to read PHP's error messages.
Hello, World!
Create a folder for your experiments and a file named hello.php in it:
<?php echo "Hello, World!\n";
Open a terminal in that folder and run it:
php hello.php
Hello, World!
What each part means
<?php— the opening tag. Everything after it is PHP code. Anything before it would be output as-is (that's how PHP mixes with HTML, as you'll see in the next lesson).echo— outputs text."Hello, World!\n"— a string.\nis a newline character, so the terminal prompt starts on its own line afterwards.;— ends the statement. Every statement in PHP ends with a semicolon.
A file that contains only PHP code should not end with a closing ?> tag. It's optional, and leaving it out avoids accidentally sending stray blank lines to the browser.
echo and print
echo can output several values separated by commas. print outputs one value. Values are joined into one string with the dot operator ., PHP's string concatenation:
<?php echo "PHP ", "is ", "fun\n"; print "One value at a time\n"; echo "7 times 6 is " . 7 * 6 . "\n"; echo 'Single quotes', "\n";
PHP is fun One value at a time 7 times 6 is 42 Single quotes
In practice almost everyone uses echo. Lesson 16 explains the difference between double and single quotes.
Several statements
Statements run from top to bottom. Here variables (lesson 6) hold values between statements:
<?php $name = "Priya"; $year = 2026; echo "Hello, $name!\n"; echo "Next year is " . ($year + 1) . ".\n"; echo "Your name has " . strlen($name) . " letters.\n";
Hello, Priya! Next year is 2027. Your name has 5 letters.
Inside double quotes, PHP replaces $name with the variable's value. strlen is one of PHP's thousands of built-in functions.
Looking inside values
echo is for output meant for users. When you want to see exactly what a value is while learning or debugging, use var_dump — it shows the type as well as the value — or print_r for a more compact view of arrays:
<?php var_dump(42); var_dump("42"); var_dump(3.5, true); print_r(["apple", "banana"]);
int(42)
string(2) "42"
float(3.5)
bool(true)
Array
(
[0] => apple
[1] => banana
)When something goes wrong
If the code can't be understood, PHP reports a parse error and runs nothing. Forgetting a semicolon:
<?php echo "first line\n"; echo "second line\n" echo "third line\n";
Parse error: syntax error, unexpected token "echo", expecting "," or ";" in main.php on line 5
Depending on your php.ini, the terminal may show each message twice — once starting with PHP (written to the error log, which is the terminal for command-line scripts) and once without. Read it as what went wrong, in which file, on which line. PHP reports line 5 because that's where it noticed the problem — the missing semicolon is at the end of line 4. When an error points at a line that looks fine, check the line before it.
Other problems are only discovered while the script runs. A warning is reported, and the script continues:
<?php echo "Hello, $nmae!\n"; echo "Still running.\n";
Warning: Undefined variable $nmae in main.php on line 3 Hello, ! Still running.
A typo in a variable name doesn't stop PHP, it just produces an empty value. Always read warnings — they usually point straight at a bug.
Quick experiments
For one-liners you don't need a file at all. php -r runs code given on the command line, and php -a starts an interactive shell:
php -r 'echo strtoupper("quick test"), "\n";'
QUICK TEST
Try it yourself
Change the name and the year of birth, and add a line that prints your age in months.
Hello, Sam! You are about 28 years old. That's roughly 10220 days.