PHP, a language for building Web pages on the server

PHP programming language

There are different views on how to optimize language to speed up its operation, and Facebook has even taken a radical approach by creating a compiler and a virtual machine. But this language became universal on the server side. PHP is the P of LAMP, a popular architecture that includes the Linux operating system, the Apache server and the MySQL database manager.

A PHP script produces web pages, and may be embedded inside HTML code as JavaScript but works server side. The interpreter may be used locally on the desktop.

The language was designed in 1993 by Rasmus Lerdorf because it needs a free tools to program Web pages and it was 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.

Having worked at Yahoo!, Rasmus Lerdorf then was employed at WePay and then Etsy. He then developped PHP 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 name like: GNU is Not Unix, PNG is Not Gif and so on...
The Zend engine was created in 1999 for PHP 4.
PHP 5 was released in 2004, it is more object-oriented and supports XML.

Features of PHP

PHP borrows ideas from C and the shell of Unix.

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.
<?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";
}
elseif(x > 10) {
   echo "$x greater 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.

String

Example, displaying the content.

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

Array

Declare, merge two arrays, extracting a subset, displaying the items.

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

Built-in local server

To work locally, including for development, you had to 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).  To launch the index.php script, and display the result in a browser, type in the URL field:

localhost:1000 

The advantage over solutions like Wamp is that it is not necessary to move the files to a specific subdirectory inside the directory of Wamp, www in this case. Scripts and PHP pages are working where they are.

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

There are actually many alternatives, Python, Ruby, Node, Go, and more recently, Julia.

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

Support for Windows XP is dropped since 5.5.

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

See also : PHP frameworks and Winbinder, an extension to build graphical user interfaces for PHP apps on Windows.