<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Abhi&#039;s Weblog &#187; Zend</title>
	<atom:link href="http://abhinavsingh.com/blog/tag/zend/feed/" rel="self" type="application/rss+xml" />
	<link>http://abhinavsingh.com/blog</link>
	<description>PHP, Memcached, XMPP and Web Development</description>
	<lastBuildDate>Mon, 27 Feb 2012 09:12:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>How does PHP echo&#039;s a &quot;Hello World&quot;? &#8211; Behind the scene</title>
		<link>http://abhinavsingh.com/blog/2008/11/how-does-php-echos-a-hello-world-behind-the-scene/</link>
		<comments>http://abhinavsingh.com/blog/2008/11/how-does-php-echos-a-hello-world-behind-the-scene/#comments</comments>
		<pubDate>Wed, 26 Nov 2008 16:53:55 +0000</pubDate>
		<dc:creator>Abhinav Singh</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[MINIT]]></category>
		<category><![CDATA[MSHUTDOWN]]></category>
		<category><![CDATA[PHP Core]]></category>
		<category><![CDATA[PHP Extension]]></category>
		<category><![CDATA[RINIT]]></category>
		<category><![CDATA[RSHUTDOWN]]></category>
		<category><![CDATA[SAPI]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[Zend Engine]]></category>

		<guid isPermaLink="false">http://abhinavsingh.com/blog/2008/11/how-does-php-echos-a-hello-world-behind-the-scene/</guid>
		<description><![CDATA[Have you ever wondered how PHP echo&#8217;s a &#8220;Hello World&#8221; for you on the browser? Even I didn&#8217;t until I read about the PHP internals and extensions. I thought may be a few out there will be interested in exploring the other side of PHP, so here we go. In my last post I discussed [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fabhinavsingh.com%2Fblog%2F2008%2F11%2Fhow-does-php-echos-a-hello-world-behind-the-scene%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fabhinavsingh.com%2Fblog%2F2008%2F11%2Fhow-does-php-echos-a-hello-world-behind-the-scene%2F&amp;source=imoracle&amp;style=normal&amp;service=bit.ly&amp;service_api=R_f027b5a79a20a49b713f16282f1e0857&amp;hashtags=Apache,MINIT,MSHUTDOWN,PHP,PHP+Core,PHP+Extension,RINIT,RSHUTDOWN,SAPI,Zend,Zend+Engine&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Have you ever wondered how PHP echo&#8217;s a &#8220;Hello World&#8221; for you on the browser? Even I didn&#8217;t until I read about the PHP internals and extensions. I thought may be a few out there will be interested in exploring the other side of PHP, so here we go.</p>
<p>In my last post I discussed in brief <span style="font-style: italic;">&#8220;How your browser reaches to my server when you type </span><a style="font-style: italic;" href="http://abhinavsingh.com">http://abhinavsingh.com</a><span style="font-style: italic;"> in address bar?&#8221;</span>. <a href="http://abhinavsingh.com/blog/2008/11/what-happens-before-you-finally-viewed-this-page/">Read through</a> if you have missed out on that. Here I will discuss in brief <span style="font-style: italic;">&#8220;How does PHP churns out the content requested on the webpage?&#8221;</p>
<p></span><span style="font-weight: bold; text-decoration: underline; color: rgb(51, 51, 255);">An Overview</span><span style="font-style: italic;"><br />
</span>Here is what happens step-wise:</p>
<ol>
<li>We never start any PHP daemon or anything by ourself. When we start Apache, it starts the PHP interpreter along itself</li>
<li>PHP is linked to Apache (In general term SAPI i.e. a Server API) using mod_php5.so module</li>
<li>PHP as a whole consists of 3 modules (Core PHP, Zend Engine and Extension Layer)
</li>
<li>Core PHP is the module which handles the requests, file streams, error handling and other such operations</li>
<li>Zend Engine(ZE) is the one which converts human readable code into machine understandable tokens/op-codes. Then it executes this generate code into a Virtual Machine.</li>
<li>Extensions are a bunch of functions, classes, streams made available to the PHP scripts, which can be used to perform certain tasks. For example, we need mysql extension to connect to MySQL database using PHP.</li>
<li>While Zend Engine executes the generated code, the script might require access to a few extensions. Then ZE passes the control to the extension module/layer which transfer back the control to ZE after completion of tasks.</li>
<li>Finally Zend Engine returns back the result to PHP Core, which gives that to SAPI layer, and finally which displays it on your browser.</li>
</ol>
<p><span style="font-weight: bold; text-decoration: underline; color: rgb(102, 51, 255);">A Step Deeper</span><br />
But wait! This is still not over yet. Above was just a high level flow diagram. Lets dig a step deeper and see what more is happening behind the scenes:</p>
<ol>
<li>When we start Apache, it also starts PHP interpreter along itself</li>
<li>PHP startup happens in 2 steps</li>
<li>1st step is to perform initial setup of structures and values that persists for the life of SAPI</li>
<li>2nd step is for transient settings that only last for a single page request</li>
</ol>
<p><span style="font-weight: bold; text-decoration: underline; color: rgb(102, 51, 255);">Step 1 of PHP Startup</span><br />
Confused over what&#8217;s step 1 and 2 ? No worries, next we will discuss the same in a bit more detail. Lets first see step 1, which is basically the main stuff.<br />
Remember step 1 happens even before any page request is being made.</p>
<ol>
<li>As we start Apache, it starts PHP interpreter</li>
<li>PHP calls <span style="font-weight: bold;">MINIT method</span> of each extension, which is being enabled. View your php.ini file to see the modules which are being enabled by default</li>
<li>MINIT refers to Module Initialization. Each Module Initialization method initializes and define a set of functions, classes which will be used by future page requests</li>
</ol>
<p>A typical MINIT method looks like:</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PHP_MINIT_FUNCTION(extension_name) {</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Initialize functions, classes etc */</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p><span style="font-weight: bold; text-decoration: underline; color: rgb(102, 51, 255);">Step 2 of PHP Startup<br />
</span>
<ol>
<li><span style="color: rgb(0, 0, 0);">When a page request is being made, SAPI layer gives control to PHP layer. PHP then set up an environment to execute the PHP page requested. In turn it also create a symbol table which will store various variables being used while executing this page.</span></li>
<li><span style="color: rgb(0, 0, 0);"></span>PHP then calls the <span style="font-weight: bold;">RINIT method</span> of each module. RINIT refers to Request Initialization Module. Classic example of RINIT module implementation is the Session&#8217;s module. If enabled in php.ini, the RINIT method of Sessions module will pre-populate the $_SESSION variable and save in the symbol table.
</li>
<li>RINIT method can be thought as an <span style="font-style: italic;">auto_prepend_file directive</span>, which is pre-appended to every PHP script before execution.</li>
</ol>
<p>A typical RINIT method looks like:</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PHP_RINIT_FUNCTION(extension_name) {</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Initialize session variables, pre-populate variables, redefine global variables etc */</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p><span style="font-weight: bold; text-decoration: underline; color: rgb(102, 51, 255);">Step 1 of PHP Shutdown<br />
</span><span style="color: rgb(0, 0, 0);">Just like PHP startup, shutdown also happens in 2 steps.<br />
</span>
<ol>
<li>After the page execution is complete either by reaching the end of the script or by call of any exit() or die() function, PHP starts the cleanup process. In turn it calls <span style="font-weight: bold;">RSHUTDOWN method</span> of every extension. RSHUTDOWN can be thought as <span style="font-style: italic;">auto_append_file directive</span> to every PHP script, which no matter what happens, is always executed.</li>
<li>RSHUTDOWN method, destroys the symbols table (memory management) by calling <span style="font-weight: bold;">unset()</span> on all variables in the symbols table</li>
</ol>
<p>A typical RSHUTDOWN method looks like:</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PHP_RSHUTDOWN_FUNCTION(extension_name) {</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Do memory management, unset all variables used in the last PHP call etc */</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p><span style="font-weight: bold; text-decoration: underline; color: rgb(102, 51, 255);">Step 2 of PHP Shutdown<br />
</span><span style="color: rgb(102, 51, 255);"><span style="color: rgb(0, 0, 0);">Finally when all requests has been made and SAPI is ready to shutdown, PHP call its 2nd step of shutdown process.<br />
</span></span>
<ol>
<li>PHP calls the <span style="font-weight: bold;">MSHUTDOWN method</span> of every extension, which is basically the last chance for every extension to unregister handlers and free any persistent memory allocated during the MINIT cycle.</li>
</ol>
<p>A typical RSHUTDOWN method looks like:</p>
<p> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PHP_MSHUTDOWN_FUNCTION(extension_name) {</p>
<p> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Free handlers and persistent memory etc */</p>
<p> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p>And that brings us to the end of what we can call as PHP Lifecycle. Important point to note is that Step 1 of Startup and Step 2 of Shutdown happens when no request is being made to the web servers.</p>
<p>I hope this post will clear many doubts and un-answered questions which you might have.</p>
<p>Do leave a comment and feedbacks.</p>
<script type="text/javascript">var wordpress_toolbar_urls = ["http:\/\/api.tweetmeme.com\/share?url=http%3A%2F%2Fabhinavsingh.com%2Fblog%2F2008%2F11%2Fhow-does-php-echos-a-hello-world-behind-the-scene%2F","http:\/\/abhinavsingh.com"];var wordpress_toolbar_url = "http://abhinavsingh.com/blog/wp-content/plugins/wordpress-toolbar/toolbar.php";var wordpress_toolbar_oinw = "oinw";var wordpress_toolbar_hash = "aHR0cDovL2FiaGluYXZzaW5naC5jb20vYmxvZy8yMDA4LzExL2hvdy1kb2VzLXBocC1lY2hvcy1hLWhlbGxvLXdvcmxkLWJlaGluZC10aGUtc2NlbmUvPHdwdGI%2BSG93IGRvZXMgUEhQIGVjaG8mIzAzOTtzIGEgJnF1b3Q7SGVsbG8gV29ybGQmcXVvdDs%2FICYjODIxMTsgQmVoaW5kIHRoZSBzY2VuZTx3cHRiPmh0dHA6Ly9hYmhpbmF2c2luZ2guY29tL2Jsb2c8d3B0Yj5BYmhpJiMwMzk7cyBXZWJsb2c%3D";</script><ul class="related_post"><li><a href="http://abhinavsingh.com/blog/2008/12/php-extensions-how-and-why/" title="PHP Extensions &#8211; How and Why?">PHP Extensions &#8211; How and Why?</a> (34)</li><li><a href="http://abhinavsingh.com/blog/2009/11/php-tokens-and-opcodes-3-useful-extensions-for-understanding-the-working-of-zend-engine/" title="PHP tokens and opcodes : 3 useful extensions for understanding the working of Zend Engine">PHP tokens and opcodes : 3 useful extensions for understanding the working of Zend Engine</a> (10)</li><li><a href="http://abhinavsingh.com/blog/2008/07/how-to-get-started-with-web-development/" title="How to get started with web development?">How to get started with web development?</a> (9)</li><li><a href="http://abhinavsingh.com/blog/2008/05/how-to-configure-ubuntu-and-lamp-on-windows/" title="How to configure Ubuntu and LAMP on Windows">How to configure Ubuntu and LAMP on Windows</a> (4)</li><li><a href="http://abhinavsingh.com/blog/2008/05/web-development-part-1-apache-mysql-php/" title="Web Development &#8211; Part 1: Apache, MySQL, PHP">Web Development &#8211; Part 1: Apache, MySQL, PHP</a> (4)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://abhinavsingh.com/blog/2008/11/how-does-php-echos-a-hello-world-behind-the-scene/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
	</channel>
</rss>

