Source Code for /public/02-hello-world/index.php
<?php
require('../inc/functions.php');
add_header('Lesson 2: Hello World');
?>
<div class="objectives">
<h2>Objectives</h2>
<ul>
<li>Test that PHP is working on your server</li>
<li>Open and close PHP blocks with <code><?php</code> and <code>?></code></li>
<li>Use the <a href="https://www.php.net/manual/en/function.echo.php"><code>echo</code></a> command to print out text</li>
<li>Use the <a href="https://www.php.net/manual/en/function.strrev.php"><code>strrev</code></a> function to reverse a string</li>
<li>Use the <a href="https://www.php.net/manual/en/function.date.php"><code>date</code></a> function to get the current date</li>
<li>Use an <code>if</code> statement to do something conditionally</li>
</ul>
</div>
<h2 id="strings-both-ways">Hello World</h2>
<p>
Find an out-of-the-way place on your webserver and create a file called <code>hello-world.php</code>.
Inside it, put the following code:
</p>
<?php code_block_from_file( '02-hello-world/hello-world.php', 'php' ); ?>
<p>
Visit your new page in your browser, e.g. at <code>http://yoursite.example.com/hello-world.php</code>.
You should see the text "HELLO WORLD backwards is DLROW OLLEH".
</p>
<p>
If so: hurrah! PHP is working!
(<a href="/troubleshooting/php-not-working/">Help! PHP isn't working!</a>)
</p>
<h3>What did I just create?</h3>
<p>
The code you wrote did the following:
</p>
<ol>
<li>Began a PHP block with <code><?php</code> (we didn't bother ending the block, and that's fine)</li>
<li>Used PHP's <code>echo</code> command to print out the text "HELLO WORLD backwards is "</li>
<li>Used PHP's <code>strrev</code> function to reverse the string "HELLO WORLD"</li>
<li>Used PHP's <code>echo</code> command to print out the reversed string</li>
</ol>
<p>
Notice how every PHP command ends with a semicolon (<code>;</code>). If you omit a semicolon, PHP will throw an error.
</p>
<h2 id="today">What day is it?</h2>
<p>
Let's tell our visitor what day it is. Create a new file called <code>today.php</code> with the following code:
</p>
<?php code_block_from_file( '02-hello-world/today.php', 'html' ); ?>
<p>
Visit that page and you should be told what day of the week it is. PHP's <code>date</code> function can return
the date in a variety of formats. The <code>l</code> means "day of the week" (e.g. Monday, Tuesday...).
Why not try <a href="https://www.php.net/manual/en/datetime.format.php">some other letters</a> to show
the date in a different format?
</p>
<p>
Notice how we can use PHP blocks "inline" with the <code><?php ... ?></code> tags all on one line. This can
be a tidy way to write simple code like <code>echo</code> commands.
</p>
<p class="tip">
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 <code><?php echo date('e'); ?></code>
and it'll tell you!
</p>
<h2 id="conditional">Closed on Mondays!</h2>
<p>
You could use this to close a page to visitors on Mondays. Here's <code>closed-on-mondays.php</code>:
</p>
<?php code_block_from_file( '02-hello-world/closed-on-mondays.php', 'html' ); ?>
<p>
See how we can mix-and-match between HTML and PHP code in the same file. Only the HTML (and anything
<code>echo</code>d by PHP) will be sent to the browser.
</p>
<p>
In this example, we use an <code>if</code> statement to check if today is Monday. If it is, the code inside the
<code>if</code> block's <code>{ ... }</code> is executed. That code sets the HTTP status code to 403 (which means
"Forbidden"), prints a message, and then runs the <code>die();</code> command to stop anything else being done.
</p>
<?php
add_footer();
?>