Table of Contents
Introduction
Rasmus Lerdorf used to write his codes for his personal website using programs written in C. In 1994 he wrote several CGI programs and extended them to work with web forms and thereafter connected it with databases. These snippets were then open sourced and were known as PHP/FI: “Personal Home Page/Forms Interpreter”.
Later organically these snippets grew into a programming language. Later in 1997, Zeev Suraski and Andi Gutmans rewrote the parser and called it Zend Engine. The new language was transformed and its new version PHP3 was released with a new acronym: PHP: Hypertext Preprocessor.
What is PHP?
PHP is a server side scripting language. A scripting language is a program that executes at run time. It means, compilation of these programs are not required unlike C/C++ or Java. By server side, we mean it cannot run directly at client side (browser) like JavaScript, and require a server where it is executed.
PHP Architecture
The PHP architecture can be designed in two ways:
1. PHP Architecture in terms of Web Development
2. PHP Architecture in terms of DB Processing
What PHP can do?
PHP is mainly used for two operations:
1. Website Development: Using Zend Engine, the PHP code is converted in to HTML & CSS.
2. SQL and back end processing: The functions within the scripts are used for backend processing, and the SQL is parsed by the server.
Apart from above activities, PHP can also be used as standalone graphical applications, Robotic Drone controls. Most famous Content Management Systems such as WordPress, Magento, Drupal and Joomla runs on PHP.
Where to write a PHP program?
A PHP can be written in any editor. The recommended editors for PHP are Rapidphpeditor and Eclipse PDT framework as they support both formatting and debugging.
How to write a PHP program?
A PHP program can be written within HTML or as a separate file. In both cases it has to be saved as php file. In case of HTML, it is written within index.php file as shown below:
<!DOCTYPE html> <html> <body> <h1>My first PHP page</h1> <?php echo "Hello World!"; ?> </body> </html>
It is recommended to divide the backend and frontend code, hence the backend code that comprises of functions and SQL operations are written in a separate file e.g. “server.php”
How to run a PHP program?
To run a php program either the one within HTML content or one with just php programs, we save them as .php files.
We need a server to run php file as it is a server side executable file. We will firstly follow steps mentioned here: “PHP Installation with XAMPP”. Once XAMPP is installed and server is running, then we will save our php files in a folder “test” inside “C:\xampp\htdocs” in windows.
Then we will run http://localhost:/test/ in browser.
In case you have saved your above discussed php code in index.php, then it will run something like this:
In case you have a php file without HTML content and you want it to run/executed via frontend (e.g. using AJAX calls), then we will be writing two programs.
The first one is a simple php file “server.php” with given code:
<?php header("Access-Control-Allow-Origin:*, Origin, X-Requested-With, Content-Type, Accept"); // header('Content-Type: application/json'); echo $_POST["method"](); //simple function example function readCall(){ echo 'PHP Call is successful'; } ?>
Here, we have created a function that return “PHP is successful”. In the same folder, we have a html file with given content:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script type = "text/javascript"> function script(){ // AJAX call $.ajax({ url: "http://localhost:81/test/server.php", type: "POST", data: { method: "readCall" }, success: function (response) { alert(response); }, error: function (request, error) { alert(request); } }); } </script> </head> <body onload="script()"> <h1>HTML Content</h1> </body> </html>
Here, this HTML file firstly loads jQuery from an external link, so that we can perform AJAX call. In second script we have written a simple AJAX call which is calling out our server.php file using its location (http://localhost:81/test/server.php, or simply http://localhost:/test/server.php if you have not set any port in XAMPP). The AJAX gets our PHP echo in response and we have added the response in the alert, which looks like this in browser:
0 Comments