PHP is an open source programming language that was developed in 1994 by Rasmus Lerdorf. PHP originally stood for Personal Home Pages but now stands for Hypertext Preprocessor, which is a recursive acronym. It is a loosely typed server-side scripting language used to build Web applications. A scripting language is a language that does not need to be compiled before use. And ‘loosely typed’ languages are those in which there is no need to define the type of a variable.
Today, more than 20 million domains use PHP, including Facebook and Yahoo.
PHP is easily embedded with HTML, and is used to manage dynamic content and the databases of websites or, we can say, Web applications. We can use PHP with many popular databases like MySQL, PostgreSQL, Oracle, Sybase, Informix and Microsoft SQL Server.
How PHP works on servers
When a client (browser) sends a request for a PHP page to the server, the server reads the requested file from the disk (storage) and sends this file to the interpreter. The interpreter then runs the PHP code, fetches the DB data (if required) and puts it into HTML tags. Once the interpreter completes its tasks, it sends the result back to the server, which sends this data to the client (browser) that made a request for this page.
MVC architecture with PHP
The Model-View-Controller concept involved in software development evolved in the late 1980s. It’s a software architecture built on the idea that the logic of an application should be separated from its presentation. A system developed on the MVC architecture should allow a front-end developer and a back-end developer to work on the same system without interfering with each other.
涉及软件开发的模型 - 视图 - 控制器概念在20世纪80年代后期发展。它是建立在应用程序的逻辑应该与其呈现分离的思想基础上的软件体系结构。在MVC架构上开发的系统应该允许前端开发人员和后端开发人员在相同的系统上工作而不会相互干扰。
Model is the name given to the component that will communicate with the database to manipulate the data. It acts as a bridge between the View component and the Controller component in the overall architecture. It doesn’t matter to the Model component what happens to the data when it is passed to the View or Controller components.
The code snippet for running first_model.php is:
The View requests for data from the Model component and then its final output is determined. View interacts with the user, and then transfers the user’s reaction to the Controller component to respond accordingly. An example of this is a link generated by the View component, when a user clicks and an action gets triggered in the Controller.
To run first_view.php, type:
The Controller’s job is to handle data that the user inputs or submits through the forms, and then Model updates this accordingly in the database. The Controller is nothing without the user’s interactions, which happen through the View component.
The code snippet for running first_controller.php is:
A simple way to understand how MVC works is given below.
1) A user interacts with View.
2) The Controller handles the user input, and sends the information to the model.
3) Then the Model receives the information and manipulates it (either saving it or updating it by communicating with the database).
4) The View checks the state of the Model and responds accordingly (lists updated information).
In the following code snippet, by running first_example.php we can see our MVC architecture work with PHP.
To run first_example.php, you must type: