PHP, a language for building Web pages on the server
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
- All those of C, but typed variables, plus:
- Object oriented.
- Dynamic untyped variables prefixed by $.
- Associatives arrays (tables with keys).
- A foreach construct to scan arrays and iterators.
- Offers a lot of APIs dedicated to the standard of the web and databases.
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)The while structure:
{ echo "$x less than 10\n"; }
else { echo 'etc...\n' }
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:
- Namespaces.
- XMLReader. To read XML files in Sax mode.
- XMLWriter.
- goto, this directive comes from the Basic language.
- Closures.
- Filefinfo eExtension, to access the file system.
- Phar. Allows to embedd a PHP application into a single executable file, as does Java with .jar files.
- Intl. Internationalization.
- The operators continue and break will work with a constant parameter or without parameter.
- The ereg() function for regular expressions will be deleted. It will be replaced by preg_match() whose format is different but more generally used.
- The compatibility with older formats like GD 1 and Freetype 1 will no longer be supported.
- Magic_quotes, that was very decried as a source of security holes will no longer be supported.
- The same is true for register_global, safe_mode and register_long_arrays.
We must therefore modify the current applications to make them compatible. - The tags <% %> will no longer be recognized, if you use <?php ?>, there will be no change.
- 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.
- Copying variables or not?
In fact PHP optimizes the allocation and value of physical variables is copied only when it changes. - Single or double quotes?
This makes no difference. Although there are variables to interpret inside the strings between double quotes, it is not slower than concatenating variables and pieces of string. - Echo or print?
The execution speed is similar, but it some cases, this may depend on the configuration of the server.
(The question of speed, is useful when generating HTML pages from PHP commands.) - Switch/case or if/else?
Again, using one or the other makes no difference because the internal code is the same.
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
- Aptana Studio
Includes a PHP editor. (Windows) - Eclipse IDE
Extension to Eclipse for a PHP development interface. - NetBeans
IDE from Sun written in Java for PHP and other languages. - CodeAnywhere. (Formerly PHPAnywhere)
Online editor.
Development tools
- Php.net
The official website, home of the interpreter and the language. - HipHop PHP Compiler
A JIT compiler created and used by Facebook, available on GitHub with source code. Running with the HHVM virtual machine, it is 60% faster than the Zend interpreter. The JIT is intended to replace the HPHPC compiler that produces C++ and then binary. See the history of HipHop. - Xampp Server or Wamp Server
Windows Apache Linux PHP local server. Put your PHP script in the htdocs sub-directory or www at root of the Xampp/Wamp directory and they can run as on a true server, with MySQL requests and PhpMyAdmin or SQLite Manager to manage the databases! - Xdebug
Interactive debugger for PHP. - Winbinder
Extension to build graphical user interfaces for Windows. Demo. - React-PHP
Not blocking server such as Node.js, for PHP (and written in PHP).
Scripts and tutorials
- SQL tutorial
with PHP and MySQL
Learn with examples to make PHP scripts for using SQL on a website. - JavaScript in PHP. How to integrate the V8 compiler into the PHP language.
- PHP reboot. Proposal for a reboot of PHP without $ and other antiquities. This is the Go language.
- Technical infos. Inside PHP code.
