Skip to content
Hello, World
step 1/5

Reading — step 1 of 5

Learn

~1 min readGetting Started

PHP code lives inside <?php ... ?> tags. Outside the tags, content is treated as raw output — that is why PHP can be embedded in HTML templates.

The closing tag is optional — and in pure-PHP files you should leave it out entirely. Open with <?php, then just stop writing at the end of the file. No ?>, no ?, no ?php>. Omitting it is the modern convention (PSR-12) because any stray space or newline after ?> gets sent as output and breaks headers. If you do close it, the only correct spelling is ?> — a ? or ?php> on its own is a parse error.

<?php
echo "Hello, World!\n";
echo "Ship That Code", "\n";   // multiple args
print("Also works\n");

echo is the standard output builtin. It accepts multiple comma-separated args. print is similar but is technically an expression that returns 1.

Statements end with ;. Variable names start with $. PHP is dynamically typed but supports type declarations on function signatures and (since 7.4) on properties.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…