003 Hello World and Caveats
Before we get into all the details of learning how to program using PHP, it is important that you know how to actually run your programs in PHP.
Running your first PHP program
The PHP executable
The PHP executable is what parses and runs your PHP code. In a typical internet interaction, your browser (Chrome, FireFox, Safari, etc.) makes a request to the web server (Apache, nginx, etc.). The web server looks at the request, and if it asks for PHP, it sends the request to PHP to handle and then return a response. Once the response comes back from PHP, the web server sends it on to the browser and you see the web page on your screen.
The PHP interpreter is very smart and very fast. It allows you to mix PHP code into an HTML file.
CLI versus Web Server - Caveats
You do not need the web server to run PHP. You do need it if you want to run it for a web site. But to learn the basics of PHP, we are going to work with the PHP interpreter directly.
The code:
Create a file named hello.php
and fill it with the following:
<?php
echo 'Hello, world!';
?>
Linux
From within a terminal, make sure you are in the same directory as the hello.php
file, and enter:
$ php hello.php
Mac
From within a terminal, make sure you are in the same directory as the hello.php
file, and enter:
$ php hello.php
Windows
Assuming you have a web server like Apache (from XAMPP) running, and the hello.php file is in the default webroot, go to your browser, type in http://localhost/hello.php
OUTPUT
Congratulations, you have executed your first PHP code.
The output for Linux and Mac will be similar: "Hello, world!" will appear on the screen. In Windows, "Hello, world!" should appear in the browser. If nothing happens, go back and make sure PHP is setup correctly. And if you are in Windows, make sure your web server is up and running the way it is supposed to.
Understanding the code
Line 1:
<?php
Any PHP you run in a file must begin with a <?php
tag. This lets the PHP interpreter know that what follows is code it needs to parse. Get used to this, as you will see it a million times.
Line 3:
echo 'Hello, world!';
There are several ways to output text to the screen. The most common is the echo
command. It is a function, and that usually means that you would call it like this: echo ('Hello, world!');
But since it is so common, the PHP overlords have let us use it without parenthesis. So, by convention, you will only ever see it called like we did in line 3.
One more point on quotation marks: while it doesn't much matter if you use single or double quotes, it does matter if you use so-called "smart quotes" where the outside quote-marks face each other: “ and ”. Those will not work at all. You need the marks that are straight up and down. So careful if you try writing your code in a rich text editor like Microsoft Word and then try to run it. Use an editor made for coding and this won't be an issue.
Line 5:
?>
The last line is a closing tag that closes the PHP tag we saw in line 1. If the file you are coding only has PHP code in it, this closing tag is optional. If you are mixing HTML and PHP, then always have a closing tag.
Also, if you have a closing tag at the end of the file, don't put any spaces or line breaks after it. That will save you some potential headaches, too.
Lines 2 and 4:
Yes, those lines are blank. Blank lines and extra spaces are ignored by PHP. But having them makes reading the code a bit easier, and since you and I are humans and not computers, it is nice to have code that is readable. So use blank lines to keep things separate and readable.
Resources
- PHP Documentation: echo
- Performance differences between Single Quotes and Double Quotes in PHP
- Disproving the Single Quotes Performance Myth
Challenges
Display your name
Write a PHP script that outputs your name to the screen. Make sure you are using the echo
command.