PHP


PHP: Hypertext Preprocessor 4 , better known as PHP ( recursive acronym ), is a free programming language 5 , mainly used to produce dynamic web pages via an HTTP server 4 , but can also function as any interpreted language Locally. PHP is an imperative object-oriented language .

PHP has created a large number of famous websites, such as Facebook , Wikipedia , etc. 6 It is considered one of the bases of the creation of dynamic web sites but also web applications.

History

The PHP language was created in 1994 by Rasmus Lerdorf for its website . It was originally a software library in C 7 which he used to keep track of visitors who came to consult his resume . As it added new functionality, Rasmus transformed the library into an implementation capable of communicating with databases and creating dynamic and simple web applications . Rasmus then decided in 1995 to publish its code, so that everyone can use and enjoy 8 . PHP s’ Called PHP / FI (for P ersonal H ome Pa ge Tools / F orm I nterpreter ). In 1997 , two students, Andi Gutmans and Zeev Suraski , redeveloped the core of PHP / FI. This work culminated one year later in PHP version 3, now PHP: Hypertext Preprocessor . Soon after, Andi Gutmans and Zeev Suraski began rewriting the internal engine of PHP. It was this new engine,

Uses

In 2002 , PHP is used by more than 8 million websites worldwide 10 , in 2007 by more than 20 million 11 and in 2013 by more than 244 million 12 .

In addition, PHP has become the most used server-side web programming language for several years:

Year Market share
Home 75%
Home 75% 13
2016 82% 14

Finally in 2010, PHP is the language of which open source software is the most used in companies, with 57% penetration rate 15 .

Versions

The current version is version 7.1, released on December 1, 2016 16 . It uses Zend Engine 2 and introduces more powerful object modeling, error handling based on the exceptions model, as well as business management capabilities. PHP 5 brings a lot of new features, such as SQLite support as well as ways to manipulate libxml2- based XML files and structures :

  • A simple API named SimpleXML ;
  • A fairly complete Document Object Model API ;
  • An XPath interface using DOM and SimpleXML;
  • Integration of libxslt for XSLT transformations via the XSL extension ;
  • A much better management of the objects compared to PHP 4, with possibilities that tend to approach those of Java .

Since June 2011 and the new delivery process of PHP 17 , the PHP delivery cycle is an annual update with significant functional changes, the life of a branch is 3 years and three stable branches are Maintained. This means that when a new version of PHP 5.x comes out, version 5.x-3 is no longer supported.

Since the release of PHP 5.6, the configuration is:

  • Development branch: 7.1;
  • Branches still supported (bug fixes and security): 7.0, 5.6;
  • All branches <5.6 are no longer supported.

It should be noted that historically, PHP had a default configuration focusing flexibility to safety (eg register globals, which was enabled by default PHP 4.2 to 18 ). This flexibility has allowed many developers to learn PHP but the flip side of the coin was that many PHP applications were insecure. The topic was well taken care of by the PHPGroup which set up default configurations focusing on security. The result was a reputation for insecure language, a reputation for insecurity which no longer had any reason for being . Required] .

Version history

Color code
Red Old version not supported
Orange Old version, security patches only
Green Current version, enhancements and security patches
Blue Future version
ViewVersion History

PHP 6 and Unicode

In 2005, the project to make PHP a working language of Unicode origin was launched by Andrei Zmievski, using the International Components for Unicode (ICU) library and using UTF-16 to represent Characters in the motor 29 .

Since this represented a major change in both the functioning of language in the PHP code created by its users, it was decided to integrate it into a new version 6.0 with other important features when developing 30 . However, the lack of experts in Unicode as well as the performance problems resulting from the conversion of strings to and from UTF-16 (rarely used in a web context) have led to the recurrent delivery of this version 31 . As a result, a version 5.3 was created in 2009 incorporating many non-Unicode features that were originally intended for version 6.0, including support for namespaces and anonymous functions. In March 2010,

The project has since moved on to a predictable (annual) delivery cycle containing significant but contained advances while maintaining as far as possible the retrocompatibility with the existing PHP code (5.4 in 2012, 5.5 in 2013, 5.6 planned for summer 2014 ). Since January 2014 33 , the idea of ​​a major new version introducing Unicode but relying on UTF-8 (widely become since the standard of the Web for the Unicode) and allowing some changes that can break back compatibility with PHP code Old is discussed again and the RFCs are now sorted according to their implementation in 5.

