Ana Sayfa / PHP / PHP Getting started with PHP

PHP Getting started with PHP

PHP 7.x
Version Supported Until Release Date

7.1 2019-12-01 2016-12-01
7.0 2018-12-03 2015-12-03
PHP 5.x
Version Supported Until Release Date

5.6 2018-12-31 2014-08-28
5.5 2016-07-21 2013-06-20
5.4 2015-09-03 2012-03-01
5.3 2014-08-14 2009-06-30
5.2 2011-01-06 2006-11-02
5.1 2006-08-24 2005-11-24
5.0 2005-09-05 2004-07-13
PHP 4.x
Version Supported Until Release Date

4.4 2008-08-07 2005-07-11
4.3 2005-03-31 2002-12-27
4.2 2002-09-06 2002-04-22
4.1 2002-03-12 2001-12-10
4.0 2001-06-23 2000-05-22
Legacy Versions
Version Supported Until Release Date

3.0 2000-10-20 1998-06-06
2.0 1997-11-01
1.0 1995-06-08

HTML output from web server

PHP can be used to add content to HTML les. 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>
<p>Hello world!</p>

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 dierences:

  • 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 aroundtheir arguments. For cosmetic consistency with functions, parentheses can be included. Extensive examples of theuse 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.

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 rst 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 le before the PHP opening tag from les that contain only PHP and from blocks of PHP code at the very end of a file.

View the output buering 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.

PHP Notes for Professionals 4It can be started by using the -S fag:

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

Example usage

1. Create an index.php le containing:

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.


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/.
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 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 dierences in terms of standard input and output.


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
  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!


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).


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 1
Stdout 3

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


See: Command Line Interface (CLI)

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

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:

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>
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>
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";

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.

echo "Hello World";

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

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.

Bunada Göz Atın

Php Outputting the Value of a Variable

To build a dynamic and interactive PHP program, it is useful to output variables and …

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir