Lesson 2: Hello World
Objectives
Hello World
Find an out-of-the-way place on your webserver and create a file called hello-world.php.
Inside it, put the following code:
<?php
echo 'HELLO WORLD backwards is ';
echo strrev( 'HELLO WORLD' );
Visit your new page in your browser, e.g. at http://yoursite.example.com/hello-world.php.
You should see the text "HELLO WORLD backwards is DLROW OLLEH".
If so: hurrah! PHP is working! (Help! PHP isn't working!)
What did I just create?
The code you wrote did the following:
- Began a PHP block with
<?php(we didn't bother ending the block, and that's fine) - Used PHP's
echocommand to print out the text "HELLO WORLD backwards is " - Used PHP's
strrevfunction to reverse the string "HELLO WORLD" - Used PHP's
echocommand to print out the reversed string
Notice how every PHP command ends with a semicolon (;). If you omit a semicolon, PHP will throw an error.
What day is it?
Let's tell our visitor what day it is. Create a new file called today.php with the following code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>What day is it?</title>
</head>
<body>
<h1>Happy <?php echo date('l'); ?>!</h1>
<p>
Today is <?php echo date('l'); ?>. That's my favourite day of the week!
</p>
</body>
</html>
Visit that page and you should be told what day of the week it is. PHP's date function can return
the date in a variety of formats. The l means "day of the week" (e.g. Monday, Tuesday...).
Why not try some other letters to show
the date in a different format?
Notice how we can use PHP blocks "inline" with the <?php ... ?> tags all on one line. This can
be a tidy way to write simple code like echo commands.
The time comes from your server. If your server is in a different timezone to you, you might see a
different time than you expect! Don't know what timezone your server is in? Try <?php echo date('e'); ?>
and it'll tell you!
Closed on Mondays!
You could use this to close a page to visitors on Mondays. Here's closed-on-mondays.php:
<?php
/*
* date('w') returns the day of the week as a number:
* 0 is Sunday, 1 is Monday ... 6 is Saturday etc.
*/
if( date('w') === '1' ) {
/*
* This code tells the browser to show a "403" error page.
* 403 is the error code for "Forbidden"!
*/
header('HTTP/1.1 403 Forbidden');
echo "This page can't be read on Mondays";
/*
* The die(); command stops the PHP script immediately!
* No code after this will be run or sent to the browser.
*/
die();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>The Non-Monday Page</title>
</head>
<body>
<h1>It's not a Monday</h1>
<p>
It's not a Monday, so you can read this page! Lucky you!
</p>
</body>
</html>
See how we can mix-and-match between HTML and PHP code in the same file. Only the HTML (and anything
echod by PHP) will be sent to the browser.
In this example, we use an if statement to check if today is Monday. If it is, the code inside the
if block's { ... } is executed. That code sets the HTTP status code to 403 (which means
"Forbidden"), prints a message, and then runs the die(); command to stop anything else being done.