PHP 7

In view of the different orientations taken by the language than those provided by PHP 6, some developers propose to name the version following PHP 5 “PHP 7” instead of “PHP 6”. A vote among the developers validated this proposal by 58 votes against 24 34 .

The new version offers code optimization and, according to Zend, offers performance beyond those of virtual machines like HHVM 35 , 36 . The benchmarks external show similar performance for PHP HHVM and 7, with a slight advantage of HHVM in most scenarios 37 .

Presentation

PHP is a scripting language used most often server-side : in this architecture, the server interprets the PHP code of requested web pages and generates code ( HTML , XHTML , CSS , for example) and data ( JPEG , GIF , PNG by Example) that can be interpreted and rendered by a browser . PHP can also generate other formats like WML , SVG , PDF .

It was designed to allow the creation of dynamic applications, mostly developed for the Web . PHP is most often coupled to an Apache server although it can be installed on most HTTP servers such as IIS . This coupling makes it possible to recover information from a database, from a file system ( file contents and from the tree) or more simply from the data sent by the browser in order to be interpreted or stored for use Later.

It is a little typed and flexible language and therefore easy to learn by a beginner but, as a result, security flaws can quickly appear in applications. Pragmatic, PHP does not clutter theory and tends to choose the most direct path. Nevertheless, the names of functions (and the passage of arguments) do not always follow a uniform logic, which can be detrimental to learning.

Its use begins with the processing of forms and then access to the databases. Access to databases is easy after installing the corresponding modules on the server. The most obvious strength of this language is that it has allowed the easy resolution of previously complicated problems over time and has consequently become an essential component of accommodation offers.

It is cross-platform : as much on Linux as with Windows it makes it possible easily to renew the same code on a environment similar to (to take into account the rules of directories tree that can change).

Free, easy to use and easy to use, this language requires, like any programming language, a good understanding of the main usual functions as well as an acute knowledge of the security problems associated with this language.

The 5.3 version introduces many new functions: namespace – fundamental to the development of extensions to libraries and frameworks structured, the anonymous functions , the closures , etc.

Today, nearly 80% of websites use the PHP language in its different versions. Many PHP developers responsible for these sites use mostly version 5.4 (38%) in their daily missions 38 .

The PHP language has been the object of a forum organized by the AFPP (French PHP Users Association) for several years now, where experts of the programming and the environment meet to exchange around the PHP and its developers . This forum is also an important moment for these actors because it is synonymous with the launch of a study dedicated to developers PHP 39 carried out by the AFUP and the recruitment agency Agence-E.

How it works

PHP belongs to the large family of descendants of C , whose syntax is very close. In particular, its syntax and its construction resemble those of Java and Perl languages , except that PHP code can easily be mixed with HTML code within a PHP file.

In a use intended for the Internet, the execution of the PHP code takes place as follows: when a visitor requests to consult a page of a website, his browser sends a request to the corresponding HTTP server . If the page is identified as a PHP script (usually through the extension .php), the server calls the PHP interpreter that will process and generate the final code of the page (typically consisting of HTML or XHTML , but also often Cascading Style Sheets and JS ). This content is returned to the HTTP server, which ultimately sends it to the client.

An additional step is often added: the dialogue between PHP and the database . Classically, PHP opens a connection to the desired DBMS server , passes requests to it and retrieves the result, before closing the connection.

The use of PHP as a dynamic web page generator is most common, but it can also be used as a command-line programming or scripting language without using an HTTP server or browser. It allows to use many functions of the C language and several others without requiring compilation with each change of the source code.

To execute a PHP executable command line script in Linux / UNIX, just like in Perl or Bash, you insert the shebang : #! /usr/bin/php40 in the first line code . In a development editor like SciTE , even in Windows, a first line <?phpis enough, if the file has an .php type .

There is also an extension called PHP-GTK to create graphical client applications on a computer with the GTK + graphical library , or its WinBinder alternative .

PHP has many functions for operations on the file system , execute commands in the terminal , the management of databases , the functions of sorting and hashing , processing strings , generation and modification ‘images, algorithms for compression …

The Wikipedia engine, MediaWiki , is written in PHP and interacts with a MySQL or PostgreSQL database

Hello world

Some examples of the traditional Hello world :

<? Php
// form the simplest, recommended
echo 'Hello World' ;

Echo being a structure of the language, it is possible – and even recommended – not to put brackets, which also allows it to accept several parameters:

