PHP


This draft deletes the entire topic.

expand all collapse all

Examples

  • 69

    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>
    

    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,

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

    is same as the below line:

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

    In real-world applications, this shorthand syntax should only be used in templates, with properly escaped values to prevent Cross-Site-Scripting attacks.

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

  • 45

    Two popular widely used language constructs to print output in PHP are echo and print.

    <?php
        echo "Hello, World!\n";
        print "Hello, World!\n";
    

    Both statements perform the same function, even though there are minor differences:

    • echo has a void return, whilst print returns a type of int with a value of 1
    • echo can take multiple arguments (without parentheses only), whilst print only takes one argument
    • echo is theoretically faster than print because it has no return value, resulting in less overhead (see the answer Reference: Comparing PHP's print and echo for more information). The performance difference is negligible, however, and will have no noticeable difference on application speed.

    Because echo and print are language constructs, not functions, they do not require parentheses around their arguments. For cosmetic consistency with other functions, the parentheses can be included. echo can accept multiple arguments, separated by commas, but only if no parentheses are used.

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

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

    Save this code to a file, hello_world.php, and run it through the PHP interpreter:

    > php hello_world.php
    Hello, World!
    

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

  • 34

    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.
    We'll add the Content-Type header to notify the browser of the content we are sending.

    The following will send a text/plain header (to output plain text):

    <?php
        header("Content-Type: text/plain");
        echo "Hello World";
    ?>
    

    You can render a JSON response by sending the application/json content type:

    <?php
        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);  #> {"response":"Hello World"}
    ?>
    

    The header() function must be called before PHP produces any output, or the web server will have already sent headers for the response. This code will produce a warning:

    <?php
        // 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";
    ?>
    

    will give you a typical warning:

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

    Note that 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.

Please consider making a request to improve this example.

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 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 two supported versions: 5.6 and 7.0.

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 maintaining or translating the official PHP documentation.

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

Versions

VersionSupported UntilRelease Date
7.12019-12-012016-12-01
7.02018-12-032015-12-03
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
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
3.02000-10-201998-06-06
2.01997-11-01
1.01995-06-08

In italic: Presumably supported until.

Still have a question about Getting started with PHP? Ask Question

Topic Outline