Php how does it work




















We have multiple executions of our PHP code, but none of the pink bars overlap. Each execution is able to fetch the current number of page views, increment it by one and then save it again. But what happens if multiple executions of our PHP code overlap? Well, this is where things start to break down. As you can see, the execution of our page view tracking PHP code overlaps now. This code overlap is our race condition. Because of it, the page view increment from the second process is incorrect.

So it counts the same number of page views as the first process. Because of the race condition, the third process also overwrites the number of page views recorded by the second process.

If we had more concurrent processes, this would compound the problems caused by the race condition. The result is an inaccurate tracking of the number of page views. So how do we fix the race condition in our page view tracking PHP code?

The obvious solution is to not do it and use an external service like Google Analytics. Well, the problem with race conditions is that there are countless ways to fix them. The key to fixing a race condition is the concept of mutual exclusion. Mutual exclusion is itself a property of larger topic called concurrency control. And the point of concurrency control is to ensure that concurrent operations like our tracking of page views generate the correct results.

Mutual exclusion is the idea that only one execution can enter a critical piece of code at a time. And how can we implement mutual exclusion with PHP? One of the common ways to implement it is with a lock. You use a lock to limit the access to a resource. In this scenario, this would be the number of page views that we only want one process to access at a time.

This timeline shows what happens to our page view tracking code when we use a lock. The yellow bar in our timeline represents the time spent waiting for the other execution to release the lock. Our page view tracking code can only begin when that happens. For example, you could use a semaphore. This is a special variable or data type that you can use to determine if a resource is in use or not.

Another option besides using a lock is to use message passing. Message passing revolves around isolating a critical piece of code inside your web application. Once isolated, your code should never make a call to this piece of code ever again. Instead, it should send a message to the web application to run it for you. So, in the case of our page view tracking code, we would isolate that code inside our application.

We would then replace that code with code that sends a message to our application. This message would tell our application to run this code for us. But how is that different from making a call to our page view tracking code? The critical element to message passing is that you send the message outside the current process. And that process can enforce mutual exclusion for that critical piece of code.

This ends up preventing the race condition. A lot of PHP applications use a message queue to implement their messaging system. The PHP application will send a message to the message queue. The message queue will then process each message one at a time in the order that it received them.

There are a lot of choices from message queue systems. Some frameworks such as Laravel also have their own message queue system. As you can see, PHP applications are in a category of their own. This is especially true if they come from other programming languages.

That said, there are also some common elements with other programming languages. PHP Web Application. Hi Carl, great article! I love this type of articles. But when I saw the title I was expecting something more like how PHP works … I mean … compiler architecture and internals, opcode , caching … but anyway, worth the reading. Yeah, I can see the confusion. Thank you for the time you spent to make this beautifully written article.

It helps me a lot to understand things that I was still unsure this whole time about PHP easily. How are we doing? Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How does PHP works and what is its architecture? Ask Question. Asked 7 years, 4 months ago.

Active 2 years, 3 months ago. Viewed 14k times. Now, what about PHP? How does it work? What, in a nutshell, is its architecture. Improve this question. Adelin Adelin Pretty much the virtual machine and compiling are The web server architecture depends on the web server, it's not language specific here — Royal Bg. A little off-topic, but if you're starting with PHP, I would recommend you phptherightway as a very useful resource on how to get started properly.

Well, as Java is more for Enterprise and corporate applications. PHP is perfectly capable of being used for enterprise applications; the fact that most people use it for simple websites doesn't preclude it from being used for the enterprise — Mark Baker. Show 1 more comment. PHP scripts are executed on the server. The output is returned in form of HTML. Skip to content.

Report a Bug. Previous Prev. Next Continue. Home Testing Expand child menu Expand. SAP Expand child menu Expand. Web Expand child menu Expand. Must Learn Expand child menu Expand. Big Data Expand child menu Expand.



0コメント

  • 1000 / 1000