<? Php
Echo 'Hello' , 'World' ;

It is also possible to use the shortened version:

<? = 'Hello World' ?>

Result displayed:

Bonjour Monde

Tags

PHP code must be inserted between <? Php and ?> Tags .

There are shortened notations: <? And ?> Or ASP notation <% and %> , but these are not recommended because they can be disabled in the server configuration (php.ini or .htaccess): the portability of the code is thus reduced.

There is also this unusual syntax: <script language = “php”> and </ script> .

7 PHP, ASP script and ratings were removed 41 .

Instructions

The instructions are separated by “;” (It is not mandatory after the last instruction) and the line breaks do not modify the operation of the program. It would therefore be possible to write:

<? Php echo 'Hello World' ; Echo 'How are you?' ; Echo 'It's fine no?' ?>

For reasons of legibility, however, it is recommended to write one instruction per line. It is also better to write the last “;”.

Control Structures

The PHP code is composed by calls to functions, in order to assign values ​​to variables, all framed in conditions, loops. Example:

<? Php
// the strtolower function returns lowercase the string passed as parameter
$ lang = strtolower ( $ _POST [ 'lang' ]);
If ( $ lang === 'fr' )
 echo 'You speak French!' ;
Elseif ( $ lang === 'en' )
 echo 'You speak English!' ;
Else
 echo 'I do not know what your language is!' ;

A condition is applied when the expression in parentheses is evaluated to ‘true’, and it is not true in the case of ‘false’. In numeric form, 0 represents the ‘false’, and 1 (and all other numbers) represent ‘true’.

The previous code could also be written in this way:

<? Php
$ Lang = strtolower ( $ _POST [ 'lang' ]);
$ Is_lang_fr = $ lang === 'fr' ;
$ Is_lang_en = $ lang === 'en' ;
If ( $ is_lang_en )
 echo 'You speak French!' ;
elseif ( $ is_lang_en )
 echo 'You speak English!' ;
Else
 echo 'I do not know what your language is!' ;

Here we test the equality between $ lang and “fr”, but not directly in the if: test returns a boolean (that is, true or false) that is stored in the $ is_lang_fr variable . This variable is then entered in the yew, and the yew, depending on the value of the variable, will or will not perform the processing.

The if, elseif, and else blocks are usually delimited by the characters “{” and “}”, which can be omitted, as in the previous codes, when these blocks contain only one statement.

It is also possible to write else if in two words, as in C / C ++.

HTML code generation

You can generate HTML code with the PHP script, for example:

<? Php
$ Name = 'Albert' ;
Echo '<div> Hello' . $ Name . '! </ Div>' ;

It is also possible to use an alternative syntax for the if / else structure:

<? Php
$ AuthorName = [ 'Albert' , 'Bertrand' ];
$ CurrentName = 'Eve' ;
>
<? Php if ( in_array ( $ username , $ allowedName )) : ?>
 <P> Hello <? Php echo $ username ; ?> ! </ P>
<? Php else : ?>
 <P> You are not an authorized user! </ P>
<? Php endif ; ?>

Another approach is to concatenate the entire HTML code in a variable and to perform an echo of the variable at the end of the file:

<? Php
$ Out = '' ;
// We add progressively all the code html
$ out . = '<P> a' . 'B' . 'C </ p>' ;
$ Out . = '<Div class="bottom"> <p> of . 'E' . 'F </ p> </ div>' ;
// Show the content
echo $ out ;

In case the user has preferred the use of the echo command to the concatenation, it will be possible for him to capture the flow using the functions ob_start()and ob_get_clean() :

<? Php
// Place the stream in an
ob_start () buffer ;
// Display
echo 'a' , 'b' , 'c' , " \ n " ;
Echo 'd' , 'e' , 'f' , " \ n " ;
Require_once 'file.php' ; // Many views (echo)
// Stop buffering, retrieve its contents and set the buffer to empty (for a possible next buffer)
$ out = ob_get_clean ();
// Show the contents of the
echo $ out buffer ;

PHP, like JavaScript , also allows you to build a Document Object Model ( DOM ) 42 , which allows you to create or edit an HTML document without writing HTML, as shown in the following example:

