Stats

8517 Contributors: 70 Wednesday, August 2, 2017
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial: riptutorial@gmail.com
Roadmap: roadmap

Getting started with PHP

Remarks

enter image description here

PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source programming language. It is especially suited for web development. The unique thing about PHP is that it serves both beginners as well as experienced developers. It has a low barrier to entry so it is easy to get started with, and at the same time, it provides advanced features offered in other programming languages.

Open-Source

It's an open-source project. Feel free to get involved.

Language Specification

PHP has a language specification.

Supported Versions

Currently, there are three supported versions: 5.6, 7.0 and 7.1.

Each release branch of PHP is fully supported for two years from its initial stable release. After this two year period of active support, each branch is then supported for an additional year for critical security issues only. Releases during this period are made on an as-needed basis: there may be multiple point releases, or none, depending on the number of reports.

Unsupported Versions

Once the three years of support are completed, the branch reaches its end of life and is no longer supported.

A table of end of life branches is available.

Issue Tracker

Bugs and other issues are tracked at https://bugs.php.net/.

Mailing Lists

Discussions about PHP development and usage are held on the PHP mailing lists.

Official Documentation

Please help to maintain or to translate the official PHP documentation.

You might use the editor at edit.php.net. Check out our guide for contributors.

Versions

PHP 7.x

VersionSupported UntilRelease Date
7.12019-12-012016-12-01
7.02018-12-032015-12-03

PHP 5.x

VersionSupported UntilRelease Date
5.62018-12-312014-08-28
5.52016-07-212013-06-20
5.42015-09-032012-03-01
5.32014-08-142009-06-30
5.22011-01-062006-11-02
5.12006-08-242005-11-24
5.02005-09-052004-07-13

PHP 4.x

VersionSupported UntilRelease Date
4.42008-08-072005-07-11
4.32005-03-312002-12-27
4.22002-09-062002-04-22
4.12002-03-122001-12-10
4.02001-06-232000-05-22

Legacy Versions

VersionSupported UntilRelease Date
3.02000-10-201998-06-06
2.01997-11-01
1.01995-06-08

HTML output from web server

PHP can be used to add content to HTML files. While HTML is processed directly by a web browser, PHP scripts are executed by a web server and the resulting HTML is sent to the browser.

The following HTML markup contains a PHP statement that will add Hello World! to the output:

<!DOCTYPE html>
<html>
    <head>
        <title>PHP!</title>
    </head>
    <body>
        <p><?php echo "Hello world!"; ?></p>
    </body>
</html>

When this is saved as a PHP script and executed by a web server, the following HTML will be sent to the user's browser:

<!DOCTYPE html>
<html>
    <head>
        <title>PHP!</title>
    </head>
    <body>
        <p>Hello world!</p>
    </body>
</html>
PHP 5.x5.4

echo also has a shortcut syntax, which lets you immediately print a value. Prior to PHP 5.4.0, this short syntax only works with the short_open_tag configuration setting enabled.

For example, consider the following code:

<p><?= "Hello world!" ?></p>

Its output is identical to the output of the following:

<p><?php echo "Hello world!"; ?></p>

In real-world applications, all data output by PHP to an HTML page should be properly escaped to prevent XSS (Cross-site scripting) attacks or text corruption.

See also: Strings and PSR-1, which describes best practices, including the proper use of short tags (<?= ... ?>).

Hello, World!

The most widely used language construct to print output in PHP is echo:

echo "Hello, World!\n";

Alternatively, you can also use print:

print "Hello, World!\n";

Both statements perform the same function, with minor differences:

  • echo has a void return, whereas print returns an int with a value of 1
  • echo can take multiple arguments (without parentheses only), whereas print only takes one argument
  • echo is slightly faster than print

Both echo and print are language constructs, not functions. That means they do not require parentheses around their arguments. For cosmetic consistency with functions, parentheses can be included. Extensive examples of the use of echo and print are available elsewhere.

C-style printf and related functions are available as well, as in the following example:

printf("%s\n", "Hello, World!");

See Outputting the value of a variable for a comprehensive introduction of outputting variables in PHP.

Instruction Separation

Just like most other C-style languages, each statement is terminated with a semicolon. Also, a closing tag is used to terminate the last line of code of the PHP block.

If the last line of PHP code ends with a semicolon, the closing tag is optional if there is no code following that final line of code. For example, we can leave out the closing tag after echo "No error"; in the following example:

<?php echo "No error"; // no closing tag is needed as long as there is no code below

However, if there is any other code following your PHP code block, the closing tag is no longer optional:

<?php echo "This will cause an error if you leave out the closing tag"; ?>
<html>
    <body>
    </body>
</html>

We can also leave out the semicolon of the last statement in a PHP code block if that code block has a closing tag:

<?php echo "I hope this helps! :D";
echo "No error" ?>      

It is generally recommended to always use a semicolon and use a closing tag for every PHP code block except the last PHP code block, if no more code follows that PHP code block.

So, your code should basically look like this:

<?php
    echo "Here we use a semicolon!";
    echo "Here as well!";
    echo "Here as well!";
    echo "Here we use a semicolon and a closing tag because more code follows";
?>
<p>Some HTML code goes here</p>
<?php
    echo "Here we use a semicolon!";
    echo "Here as well!";
    echo "Here as well!";
    echo "Here we use a semicolon and a closing tag because more code follows";
?>
<p>Some HTML code goes here</p>
<?php
    echo "Here we use a semicolon!";
    echo "Here as well!";
    echo "Here as well!";
    echo "Here we use a semicolon but leave out the closing tag";

