How Exactly Is a PHP Script Executed

What gets executed first? Is it script inside php tag or HTML?

PHP gets executed on the server, output of this execution is a HTML code.

The browser takes the HTML code and displays it.
Sample Image

How does php script executes


How does php script executes. For example I have a file that is being called with certain parameters. Let's say the script first start processing the request, then connect to a database where it inserts values into a table then displays or logs a report in text file. Lets say the PHP file was in the middle of inserting data into the database and another request comes. Will it process this second request?

A separate instance of PHP will be used.

Talking bout PayPal API and esp IPN Listner, PayPal advices to keep the Listener minimal. So lets say my listener just got a request of payment notification with about 20 parameters. Now I am going to save these into the database but at the same time want to get free quick so I can catch other requests as well. What should be my logic here? Use CURL or something similar to post the request to another page where data is inserted into the table? That will keep my primary listener less loaded and quick. Is this the correct aproach? Or I should do data insertion in the primary listener.

No, cURL is blocking, so that'd just make things worse. Just do it in the main PHP file that's being accessed - if 20 people hit it all at once, your web server should have no trouble keeping them separate.

How does PHP works and what is its architecture ?

The biggest difference between a Java web server and PHP is that PHP doesn't have its own built-in web server. (Well, newer versions do, but it's supposed to be for testing only, it's not a production ready web server.) PHP itself is basically one executable which reads in a source code file of PHP code and interprets/executes the commands written in that file. That's it. That's PHP's architecture in a nutshell.

That executable supports a default API which the userland PHP code can call, and it's possible to add extensions to provide more APIs. Those extensions are typically written in C and compiled together with the PHP executable at install time. Some extensions can only be added by recompiling PHP with additional flags, others can be compiled against a PHP install and activated via a configuration file after the fact. PHP offers the PEAR and PECL side projects as an effort to standardise and ease such after-the-fact installs. Userland PHP code will often also include additional third party libraries simply written in PHP code. The advantage of C extensions is their execution speed and low-level system access, the advantage of userland code libraries is their trivial inclusion. If you're administering your own PHP install, it's often simple enough to add new PHP extensions; however on the very popular shared-host model there's often a tension between what the host wants to install and what the developer needs.

In practice a web service written in PHP runs on a third party web server, very often Apache, which handles any incoming requests and invokes the PHP interpreter with the given requested PHP source code file as argument, then delivers any output of that process back to the HTTP client. This also means there's no persistent PHP process running at all times with a persistent state, like Java typically does, but each request is handled by starting up and then tearing down a new PHP instance.

While Java simply saves persistent data in memory, data persistence between requests in PHP is handled via a number of methods like memcache, sessions, databases, files etc.; depending on the specific needs of the situation. PHP does have opcode cache addons, which kind of work like Java byte code, simply so PHP doesn't have to repeat the same parse and compile process every single time it's executing the same file.

Do keep in mind that it's entirely feasible to write a persistent PHP program which keeps running just like Java, it's simply not PHP's default modus operandi. Personally I'm quite a fan of writing workers for specific tasks on Gearman or ZMQ which run persistently, and have some ephemeral scripts running on the web server as "frontend" which delegate work to those workers as needed.

If this sounds like a typical PHP app is much more of a glued-together accumulation of several disparate components, you'd be correct. Java is pretty self-contained, except for external products like RDBMS servers. PHP on the other hand often tends to rely on a bunch of third party products; which can work to its advantage in the sense that you can use best-of-breed products for specific tasks, but also requires more overhead of dealing with different systems.

How does PHP act when executed by a shell script?

The difference is that you won't have $_GET, $_POST and other http specific stuff available. Sessions also won't work (why would you need them?). You obviously won't be able to set cookies, headers and other such things.

Other than that you can pretty much ignore the fact that you are "in a shell".

See here for more details: http://www.php.net/manual/en/features.commandline.differences.php

How long can a php code execute?

The default max execution time is 30 seconds. You can easily modify this by going to your php.ini file and changing max_execution_time option.

If you wish to modify max execution time within your PHP script, you can use this function: set_time_limit ( int $seconds ), you can also revert these changes later.

//Settings time limit to 0 will make the script execute without time limit(see comments).
set_time_limit (0);

foreach($users as $user){
//code to send the email
}

set_time_limit (30);


Related Topics



Leave a reply



Submit