<? Php
$ Doctype = DOMImplementation :: createDocumentType ( 'html' );
$ Dom = DOMImplementation :: createDocument ( null , "html" , $ doctype );
$ Html = $ dom -> documentElement ;
$ Html -> head = $ dom -> createElement ( "head" );
$ Html -> appendChild ( $ html -> head );
$ Html -> head -> Title = $ dom -> createElement ( "title" );
$ Html -> head -> title -> nodeValue = "Sample HTML" ;
$ Html -> head -> appendChild ( $ html -> head -> title );
$ Html -> head -> charset = $ dom -> createElement ( "meta" );
$ Html -> head -> charset -> SetAttribute ( "http-equiv" , "Content-Type" );
$ Html -> head -> charset -> setAttribute ( "content" , "text / html; charset = utf-8" );
$ Html -> head -> appendChild ( $ html -> head -> charset );
$ Html -> body = $ dom -> createElement ( "body" );
$ Html -> AppendChild ( $ html -> body );
$ Html -> body -> p = $ dom -> createElement ( "p" );
$ Html -> body -> p -> nodeValue = "This is a paragraph." ;
$ Html -> body -> appendChild ( $ html -> body -> p );
$ Html -> body -> p -> Br = $ dom -> createElement ( "br" );
$ Html -> body -> p -> appendChild ( $ html -> body -> p -> br );
$ Html -> body -> p -> a = $ dom -> createElement ( "a" );
$ Html -> body -> p -> a -> nodeValue = "
Echo $ dom -> saveHTML ();

Who creates the following HTML code:

<! DOCTYPE html>
< html >
< head >
< title > Sample HTML </ title >
< meta http-equiv = "Content-Type" happy = "text / html; charset = utf-8" >
</ head >
< Body > < p > This is a paragraph. < Br /> < a href = "cible.html" > This is a link. </ A > </ p >

However, this method is little used to generate a complete document, it is usually used to generate an XML file .

The command phpinfo()is also used to generate HTML code describing the server settings; It is also widely used to test the proper execution of the PHP runtime.

Object Oriented Programming

As in C ++ and Java, PHP allows to program in object oriented, creating classes containing attributes and methods, which can be instantiated or used in static.

However, PHP is a simple inheritance language 43 , that is, a class can only inherit from at most one other class (otherwise you must use a stroke ).

Here is an example of creating a class:

<? Php
Class Perso {
 const PV_INITIAL = 2000 ;
 Private $ pv ;
 Public function __construct ( $ pv = false ) { // Optional parameter
 if ( ! Is_numeric ( $ pv ) || $ pv < 0 || $ pv > 100000000 )
 $ this -> pv = self :: PV_INITIAL ;
 Else
 $ this -> pv = $ pv ;
 }
 // Accessors
 public function getPv () {
 return $ this -> pv ;
 }
 Public function isDead () {
 return $ this -> pv == 0 ;
 }
}
// Create a child class of Perso
class Magician extends Perso {
 private $ magie ;
}
// Create a class instance
$ perso = new Perso ( 1000 );
// Using the
echo object 'Your character has' . $ Perso -> getPV () . 'PV.' ;
// Constants of classes
echo 'The default PV attributed to a new character is' . Personal :: PV_INITIAL . '. ;
// Destroy the
unset object ( $ perso );

Mascot

PHP’s PHP mascot.

Like many Open Source projects, PHP has a mascot. This is the elePHPant, designed in 1998 by El Rubio 44 .

El Roubio was inspired by the similarity of PHP letters with an elephant and the fact that two of the letters of the language are already present in this word, which made it possible to create the neologism ePHPant. All works by El Roubio are distributed under the GNU GPL license . A plush of elePHPant 45 blue exists. Other versions have emerged in recent years (pink, yellow, red, purple and orange) under the impulse of companies (PHP Architect or Zend Technologies) or user groups like PHP Women.

Application examples

  • Wiki ( MediaWiki , WikiNi , DokuWiki …)
  • Forum ( phpBB , IPB , punBB …)
  • Blog management systems ( Dotclear , WordPress …)
  • Content management systems (aka CMS ) ( Spip , ExpressionEngine , Drupal , Xoops , Joomla …)
  • Administration of databases ( phpMyAdmin , phpPgAdmin , Adminer , Eskuel …)
  • Frameworks ( Zend Framework , CodeIgniter , CakePHP , Symfony , etc. )
  • ECM Software ( Dynacase Platform )
  • BPM software ( Dynacase Platform )
  • E-commerce ( Magento , osCommerce , PrestaShop , etc.)