Non-HTML output from web server

In some cases, when working with a web server, overriding the web server's default content type may be required. There may be cases where you need to send data as plain text, JSON, or XML, for example.

The header() function can send a raw HTTP header. You can add the Content-Type header to notify the browser of the content we are sending.

Consider the following code, where we set Content-Type as text/plain:

header("Content-Type: text/plain");
echo "Hello World";

This will produce a plain text document with the following content:

Hello World

To produce JSON content, use the application/json content type instead:

header("Content-Type: application/json");

// Create a PHP data array.
$data = ["response" => "Hello World"];

// json_encode will convert it to a valid JSON string.
echo json_encode($data);

This will produce a document of type application/json with the following content:

{"response":"Hello World"}

Note that the header() function must be called before PHP produces any output, or the web server will have already sent headers for the response. So, consider the following code:

// Error: We cannot send any output before the headers
echo "Hello";

// All headers must be sent before ANY PHP output
header("Content-Type: text/plain");
echo "World";

This will produce a warning:

Warning: Cannot modify header information - headers already sent by (output started at /dir/example.php:2) in /dir/example.php on line 3

When using header(), its output needs to be the first byte that's sent from the server. For this reason it's important to not have empty lines or spaces in the beginning of the file before the PHP opening tag <?php. For the same reason, it is considered best practice (see PSR-2) to omit the PHP closing tag ?> from files that contain only PHP and from blocks of PHP code at the very end of a file.

View the output buffering section to learn how to 'catch' your content into a variable to output later, for example, after outputting headers.

PHP built-in server

PHP 5.4+ comes with a built-in development server. It can be used to run applications without having to install a production HTTP server such as nginx or Apache. The built-in server is only designed to be used for development and testing purposes.

It can be started by using the -S flag:

php -S <host/ip>:<port>

Example usage

  1. Create an index.php file containing:
<?php
echo "Hello World from built-in PHP server";
  1. Run the command php -S localhost:8080 from the command line. Do not include http://. This will start a web server listening on port 8080 using the current directory that you are in as the document root.

  2. Open the browser and navigate to http://localhost:8080. You should see your "Hello World" page.

Configuration

To override the default document root (i.e. the current directory), use the -t flag:

php -S <host/ip>:<port> -t <directory>

E.g. if you have a public/ directory in your project you can serve your project from that directory using php -S localhost:8080 -t public/.

Logs

Every time a request is made from the development server, a log entry like the one below is written to the command line.

[Mon Aug 15 18:20:19 2016] ::1:52455 [200]: /

PHP CLI

PHP can also be run from command line directly using the CLI (Command Line Interface).

CLI is basically the same as PHP from web servers, except some differences in terms of standard input and output.

Triggering

The PHP CLI allows four ways to run PHP code:

  1. Standard input. Run the php command without any arguments, but pipe PHP code into it:
    echo '<?php echo "Hello world!";' | php
  2. Filename as argument. Run the php command with the name of a PHP source file as the first argument:
    php hello_world.php
  3. Code as argument. Use the -r option in the php command, followed by the code to run. The <?php open tags are not required, as everything in the argument is considered as PHP code:
    php -r 'echo "Hello world!";'
  4. Interactive shell. Use the -a option in the php command to launch an interactive shell. Then, type (or paste) PHP code and hit return:
    $ php -a
    Interactive mode enabled
    php > echo "Hello world!";
    Hello world!

Output

All functions or controls that produce HTML output in web server PHP can be used to produce output in the stdout stream (file descriptor 1), and all actions that produce output in error logs in web server PHP will produce output in the stderr stream (file descriptor 2).

Example.php
<?php
echo "Stdout 1\n";
trigger_error("Stderr 2\n");
print_r("Stdout 3\n");
fwrite(STDERR, "Stderr 4\n");
throw new RuntimeException("Stderr 5\n");
?>
Stdout 6
Shell command line
$ php Example.php 2>stderr.log >stdout.log;\
> echo STDOUT; cat stdout.log; echo;\
> echo STDERR; cat stderr.log\

STDOUT
Stdout 1
Stdout 3

STDERR
Stderr 4
PHP Notice:  Stderr 2
 in /Example.php on line 3
PHP Fatal error:  Uncaught RuntimeException: Stderr 5
 in /Example.php:6
Stack trace:
#0 {main}
  thrown in /Example.php on line 6

Input

See: Command Line Interface (CLI)

PHP Tags

There are three kinds of tags to denote PHP blocks in a file. The PHP parser is looking for the opening and (if present) closing tags to delimit the code to interpret.

Standard Tags

These tags are the standard method to embed PHP code in a file.

<?php
    echo "Hello World";
?>
PHP 5.x5.4

Echo Tags

These tags are available in all PHP versions, and since PHP 5.4 are always enabled. In previous versions, echo tags could only be enabled in conjunction with short tags.

<?= "Hello World" ?>

Short Tags

You can disable or enable these tags with the option short_open_tag.

<?
    echo "Hello World";
?>

Short tags:

  • are disallowed in all major PHP coding standards
  • are discouraged in the official documentation
  • are disabled by default in most distributions
  • interfere with inline XML's processing instructions
  • are not accepted in code submissions by most open source projects
PHP 5.x5.6

ASP Tags

By enabling the asp_tags option, ASP-style tags can be used.

<%
    echo "Hello World";
%>

These are an historic quirk and should never be used. They were removed in PHP 7.0.