How this hello world example works
Every PHP script starts with the opening tag <?php. echo sends text to the output, and the statement ends with a semicolon. The closing ?> tag is optional and is usually left out in files that contain only PHP, which avoids accidental whitespace being sent after it.
echo vs print
Both output text. echo accepts several comma-separated arguments and returns nothing; print takes one argument and returns 1, so it can be used in expressions. In practice echo is used almost everywhere.
<?php
echo "Hello", ", ", "World!", "\n";
print "Hello from print\n";
$name = "PHP";
echo "Hello, $name!\n"; // variables are interpolated in double quotes
echo 'Hello, $name!' . "\n"; // but not in single quotes
Output:
Hello, World! Hello from print Hello, PHP! Hello, $name!
Hello World inside an HTML page
Saved as index.php on a web server, PHP runs the code between the tags and sends the rest of the file as HTML. <?= ... ?> is short for <?php echo ... ?>.
<!DOCTYPE html>
<html>
<body>
<h1><?= "Hello, World!" ?></h1>
<p>Today is <?php echo date("l"); ?>.</p>
</body>
</html>
Run hello.php on your computer
With PHP installed, save the program as hello.php and run php hello.php in a terminal, or start the built-in server with php -S localhost:8000 and open http://localhost:8000/hello.php. No installation? Press Run above: the code executes with real PHP compiled to WebAssembly in your browser.