php_cookbook-6-12
php_cookbook-6-12
Preface
PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language.
Originally created by Rasmus Lerdorf in 1994, the PHP reference implementation is now produced by The PHP Group. PHP
originally stood for Personal Home Page, but it now stands for the recursive backronym PHP: Hypertext Preprocessor.
PHP code may be embedded into HTML code, or it can be used in combination with various web template systems, web
content management systems and web frameworks. PHP code is usually processed by a PHP interpreter implemented as a
module in the web server or as a Common Gateway Interface (CGI) executable. The web server combines the results of the
interpreted and executed PHP code, which may be any type of data, including images, with the generated web page. (Source:
https://en.wikipedia.org/wiki/PHP)
In this ebook, we provide a compilation of PHP based examples that will help you kick-start your own web projects. We
cover a wide range of topics, from HTML tables and files uploading, to SOAP clients and Curl command execution. With our
straightforward tutorials, you will be able to get your own projects up and running in minimum time.
PHP Programming Cookbook viii
WCGs (Web Code Geeks) is an independent online community focused on creating the ultimate Web developers resource center;
targeted at the technical architect, technical team lead (senior developer), project manager and junior developers alike.
WCGs serve the Web designer, Web developer and Agile communities with daily news written by domain experts, articles,
tutorials, reviews, announcements, code snippets and open source projects.
You can find them online at https://www.webcodegeeks.com/
PHP Programming Cookbook 1 / 63
Chapter 1
PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that
is especially suited for web development and can be embedded into HTML. It allows web developers to create dynamic content
that interacts with databases. PHP is basically used for developing web based software applications.
PHP is mainly focused on server-side scripting, so you can do anything any other CGI program can do, such as collect form
data, generate dynamic page content, or send and receive cookies. Code is executed in servers, that is why you’ll have to install
a sever-like environment enabled by programs like XAMPP which is an Apache distribution.
1.1 Introduction
Back in 1994, Rasmus Lerdorf unleashed the very first version of PHP. However, now the reference implementation is now
produced by The PHP Group. The term PHP originally stood for Personal Home Page but now it stands for the recursive
acronym: Hypertext Preprocessor. PHP 4 and PHP 5 are distributed under the PHP Licence v3.01, which is an Open Source
licence certified by the Open Source Initiative.
Three are the main areas where PHP scripts are used:
• Server-side scripting
This is the most used and main target for PHP. You need three things to make this work the way you need it. The PHP parser (CGI
or server module), a web server and a web browser. You need to run the web server where. You can access the PHP program
output with a web browser, viewing the PHP page through the server. All these can run on your home machine if you are just
experimenting with PHP programming.
You can make a PHP script to run it without any server or browser. You only need the PHP parser to use it this way. This type
of usage is ideal for scripts regularly executed using cron (on Linux) or Task Scheduler (on Windows). These scripts can also be
used for simple text processing tasks.
PHP may not the very best language to create a desktop application with a graphical user interface, but if you know PHP very
well, and would like to use some advanced PHP features in your client-side applications you can also use PHP-GTK to write
such programs. You also have the ability to write cross-platform applications this way.
In this article, we’ll have a detailed look at the server-side scripting using PHP.
PHP Programming Cookbook 2 / 63
There stand convincing arguments for all those who wonder why PHP is so popular today:
A web server is an information technology that processes requests via HTTP, the basic network protocol used to distribute
information on the World Wide Web. There exist many types of web servers that servers use. Some of the most important and
well-known are: Apache HTTP Server, IIS (Internet Information Services), lighttpd, Sun Java System Web Server etc. As a
matter of fact, PHP is compatible with all these web servers and many more.
Unlike some technologies that require a specific operating system or are built specifically for that, PHP is engineered to run on
various platforms like Windows, Mac OSX, Linux, Unix etc)
An important reason why PHP is so used today is also related to the various databases it supports (is compatible with). Some
of these databases are: DB++, dBase, Ingres, Mongo, MaxDB, MongoDB, mSQL, Mssql, MySQL, OCI8, PostgreSQL, SQLite,
SQLite3 and so on.
Anyone can start using PHP right now by downloading it from php.net. Millions of people are using PHP to create dynamic
content and database-related applications that make for outstanding web systems. PHP is also open source, which means the
original source code is made freely available and may be redistributed and modified.
PHP is a simple language to learn step by step. This makes it easier for people to get engaged in exploring it. It also has such a
huge community online that is constantly willing to help you whenever you’re stuck (which actually happens quite a lot).
The graphic below shows a basic workflow of dynamic content being passed to and from the client using PHP:
PHP Programming Cookbook 3 / 63
XAMPP is a free and open source cross-platform web server solution developed by Apache Friends, consisting mainly of the
Apache HTTP Server, MariaDB database, and interpreters for scripts written in the PHP and Perl programming languages. In
order to make your PHP code execute locally, first install XAMPP.
• Download XAMPP
• Install the program (check the technologies you want during installation)
• Open XAMPP and click on "Start" on Apache and MySQL (when working with databases)