Step 1 of 5 · Reading · ~4 min
Learn
Getting Started
Your First PHP Program
Most languages start executing at the top of the file. PHP does something else: it copies the file to the output and only runs the parts you explicitly mark as code. That single design decision — PHP began life as a way to sprinkle logic into HTML pages — explains almost everything that looks unusual about the language.
Why is there an opening tag at all?
Because a .php file is a template. Everything outside <?php ... ?> is sent to the output untouched; everything inside is executed.
Shipping label
<?php
echo "Order #1042\n";
?>
Thank you
//> Shipping label
//> Order #1042
//> Thank you
Should I write the closing tag?
In a file that is only PHP — which is every exercise in this course — you open with <?php and simply stop writing at the end. No closing tag at all.
| Form | What happens |
|---|---|
✓ <?php … end of file | Nothing can leak out after your code |
✗ <?php … ?> plus a stray blank line | That blank line becomes output and breaks HTTP headers |
✗ ? or ?php> on its own | Parse error — neither of those is a tag |
Leaving ?> off is the modern convention (PSR-12). If you do close the block, ?> is the only correct spelling.
How do I print something?
<?php
echo "Hello, World!\n";
echo "Ship", " ", "That", " ", "Code", "\n"; // echo takes several arguments
print("print works too\n"); // print takes exactly one
printf("%s has %d lessons\n", "PHP Fundamentals", 15);
//> Hello, World!
//> Ship That Code
//> print works too
//> PHP Fundamentals has 15 lessons
echo is the one you will reach for. It is a language construct rather than a function: it accepts a comma-separated list and hands back nothing. print is an expression that always evaluates to 1, which is why it takes only a single argument.
echo never adds a line break. Nothing in PHP does. Every newline you want in the output, you write yourself as \n.
Three rules that never change
<?php
// a comment runs to the end of the line
# this is also a comment
/* and this one
spans lines */
$lessons = 15;
echo "Lessons: $lessons\n";
//> Lessons: 15
- Every statement ends with
;. - Every variable name begins with a dollar sign —
$lessons,$total. - PHP is dynamically typed, but you can declare types on function signatures and (since 7.4) on class properties. Both come later in this course.
What the grader actually does
It saves your code to a file, runs it with the PHP 7.4 command-line interpreter, and compares everything your program wrote to standard output against the expected text. The comparison is exact everywhere except at the very end, where whitespace trailing the whole output is trimmed from both sides first. So no leading spaces, no trailing labels, no Result: prefixes — but a missing final line break is forgiven. Line breaks between lines are not: they sit inside the text and are compared like any other character.
Knowing the version matters. Features added in PHP 8 — match, str_contains, constructor property promotion — are parse errors here. Each lesson flags them where they come up, so you learn what modern PHP looks like without writing code the grader rejects.
Your exercise
First Print wants exactly one line of output: Hello, PHP! — nothing before it, and a newline after it. There is no input to read.
Write the "\n" at the end anyway — every later exercise prints more than one line, and there a missing break really does fail. The mistake the grader catches here is spelling: PHP is three capitals, Hello has one, and the comma and the exclamation mark are both part of the string. Put the whole thing inside one pair of double quotes, end the statement with a semicolon, and you are done.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…