Lesson 1 of 8 · 6 min

Hello, PHP

PHP runs a huge share of the web, including WordPress and Wikipedia. It is usually run by a web server to build pages, but it is a full programming language, and here it runs right in your browser.

Every PHP file starts with <?php. Printing uses echo:

<?php
echo "Mhoro!\n";
echo 2 + 3, "\n";

\n inside double quotes is a new line. Each statement ends with a semicolon.

Variables

Variable names start with a dollar sign:

$city = "Gweru";
$population = 158000;
$population = $population + 2000;

Joining text

The dot . joins strings:

echo $city . " has " . $population . " people\n";

Inside double quotes, variables are filled in automatically, which is often neater:

echo "$city has $population people\n";

Single quotes ('...') take text literally: no variables and no \n.

The first run downloads PHP (about 12 MB); after that it is quick.

Your task

  1. Print Hello, PHP!.
  2. Create $shop with the text Mama's Kitchen and $meals with the number 12.
  3. Print Mama's Kitchen sold 12 meals using your variables.
index.php
<?php
// Your PHP code here

PHP 8.4 runs right here in your browser. The first run downloads about 12 MB; after that it is instant.

Discussion

Stuck? Ask here. Be kind, and share hints rather than full answers.

Loading the discussion…