BeginnerLesson 1 of 16
Hello World & PHP Tags – PHP
Your first PHP script, echo and how PHP works together with HTML.
A PHP file ends in .php. Everything between <?php and the end of the file is executed as PHP. You print text with echo. Every statement ends with a semicolon.
<?php
echo "Hello World!\n";
print("print works too\n");
echo "I'm learning ", "PHP ", 8, "\n"; // echo accepts multiple argumentsThere are three forms of comments.
is only understood as a line break inside double quotes.
<?php
// Single-line comment
# This works too
/* Multi-line
comment */
echo "Line 1\n";
echo 'Line 2\n'; // single quotes: \n stays literalPHP was invented to make HTML dynamic. That is why you can mix PHP and HTML. <?= ... ?> is the short form of <?php echo ... ?>:
<!DOCTYPE html>
<html>
<body>
<h1>Hallo <?= htmlspecialchars("Ada") ?></h1>
<?php if ((int) date("H") < 12): ?>
<p>Guten Morgen!</p>
<?php else: ?>
<p>Guten Tag!</p>
<?php endif; ?>
</body>
</html>TipIn files that contain only PHP, leave out the closing
?>. That way no accidental spaces or line breaks after the code sneak into the output.WarningThe semicolon is mandatory (except after the last statement before
?>). If it is missing, PHP reports a parse error on the line after it.