AnyCompiler

Your First PHP Program

A PHP file's code lives inside <?php ... ?> tags — everything outside them is treated as literal text to print verbatim (PHP's templating-language roots showing through), and in a script that's pure PHP from start to finish, like every example in this course, the closing ?> is conventionally left off entirely. There's no compile step and no required entry-point function — no main, no class wrapper — the interpreter just runs top-level statements in order, the moment the file executes.

echo writes one or more comma-separated values to standard output, with no automatic newline — you add \n yourself inside a string wherever you want one. Comments come in two flavors: // (or #) runs to the end of the line, and /* ... */ spans multiple lines.

Run it yourself

This was actually executed to get the output shown below — try changing the text and running it yourself using the button under the example.

<?php

// This is a comment
echo "Hello, PHP!\n";
echo "Learning one line at a time.\n";
Output
Hello, PHP!
Learning one line at a time.

Each echo call printed exactly one line, because each string explicitly ended in \n — leave that off and the next echo's output would land on the same line, with nothing inserted between them. Next: variables and types — the $ sigil, how PHP's dynamic typing actually behaves, and var_dump() and gettype() for inspecting a value's real type.

Practice exercise

Add a comment above your code, then echo "Learning PHP one step at a time." followed by a newline.