Source Code for /public/01-intro/index.php

<?php
// Include the functions.php file, which provides functions we use all over the place:
require('../inc/functions.php');

// Render our HTML header, including this lesson's title
add_header('Lesson 1: Introduction');
?>

<div class="objectives">
  <h2>Objectives</h2>
  <ul>
    <li>Work out whether you're ready to start writing PHP</li>
    <li>Understand the difference between static HTML and PHP-enhanced HTML</li>
    <li>
      Know that when you see a <code>&lt;?php</code> block in a <code>php</code> file, you're looking at PHP code
      that will be run on the server, not be sent to the visitor's browser
    </li>
  </ul>
</div>

<h2>Prerequisites</h2>
<p>
  You will need the following, which it's assumed you've already got:
</p>
<ul>
  <li>A basic understanding of HTML and CSS (a familiarity with JavaScript or another imperative programming language might be helpful)</li>
  <li>A personal website running on a server that supports PHP (ask your host!)</li>
  <li>A text editor (I like <a href="https://www.sublimetext.com/">Sublime Text</a>, but use your favourite!)</li>
</ul>

<h2>PHP's place on the Web</h2>

<h3>Fundamentals of static HTML</h3>
<p>
  Suppose you put a static HTML file on your server called <code>mypage.html</code>. When somebody goes to
  <code>https://yoursite.example.com/mypage.html</code>, the Web server sends a copy of the file to your visitor's browser.
  The browser renders the HTML (turns it from code into something visual), runs any JavaScript, applies any CSS, and
  displays the result.
</p>
<?php begin_code_block('html'); ?>
&lt;p&gt;
  This is my static web page!
&lt;/p&gt;

&lt;p&gt;
  I hope you enjoy my site!
&lt;/p&gt;
<?php end_code_block(); ?>
<p>
  <strong>Everything happens in the visitor's browser.</strong> They can "view source" to see how everything works.
  They can save a copy and edit it for themselves. And nothing they do will affect anybody else who visits your site: everybody
  will always get a copy of the same page with the same content, every single time.
</p>

<h3>How PHP is different</h3>
<p>
  Now suppose you rename that file to <code>mypage.php</code>. When somebody goes to
  <code>https://yoursite.example.com/mypage.html</code>, your Web server does <em>an extra step</em> before sending anything
  to your visitor. It <em>looks through your file</em> to see if it contains any PHP code. If it does, it runs that code,
  <em>replacing it with anything that code outputs</em>. Then it sends the result to the visitor's browser.
</p>
<p>
  Your visitor still gets HTML, JavaScript, and CSS as usual. But <em>before they do</em>, you get the chance for <em>your</em>
  code to run. And because it runs on your server, it can do things that your visitor's browser can't (or that you don't want
  to trust them to do!). You can use PHP to:
</p>
<ul>
  <li>Produce a list of pages on your server and output a list of links to them</li>
  <li>Embed the same header or navigation bar on every page, so you only have to edit it in one place</li>
  <li>Show a page only to users who enter the correct password</li>
  <li>Accept comments or guestbook entries from your visitors and share them with future visitors</li>
  <li>Give visitors a (tamper-proof!) cookie so you recognise them when they come back</li>
  <li>Modify your content based on the time and date, e.g. to schedule content for automatic publication</li>
  <li>Dynamically resize images to produce thumbnails as and when they're needed</li>
  <li>... and much, much more!</li>
</ul>

<h3>How PHP is written</h3>
<p>
  So long as your server is configured to run PHP, you can get started by creating writing a file with a <code>.php</code> extension,
  and writing PHP code inside it.
</p>
<p>
  PHP code has a special syntax to set it apart from regular HTML. A block of PHP code begins with <code>&lt;?php</code> and ends
  with <code>?&gt;</code>. For example:
</p>

<?php begin_code_block('php'); ?>
&lt;p&gt;
  This is my web page with PHP in it!
&lt;/p&gt;

&lt;?php
// 👆 This is the start of a PHP block.

// This is a comment!
// It's inside a PHP block so it won't be sent to the visitor,
// unlike a HTML comment which can be seen with "View Source".

/*
 * This is also a comment.
 * The 'echo' line below is a PHP command that outputs the text
 * "Hello, World!" to the visitor's browser.
 */
echo "Hello, World!";

// 👇 This is the end of the PHP block.
?&gt;

&lt;p&gt;
  I hope you enjoy my site!
&lt;/p&gt;
<?php end_code_block(); ?>

<h3>Developing locally</h3>
<p>
  PHP <em>requires</em> a compatible web server to run it. Unlike HTML, you can't just drag a PHP file to your browser and
  see the result.
</p>
<p>
  If you want to develop and test PHP code locally, you'll need to install a local web server on your own computer. There are
  lots of ways to do this, but for most people the easiest is to install <a href="https://www.apachefriends.org/">XAMPP</a>.
  XAMPP is a free and open-source tool that includes a web server (Apache) preconfigured with PHP capabilities. It also 
  includes lots of other features that are beyond the scope of this class!
</p>
<p>
  <a href="/02-hello-world/">The next lesson</a> includes code that you can use to test that PHP is working correctly, whether
  on your own computer or on your personal website's server.
</p>

<?php
// Render our HTML footer
add_footer();
?>