Your first PHP program

Introduction

PHP is a language for building the server side of websites. It powers WordPress, Laravel and a very large share of the pages on the internet. The name once meant "Personal Home Page", and today it is a modern, fast and friendly language. In this course you will learn PHP 8.4 by writing small programs that read input and print results. Everything runs in your browser, so there is nothing to install. We use PHP the way the command line does: a program starts, reads its input, prints its output and ends.

How it works

A PHP file starts with the opening tag <?php. Everything after it is PHP code. Every statement ends with a semicolon, and a line that starts with // is a comment that PHP ignores.

<?php
echo "Hello, PHP!\n";

The word echo prints text. The \n at the end is a new line character: without it the next output would continue on the same line. print does almost the same job, but it takes only one value. With echo you can give several values separated by commas.

Text in double quotes can hold variables and escape codes like \n. Text in single quotes is taken exactly as written, so '\n' prints a backslash and the letter n. A variable starts with a dollar sign, for example $course. When you write "I like $course", PHP puts the value of the variable inside the text.

To print numbers in a fixed format, use printf. It takes a format and then the values. %d is for a whole number, %s for text, and %.2f for a number with two decimals. A number between the percent sign and the letter, such as %5d, sets the minimum width.

PHP reads your program from top to bottom. If you make a mistake in the syntax, such as a missing semicolon, PHP stops with a parse error and tells you the line. A program that runs but makes a mistake in logic prints a wrong result, and you have to find the reason yourself.

Example

<?php
// Our first PHP program
echo "Hello, PHP!\n";
print "Welcome to StepVioz.\n";
echo "Two", " ", "words", "\n";

$course = "PHP";
$lessons = 15;
echo "The $course course has $lessons lessons.\n";
printf("Pi is about %.2f and %5d is padded.\n", 3.14159, 42);
echo 'Single quotes keep \n as it is', "\n";

Line by line

  1. <?php opens PHP mode. A line starting with // is only a comment.
  2. echo prints the greeting and \n ends the line.
  3. print shows one more line of text.
  4. echo with commas prints three values one after another.
  5. Two variables are created. The double-quoted text fills in their values.
  6. printf prints pi with two decimals, and the number 42 padded to a width of five characters.
  7. The single-quoted text prints \n as two ordinary characters.

Expected result

The program prints:

Hello, PHP!
Welcome to StepVioz.
Two words
The PHP course has 15 lessons.
Pi is about 3.14 and    42 is padded.
Single quotes keep \n as it is

In the real world

PHP runs about three quarters of the websites whose language is known. Companies use it for shops, blogs, forums and APIs. Frameworks like Laravel and Symfony give structure to large projects. A command line PHP script is also common for background jobs, such as sending emails every night or converting data files.

Common mistakes

  • Forgetting the semicolon at the end of a statement, which gives a parse error.
  • Forgetting the \n, so that two outputs appear on the same line.
  • Using single quotes when you want a variable filled in. 'Hello $name' prints the dollar sign and the word name.
  • Writing the variable without the dollar sign, for example name instead of $name.

Best practices

  • Start every file with <?php and keep one statement on each line.
  • Use double quotes only when you need variables or escape codes; otherwise single quotes are fine.
  • Use printf when numbers need a fixed number of decimals.
  • Add short comments to explain why you did something, not what the line does.

Summary

A PHP program starts with <?php, and each statement ends with a semicolon. echo and print write output, and \n ends a line. Variables start with a dollar sign and are filled into double-quoted text. Single-quoted text is not changed. printf formats numbers and text with codes such as %d, %s and %.2f.

Quick check

1. Which tag starts a PHP program?
2. What does echo 'Hi $name'; print when $name holds Ada?
3. What does \n do inside double quotes?
4. Which printf code prints a number with two decimals?

Key takeaways

  • A PHP program starts with the opening tag and every statement ends with a semicolon.
  • Double quotes fill in variables and understand escape codes; single quotes do not.
  • echo and print write output and printf formats it.

Practice problems for this lesson

Was this lesson clear?

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.