10.1
What is PHP?
PHP (PHP Hypertext Preprocessor) is one of the most popular server side scripting languages used for creating dynamic web pages. PHP usually runs on a web server and is available on different operating systems and platforms. It is an interpreted language — a PHP script is processed by the PHP engine each time it runs. PHP is an HTML embedded language, meaning PHP statements can be written within HTML code. When a PHP script runs, it processes the information and produces hypertext (HTML) as a result.
PHP was created by Rasmus Lerdorf, first in 1995, with the first official release in 1997. He is known as the Father of PHP, and continues to contribute to the project. PHP is an open-source project, completely free, and supported by a worldwide community of volunteers. It can access more than 20 different databases including MySQL, and can also output images, PDF files and Flash movies.
10.1.1
Benefits of using PHP
Client-side scripts execute faster since they run at the client, but a client-side language is dependent on the browser, and code like JavaScript is visible through "view source", making it less secure. PHP code, on the other hand, is executed on the server — only the output is sent to the client, so it is impossible for users to see the PHP source code.
- Easily embedded into HTML — simple to convert a static site into a dynamic one.
- Compatible with a wide variety of databases.
- An independent platform — runs across Windows, Linux or Mac servers.
- Supports most current web servers and interfaces easily with Apache/MySQL.
- Simple and easy to learn, with syntax similar to C++.
- Operates fast since it does not use a lot of system resources.
- Offers many levels of security to prevent malicious attacks.
- Free, open source, and legally usable for personal and commercial development.
10.1.2
Basics of PHP
A. Setting up the development environment — A PHP compatible web server and PHP interpreter must be installed, either separately or from a bundled package like WAMP (Windows, Apache, MySQL, PHP), XAMPP (X-OS, Apache, MySQL, PHP, PERL — works on any OS) or LAMP (Linux, Apache, MySQL, PHP). After installation, typing http://localhost in the browser confirms the server is running.
B. Writing and running the script — PHP scripts are plain text; only a text editor is needed. PHP scripting begins with <?php and ends with ?>. Files are saved with a .php extension in the web server's document root directory (e.g. C:\wamp\www for WAMP), and the output is viewed by typing the corresponding URL (e.g. http://localhost/example.php).
C. Combining HTML and PHP — PHP and HTML function together: PHP generates HTML, and HTML passes information to PHP. PHP code is embedded anywhere inside standard HTML using the tags <?php and ?>; anything outside these tags is read as plain HTML.
<HTML><BODY>
<?php
echo "<H2> Welcome to php </H2>";
?>
</BODY></HTML>
D. Comments in PHP — A comment is a line not read/executed as part of the program. PHP supports single line comments (//) and multiline comments (/* ... */).
E. Output statements — echo and print are the two basic constructs used to display output; both can output any data type, but only echo can take multiple comma-separated parameters. var_dump() displays both the data type and value of one or more variables.
| echo | print |
| Can take more than one parameter (without parentheses) | Takes only one parameter |
| Does not return any value | Returns TRUE/1 on success, FALSE if unable to print |
| Slightly faster | Slightly slower |
<?php
$a1=678; $a2="678"; $a3="Apple"; $a4=698.99;
var_dump($a1); var_dump($a2); var_dump($a3); var_dump($a4);
?>
// int(678) string(3) "678" string(5) "Apple" float(698.99)