Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Introduction to PHP

PHP is a programming language that runs on the server. It powers a large share of the web, from small blogs to big online stores.

How PHP works

When you open a page, three things happen:

  1. The browser sends a request to the server.
  2. The server finds the right PHP file and runs it.
  3. The result — usually a finished HTML page — is sent back to the browser.

The visitor only sees the result. The PHP code itself stays on the server and cannot be viewed from the browser. That is the difference from JavaScript, which runs right in the browser.

Your first program

PHP code goes after the opening <?php tag. The echo statement prints text:

<?php
 
echo 'Hello, world!';

Press "Run" to see the result. Text in quotes is called a string — strings get their own lesson later.

Every statement ends with a semicolon. A missing semicolon is the most common beginner mistake: PHP reports a syntax error and does not run the program.

Several statements

Statements run in order, from top to bottom:

<?php
 
echo 'First line';
echo "\n";
echo 'Second line';

echo does not add a line break by itself. To start a new line, print the newline character "\n" — note that it is written in double quotes.

You can print several values at once, separated by commas:

<?php
 
echo 'One', ', ', 'two';

Comments

Comments are not executed; they explain the code to you and to other developers:

<?php
 
// A single-line comment
 
/*
   A multi-line
   comment
*/
echo 'Comments are not printed';

A good comment explains why the code exists rather than restating what it does.

What's next

TopicWhat you will learn
VariablesHow to store data
Data typesWhat kinds of values exist

The next lesson covers variables.

Thanks for reading!

Donate to the site — we work hard for you.