PHP, a language for building Web pages on the server

PHP programming language

A PHP script produces web pages, and may be embedded inside HTML code as JavaScript but works server side. A PHP script may be used locally.
It is similar to C and shell languages on Unix.

PHP was designed in 1993 by Rasmus Lerdorf because it needs a free tools to program Web pages and released under the name PHP/FI, (Personal Home Pages / Form Interpreter).

"I never thought while writing PHP that someday millions of people will be looking over my shoulder into my code. If I would have known that, I would have surely done a few things differently". Rasmus Lerdorf, 2012.

He has developped it further with the help of other programmers and made it open source for the community of users. A new engine was created for PHP 3 and a new name given: PHP Hypertext Preprocessor in 1997. Recursive acronyms were the trend with GNU is Not Unix, PNG is Not Gif and so ones... The Zend engine was created in 1999 for PHP 4.
Having worked at Yahoo!, Rasmus Lerdorf then was employed at WePay and then Etsy.

PHP 5 was released in 2004, it is more object-oriented and supports XML.

A server must be configured to execute the interpreter on pages with .php extensions, and to send resulting modified HTML pages on the network.
PHP is the P of LAMP, a popular architecture that includes the Linux operating system, the Apache server and the MySQL database manager.
A project to port PHP to .NET exists and is named Phalanger.

Features of PHP

Syntax

The language is not case-sensitive.
Floating point numbers are denoted by a dot followed by a number or zéro.
Variables are prefixed with a $ symbol and no type is specified in advance.
Literal string which use the "" notation are evaluated for variables and special chars.

Some symbols:

<?php and ?> must enclose a PHP program.
# or // starts a comment.
array( "1" =>" "a, ...) is a dictionary.

Control structures

The if structure has elsif and else options.

if(x < 10)
{ echo "$x less than 10\n"; }
else { echo 'etc...\n' }
The while structure:
while(expr)

{
    ... 
} 

Function or method

The definition of a function starts with the function keyword, followed by the name and the list or parameters separated by commas, and the body is enclosed between { and }.
The return keyword in the body of the definition allows to return one value.

function funcname( arguments )
{
...statements... return(x);
}

Class

class name

{
    ...
}

The body is similar to the global code.

Local server

To work locally, including for development, you must install a server and then run the script in a browser with the prefix http://localhost.

Since version 5.4, it becomes redundant as a local server is included in the distribution. The server is started with this command:

$ php -S localhost:1000

Where 1000 is the port number (this is an example). 
The advantage over solutions like Wamp is that it is not necessary to move the files to a specific directory, www in this case. Scripts and PHP pages are working where they are.

Sample code

Displaying chars of a string.

$str = "demo";
$len = strlen($str);
for($i = 0; $i < $len; $i++) 
{ 
  echo $str[$i];
} 

Displaying elements of a list.

$arr = array(1,2,3 );
$arr = array_merge($arr, array( 4,5));
$sub = array_slice(|$arr, 1,3);
foreach($sub $as $num) 
{
  echo $num;
} 
>> should display:  234

Why use PHP? The user experience

PHP is an Internet tool, running server side, it is a scripting language that may be embedded into web pages. It is usable for processing large data and build a HTML page that display the results. (JavaScript is convenient for dynamic change of HTML pages.)
PHP 5 is a simpler concurrent to Java as an application server and a platform for web applications and web services. It eases the use of XML, MySQL, SQLite, SOAP and many database systems.
PHP is the most used language to build a CMS, a content management system.

The language was developed from day to day, to add the features needed, without well-defined plan. As a result it lacks consistency, clear rules, and we must constantly refer to the manual, especially to find the parameters of a particular function.
For example arguments for the substr_replace function are the original string followed by the part to be replaced and the replacement string, while the str_replace function has for arguments the part to be replaced and the replacement string followed by the original string.
Read more about PHP inconsistency in PHP, a fractal of bad design. But this article is not without errors. For example, PHP has a debugger (see the list of tools below), it works without Apache, constants are not functions, etc...

PHP 6 (actually PHP 5.x)

The PHP 6 project with Unicode support has been abandoned, but some features seemed so useful that it was decided to incorporate them into PHP 5:

  1. Namespaces.
  2. XMLReader. To read XML files in Sax mode.
  3. XMLWriter.
  4. goto, this directive comes from the Basic language.
  5. Closures.
  6. Filefinfo eExtension, to access the file system.
  7. Phar. Allows to embedd a PHP application into a single executable file, as does Java with .jar files.
  8. Intl. Internationalization.
  9. The operators continue and break will work with a constant parameter or without parameter.
  10. The ereg() function for regular expressions will be deleted. It will be replaced by preg_match() whose format is different but more generally used.
  11. The compatibility with older formats like GD 1 and Freetype 1 will no longer be supported.
  12. Magic_quotes, that was very decried as a source of security holes will no longer be supported.
  13. The same is true for register_global, safe_mode and register_long_arrays.
    We must therefore modify the current applications to make them compatible.
  14. The tags <% %> will no longer be recognized, if you use <?php ?>, there will be no change.
  15. dl() to load dynamically a library is disabled.

Optimizing PHP: Google vs. PHP Team

Technicians at Google advise Webmasters how to optimize the PHP code and indicates several points on how to change the code, replace control structures, to optimize it at best.

But the creators of the language have a different analysis. This controversy is interesting for webmasters that use PHP on their site because it provides useful information on optimizing the code.

The only point on which both parties agree, is that it is best to migrate to the latest version of PHP , as it is always the fastest.

Tools and tutorials

PHP Editors

Development tools

Scripts and tutorials