Your First PHP Script
Writing, running, and understanding a first PHP script from top to bottom.
2 min read
With PHP installed, the fastest way to learn its shape is to write a small script and watch exactly what happens when it runs.
Hello, World
<?php
echo "Hello, World!";Save this as hello.php and run it directly from the command line:
php hello.phpThis prints Hello, World! straight to your terminal — no web server needed for this. PHP works equally well as a command-line scripting language and as a web server-side language; both use the exact same syntax.
Statements end in semicolons
<?php
echo "First line";
echo "Second line";Every PHP statement ends with a semicolon, similar to JavaScript or C. Forgetting one is one of the most common early syntax errors — PHP will throw a parse error pointing at (often confusingly) the next line, since that's where it realized something was missing.
Variables start with a dollar sign
<?php
$name = "Amara";
$age = 28;
echo "Name: $name, Age: $age";Every PHP variable is prefixed with $ — this isn't optional punctuation, it's part of the variable's actual name. Variables are also case-sensitive ($name and $Name are different variables), and PHP is dynamically typed: you never declare a variable's type up front, it's inferred from whatever value you assign.
Combining PHP with a script's output
<?php
$items = ["apple", "banana", "cherry"];
echo "Shopping list:\n";
foreach ($items as $item) {
echo "- $item\n";
}Shopping list:
- apple
- banana
- cherry
\n inside a double-quoted string represents a newline. This small script already shows PHP's core loop: build up some data, then print (or in a web context, render) something based on it — the same pattern that scales up to a full page generated from a database query.
Where to run PHP as you learn
You don't need a database or a framework to practice PHP's syntax — the command line (php filename.php) is the fastest feedback loop for the fundamentals covered in the next several lessons, before this course introduces web-specific concepts like handling form input.
Test what you just learned
4 quick questions. Get all of them right to unlock the next lesson.
You can take the quiz without an account — logging in just lets your result count toward your progress.