<?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>PHP programming language &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/php-programming-language/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>PHP Strings Don&#8217;t Need Quotes</title>
		<link>/2012/04/26/php-strings-dont-need-quotes/</link>
		<comments>/2012/04/26/php-strings-dont-need-quotes/#comments</comments>
		<pubDate>Thu, 26 Apr 2012 13:52:53 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Cross-platform software]]></category>
		<category><![CDATA[Curly bracket programming languages]]></category>
		<category><![CDATA[PHP interpreter]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[Procedural programming languages]]></category>
		<category><![CDATA[Programming language]]></category>
		<category><![CDATA[Scripting languages]]></category>
		<category><![CDATA[Social Issues]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[String]]></category>

		<guid isPermaLink="false">/?p=3017</guid>
		<description><![CDATA[I bet you didn&#8217;t know that PHP strings don&#8217;t need quotes! Indeed PHP developers work with strings with either single or double quotes, but actually in some cases you don&#8217;t need them. PHP by Book Here&#8217;s how PHP developer declare a string, which is something very common in any programming language. $my_var = 'hello world'; &#8230; <a href="/2012/04/26/php-strings-dont-need-quotes/" class="more-link">Continue reading <span class="screen-reader-text">PHP Strings Don&#8217;t Need Quotes</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2011/07/12/a-javascript-trick-you-should-know/" rel="bookmark" title="A JavaScript Trick You Should Know">A JavaScript Trick You Should Know </a></li>
<li><a href="/2010/03/10/php-if-else-endif-statements/" rel="bookmark" title="PHP if-else-endif Statements">PHP if-else-endif Statements </a></li>
<li><a href="/2011/08/18/powerful-php-less-known-string-manipulation/" rel="bookmark" title="Powerful PHP: Less Known String Manipulation">Powerful PHP: Less Known String Manipulation </a></li>
<li><a href="/2010/06/10/json-and-zend-framework-zend_json/" rel="bookmark" title="JSON and Zend Framework? &#8211; Zend_Json">JSON and Zend Framework? &#8211; Zend_Json </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>I bet you didn&#8217;t know that PHP strings don&#8217;t need quotes! Indeed PHP developers work with strings with either single or double quotes, but actually in some cases you don&#8217;t need them.</p>
<h2>PHP by Book</h2>
<p>Here&#8217;s how PHP developer declare a string, which is something very common in any programming language.</p>
<pre lang="PHP">
$my_var = 'hello world';
// or
$my_var = "hello world";
</pre>
<h2>PHP Tricks</h2>
<p>What if you do the following:</p>
<pre lang="PHP">
echo hello;
</pre>
<p>That appears to be correct &#8230; Well, it&#8217;s not absolutely correct. You&#8217;ll be &#8220;noticed&#8221;.</p>
<pre lang="PHP">
// Notice: Use of undefined constant hello
echo hello;
</pre>
<p>However if you disable error reporting, the code will be completely fine.</p>
<pre lang="PHP">
error_reporting(0);

// no problem now
echo hello;
</pre>
<h2>Variations</h2>
<p>What follows from the thing above is that you can use strings without quotes:</p>
<pre lang="PHP">
// hello
echo hello;

// hello world (concatenated)
echo hello . ' world';

// helloworld
echo hello . world;
</pre>
<p>However you can&#8217;t have spaces and most of the &#8220;special&#8221; symbols.</p>
<pre lang="PHP">
// syntax error
echo hello world;

// syntax error
echo hello!;
</pre>
<h2>Final Words</h2>
<p>Although you can do this in PHP, that is completely wrong. The code becomes more difficult to read and understand. In the second place you can miss a $ sign in front of a variable declaration and thus the PHP interpreter will assume this is a string. So disable error reporting isn&#8217;t so great sometimes.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2011/07/12/a-javascript-trick-you-should-know/" rel="bookmark" title="A JavaScript Trick You Should Know">A JavaScript Trick You Should Know </a></li>
<li><a href="/2010/03/10/php-if-else-endif-statements/" rel="bookmark" title="PHP if-else-endif Statements">PHP if-else-endif Statements </a></li>
<li><a href="/2011/08/18/powerful-php-less-known-string-manipulation/" rel="bookmark" title="Powerful PHP: Less Known String Manipulation">Powerful PHP: Less Known String Manipulation </a></li>
<li><a href="/2010/06/10/json-and-zend-framework-zend_json/" rel="bookmark" title="JSON and Zend Framework? &#8211; Zend_Json">JSON and Zend Framework? &#8211; Zend_Json </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/04/26/php-strings-dont-need-quotes/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>How to Dump the Generated Zend_Db SQL Query</title>
		<link>/2012/02/09/how-to-dump-the-generated-zend_db-sql-query/</link>
		<comments>/2012/02/09/how-to-dump-the-generated-zend_db-sql-query/#comments</comments>
		<pubDate>Thu, 09 Feb 2012 14:39:48 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[Cross-platform software]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[From]]></category>
		<category><![CDATA[Insert]]></category>
		<category><![CDATA[PHP programmer]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[select]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL keywords]]></category>

		<guid isPermaLink="false">/?p=2709</guid>
		<description><![CDATA[The Typical PHP Approach Typically a PHP programmer will write his SQL query as a string and will execute it via mysql_query. $sql = "SELECT * FROM my_table"; $resource = mysql_query($sql); So eventually when you want to dump this &#8220;complex&#8221; query, or whatever query there is, you can simply &#8220;echo&#8221; it and see what’s its &#8230; <a href="/2012/02/09/how-to-dump-the-generated-zend_db-sql-query/" class="more-link">Continue reading <span class="screen-reader-text">How to Dump the Generated Zend_Db SQL Query</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/04/29/debugging-in-zend-framework/" rel="bookmark" title="Debugging in Zend Framework">Debugging in Zend Framework </a></li>
<li><a href="/2010/08/05/fetching-rows-with-zend_db-fetch/" rel="bookmark" title="Fetching Rows With Zend_Db fetch()">Fetching Rows With Zend_Db fetch() </a></li>
<li><a href="/2010/07/28/which-model-should-contain-that-method/" rel="bookmark" title="Which Model Should Contain That Method?">Which Model Should Contain That Method? </a></li>
<li><a href="/2010/04/26/mysql-expressions-in-zend-framework/" rel="bookmark" title="MySQL Expressions in Zend Framework">MySQL Expressions in Zend Framework </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>The Typical PHP Approach</h2>
<p>Typically a PHP programmer will write his SQL query as a string and will execute it via <a href="http://php.net/manual/en/function.mysql-query.php" title="PHP: mysql_query" target="_blank">mysql_query</a>.</p>
<pre lang="PHP">
$sql = "SELECT * FROM my_table";
$resource = mysql_query($sql);
</pre>
<p>So eventually when you want to dump this &#8220;complex&#8221; query, or whatever query there is, you can simply &#8220;echo&#8221; it and see what’s its syntax. </p>
<pre lang="PHP">
// this query is WRONG because of the where clause
$sql = "SELECT * FROM my_table WHERE id = ";

// dump and debug the wrong query
die($sql);

// this line won't be executed
$resource = mysql_query($sql);
</pre>
<p>So far so good, but things appear to be a bit different when you start to work with <a href="http://framework.zend.com/" title="Zend Framework" target="_blank">Zend Framework</a>. Higher levels of abstraction come with slightly more difficult ways to dump (debug) your SQL queries.</p>
<p>OK you&#8217;ve two options. Using <a href="http://framework.zend.com/manual/en/zend.db.select.html" title="Zend_Db_Select" target="_blank">Zend_Db_Select</a> or &#8230; not.<br />
<span id="more-2709"></span></p>
<h2>Dump Zend_Db_Select Query</h2>
<p>Debugging queries generated with Zend_Db_Select is as easy as the following snippet. Let’s say there’s a db table “users” and typically the model is called Users.</p>
<pre lang="PHP">
class Users extends Zend_Db_Table
{
	protected $_name = 'users';

	public function getSomeUsers()
	{
		$select = $this->select()
				->from(this->_name)
				->where('id > ');

		$rows = $this->fetchAll($select);
	}
}
</pre>
<p>Obviously this code is wrong, because the &#8220;where&#8221; clause is wrong and we don&#8217;t pass any values to it. So now the question is how can we dump this SQL. Well, since we use Zend_Db_Select, so the simplest way to dump the SQL is by calling __toString(). The code above will become as follows.</p>
<pre lang="PHP">
class Users extends Zend_Db_Table
{
	protected $_name = ‘users’;

	public function getSomeUsers()
	{
		$select = $this->select()
				->from(this->_name)
				->where('id > ');

		die($select->__toString());

		$rows = $this->fetchAll($select);
	}
}
</pre>
<p>That&#8217;s great, but how can we dump update/insert queries for instance?</p>
<h2>Dump with Zend_Db_Profiler</h2>
<p>The way you should dump insert or update queries is a bit different. You should use <a href="http://framework.zend.com/manual/en/zend.db.profiler.html" title="Zend_Db_Profiler" target="_blank">Zend_Db_Profiler</a>, which is a more complex way to debug (profile) your queries. You can not only debug insert or update queries, of course, but any queries generated by the Zend_Db abstraction. Let’s see how.</p>
<p>Now from a controller perspective this model can be just called as:</p>
<pre lang="PHP">
$users = new Users();
$users->update(array('name' => 'my name'), 'id =');
</pre>
<p>Obviously this code is wrong, again because of the where clause and now the question is how we can debug the generated SQL. The answer is: using Zend_Db_Profiler. Looking back again to the model, this should look like this.</p>
<pre lang="PHP">
class Users extends Zend_Db_Table
{
	protected $_name = ‘users’;

	public function updateSomeUsers()
	{
		// first enable the profiler
		$this->getProfiler()->setEnabled(true);

		// try to execute
		$this->update(array('name' => 'my name'), 'id = ');

		// dump the SQL
		Zend_Debug::dump($this->getProfiler()->getLastQueryProfile()->getQuery());

		// don't forget to disable the profiler is
		// you don't need it anymore
		$this->getProfiler()->setEnabled(false);
	}
}
</pre>
<p>The thing is that since Zend_Db is using prepared statements, the last row (the one with the Zend_Debug::&#8230;) will dump something like the following line.</p>
<pre lang="SQL">
UPDATE `users` SET `name` = ? WHERE (id =)
</pre>
<p>So as you can see, actually here you don’t see the name’s value, which should be “my name”. To see this you should call getQueryParams().</p>
<pre lang="PHP">
class Users extends Zend_Db_Table
{
	protected $_name = 'users';

	public function updateSomeUsers()
	{
		// first enable the profiler
		$this->getProfiler()->setEnabled(true);

		// try to execute
		$this->update(array('name' => 'my name'), 'id = ');

		// dump the SQL
		Zend_Debug::dump($this->getProfiler()->getLastQueryProfile()->getQuery());
		Zend_Debug::dump($this->getProfiler()->getLastQueryProfile()->getQueryParams());

		// don't forget to disable the profiler is
		// you don't need it anymore
		$this->getProfiler()->setEnabled(false);
</pre>
<p>Now the debug info will be something like this.</p>
<pre lang="PHP">
UPDATE `users` SET `name` = ? WHERE (id =)
array
	1 => string 'my name' (length 7)
</pre>
<p>Calling this from the controller there are some changes. Take a look at the example bellow.</p>
<pre lang="PHP">
$users = new Users();

// enable the profiler
$users->getAdapter()->getProfiler()->setEnabled(true);

// execute (or at least try to)
$users->update(array('name' => 'my name'), 'id =');

// debugdump the SQL
Zend_Debug::dump($users->getAdapter()->getProfiler()->getLastQueryProfile()->getQuery());
Zend_Debug::dump($users->getAdapter()->getProfiler()->getLastQueryProfile()->getQueryParams());

// don't forget to disable the profiler is
// you don't need it anymore
$users->getAdapter()->getProfiler()->setEnabled(false);
</pre>
<p>In this case you should use the longer model_name->getAdapter()</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/04/29/debugging-in-zend-framework/" rel="bookmark" title="Debugging in Zend Framework">Debugging in Zend Framework </a></li>
<li><a href="/2010/08/05/fetching-rows-with-zend_db-fetch/" rel="bookmark" title="Fetching Rows With Zend_Db fetch()">Fetching Rows With Zend_Db fetch() </a></li>
<li><a href="/2010/07/28/which-model-should-contain-that-method/" rel="bookmark" title="Which Model Should Contain That Method?">Which Model Should Contain That Method? </a></li>
<li><a href="/2010/04/26/mysql-expressions-in-zend-framework/" rel="bookmark" title="MySQL Expressions in Zend Framework">MySQL Expressions in Zend Framework </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/02/09/how-to-dump-the-generated-zend_db-sql-query/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Object Cloning and Passing by Reference in PHP</title>
		<link>/2011/10/27/object-cloning-and-passing-by-reference-in-php/</link>
		<comments>/2011/10/27/object-cloning-and-passing-by-reference-in-php/#comments</comments>
		<pubDate>Thu, 27 Oct 2011 14:25:17 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Clone]]></category>
		<category><![CDATA[Cloning]]></category>
		<category><![CDATA[Comparison of programming languages]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Curly bracket programming languages]]></category>
		<category><![CDATA[Java programming language]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[Procedural programming languages]]></category>
		<category><![CDATA[Scripting languages]]></category>
		<category><![CDATA[Software engineering]]></category>

		<guid isPermaLink="false">/?p=2408</guid>
		<description><![CDATA[In PHP everything&#8217;s a reference! I&#8217;ve heard it so many times in my practice. No, these words are too strong! Let&#8217;s see some examples. Passing Parameters by Reference Clearly when we pass parameters to a function it&#8217;s not by reference. How to check this? Well, like this. function f($param) { $param++; } $a = 5; &#8230; <a href="/2011/10/27/object-cloning-and-passing-by-reference-in-php/" class="more-link">Continue reading <span class="screen-reader-text">Object Cloning and Passing by Reference in PHP</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2012/08/17/its-not-true-that-php-arrays-are-copied-by-value/" rel="bookmark" title="It&#8217;s Not True that PHP Arrays are Copied by Value">It&#8217;s Not True that PHP Arrays are Copied by Value </a></li>
<li><a href="/2012/04/26/php-strings-dont-need-quotes/" rel="bookmark" title="PHP Strings Don&#8217;t Need Quotes">PHP Strings Don&#8217;t Need Quotes </a></li>
<li><a href="/2011/10/20/some-notes-on-the-object-oriented-model-of-php/" rel="bookmark" title="Some Notes on the Object-oriented Model of PHP">Some Notes on the Object-oriented Model of PHP </a></li>
<li><a href="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>In <a href="/category/php/" title="PHP at stoimen.com">PHP </a>everything&#8217;s a reference! I&#8217;ve heard it so many times in my practice. No, these words are too strong! Let&#8217;s see some examples.<br />
<figure id="attachment_2432" style="width: 480px" class="wp-caption alignnone"><a href="/wp-content/uploads/2011/10/ampersand.jpg"><img src="/wp-content/uploads/2011/10/ampersand.jpg" alt="Passing by reference in PHP can be tricky!" title="ampersand" width="480" height="480" class="size-full wp-image-2432" srcset="/wp-content/uploads/2011/10/ampersand.jpg 480w, /wp-content/uploads/2011/10/ampersand-150x150.jpg 150w, /wp-content/uploads/2011/10/ampersand-300x300.jpg 300w" sizes="(max-width: 480px) 100vw, 480px" /></a><figcaption class="wp-caption-text">Some developers think that everything&#039;s passed by reference in PHP.</figcaption></figure></p>
<h2>Passing Parameters by Reference</h2>
<p>Clearly when we pass parameters to a function it&#8217;s not by reference. How to check this? Well, like this.</p>
<pre lang="PHP">
function f($param)
{
	$param++;
}

$a = 5;
f($a);

echo $a;
</pre>
<p>Now the value of $a equals 5. If it were passed by reference, it would be 6. With a little change of the code we can get it.</p>
<pre lang="PHP">
function f(&$param)
{
	$param++;
}

$a = 5;
f($a);

echo $a;
</pre>
<p>Now the variable&#8217;s value is 6. </p>
<p>So far, so good. Now what about copying objects?<br />
<span id="more-2408"></span></p>
<h2>Objects: A Copy or a Cloning?</h2>
<p>We can check whether by assigning an object to a variable a reference or a copy of the object is passed.</p>
<pre lang="PHP">
class C
{
	public $myvar = 10;
}

$a = new C();
$b = $a;

$b->myvar = 20;

// 20, not 10
echo $a->myvar;
</pre>
<p>The last line outputs 20! This makes it clear. By assigning an object to a variable PHP pass its reference. To make a copy there&#8217;s another approach. We need to change $b = $a, to $b = clone $a;</p>
<pre lang="PHP" escaped="true">
class C
{
	public $myvar = 10;
}

$a = new C();
$b = clone $a;

$b->myvar = 20;

// 10
echo $a->myvar;
</pre>
<h2>Arrays by Reference</h2>
<p>What about arrays? What if I assign an array to a variable?</p>
<pre lang="PHP">
$a = array(20);

$b = $a;
$b[0] = 30;

var_dump($a);
</pre>
<p>What do you think is the value of $a[0]? Well, the answer is: 20! So $b is a copy of the array &#8220;a&#8221;. Instead you should assign explicitly its reference to make &#8220;b&#8221; point to &#8220;a&#8221;.</p>
<pre lang="PHP">
$a = array(20);

$b = &$a;
$b[0] = 30;

var_dump($a);
</pre>
<p>Now $a[0] equals 30!</p>
<p>I think this could be useful!</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2012/08/17/its-not-true-that-php-arrays-are-copied-by-value/" rel="bookmark" title="It&#8217;s Not True that PHP Arrays are Copied by Value">It&#8217;s Not True that PHP Arrays are Copied by Value </a></li>
<li><a href="/2012/04/26/php-strings-dont-need-quotes/" rel="bookmark" title="PHP Strings Don&#8217;t Need Quotes">PHP Strings Don&#8217;t Need Quotes </a></li>
<li><a href="/2011/10/20/some-notes-on-the-object-oriented-model-of-php/" rel="bookmark" title="Some Notes on the Object-oriented Model of PHP">Some Notes on the Object-oriented Model of PHP </a></li>
<li><a href="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2011/10/27/object-cloning-and-passing-by-reference-in-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>POST with Zend_Http_Client</title>
		<link>/2011/04/13/post-with-zend_http_client/</link>
		<comments>/2011/04/13/post-with-zend_http_client/#comments</comments>
		<pubDate>Wed, 13 Apr 2011 13:26:47 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[Computer networking]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[CURL]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[Hypertext Transfer Protocol]]></category>
		<category><![CDATA[Internet protocols]]></category>
		<category><![CDATA[Network protocols]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[Web 2.0]]></category>
		<category><![CDATA[World Wide Web]]></category>
		<category><![CDATA[Zend Technologies]]></category>

		<guid isPermaLink="false">/?p=2299</guid>
		<description><![CDATA[CURL and Zend_Http It&#8217;s a well know fact that you can preform HTTP requests with CURL. Zend Framework does the same job with Zend_Http. Especially Zend_Http_Client can be used to &#8220;replace&#8221; the usual client &#8211; the browser, and to perform some basic requests. I&#8217;ve seen mostly GET requests, although Zend_Http_Client can perform various requests such &#8230; <a href="/2011/04/13/post-with-zend_http_client/" class="more-link">Continue reading <span class="screen-reader-text">POST with Zend_Http_Client</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/06/28/send-authenticated-post-request-with-zend_http_client/" rel="bookmark" title="Send Authenticated POST Request with Zend_Http_Client">Send Authenticated POST Request with Zend_Http_Client </a></li>
<li><a href="/2010/04/14/zend_http_client-and-case-sensitivity/" rel="bookmark" title="Zend_Http_Client and Case Sensitivity">Zend_Http_Client and Case Sensitivity </a></li>
<li><a href="/2010/03/23/read-remote-file-content-type-with-zend_http_client/" rel="bookmark" title="Read Remote File Content-Type with Zend_Http_Client">Read Remote File Content-Type with Zend_Http_Client </a></li>
<li><a href="/2011/04/07/use-fopen-to-check-file-availability/" rel="bookmark" title="Use fopen() to Check File Availability?">Use fopen() to Check File Availability? </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>CURL and Zend_Http</h2>
<p>It&#8217;s a well know fact that you can preform <a title="Http Requests - the Hypertext Transfer Protocol" href="http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol" target="_blank">HTTP requests</a> with <a href="http://en.wikipedia.org/wiki/CURL" title="CURL" target="_blank">CURL</a>. <a title="Zend Framework" href="http://framework.zend.com/" target="_blank">Zend Framework</a> does the same job with <a title="Zend_Http" href="http://framework.zend.com/manual/en/zend.http.html" target="_blank">Zend_Http</a>. Especially <a title="Http on Stoimen.com" href="/tag/http/">Zend_Http_Client</a> can be used to &#8220;replace&#8221; the usual client &#8211; the browser, and to perform some basic requests.</p>
<figure id="attachment_2308" style="width: 450px" class="wp-caption aligncenter"><a href="/wp-content/uploads/2011/04/http.jpg"><img class="size-full wp-image-2308" title="HTTP requests can be performed with Zend_Http_Client" src="/wp-content/uploads/2011/04/http.jpg" alt="HTTP requests can be performed with Zend_Http_Client" width="450" height="337" srcset="/wp-content/uploads/2011/04/http.jpg 450w, /wp-content/uploads/2011/04/http-300x224.jpg 300w" sizes="(max-width: 450px) 100vw, 450px" /></a><figcaption class="wp-caption-text">Zend_Http_Client is mostly used to perform GET requests, but it can be also very helpful for POST HTTP requests.</figcaption></figure>
<p>I&#8217;ve seen mostly GET requests, although Zend_Http_Client can perform various requests such as <a title="POST Requests on Stoimen.com" href="/tag/post/">POST</a> as well.</p>
<pre lang="php">
// new HTTP request to some HTTP address
$httpClient = new Zend_Http_Client('http://www.example.com/');
// GET the response
$response = $httpClient->request(Zend_Http_Client::GET);
</pre>
<p>Here&#8217;s a little snippet showing how to POST some data to a server.</p>
<pre lang="php">
// new HTTP request to some HTTP address
$client = new Zend_Http_Client('http://www.example.com/');
// set some parameters
$client->setParameterPost('name', 'value');
// POST request
$response = $client->request(Zend_Http_Client::POST);
</pre>
<p>Note that the request method returns a response. Thus if you are simulating a form submit action you can &#8220;redirect&#8221; to the desired page just like the form.</p>
<pre lang="php">
// new HTTP request to some HTTP address
$client = new Zend_Http_Client('http://www.example.com/');
// set some parameters
$client->setParameterPost('name', 'value');
// POST request
$response = $client->request(Zend_Http_Client::POST);
echo $response->location;
</pre>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/06/28/send-authenticated-post-request-with-zend_http_client/" rel="bookmark" title="Send Authenticated POST Request with Zend_Http_Client">Send Authenticated POST Request with Zend_Http_Client </a></li>
<li><a href="/2010/04/14/zend_http_client-and-case-sensitivity/" rel="bookmark" title="Zend_Http_Client and Case Sensitivity">Zend_Http_Client and Case Sensitivity </a></li>
<li><a href="/2010/03/23/read-remote-file-content-type-with-zend_http_client/" rel="bookmark" title="Read Remote File Content-Type with Zend_Http_Client">Read Remote File Content-Type with Zend_Http_Client </a></li>
<li><a href="/2011/04/07/use-fopen-to-check-file-availability/" rel="bookmark" title="Use fopen() to Check File Availability?">Use fopen() to Check File Availability? </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2011/04/13/post-with-zend_http_client/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Use fopen() to Check File Availability?</title>
		<link>/2011/04/07/use-fopen-to-check-file-availability/</link>
		<comments>/2011/04/07/use-fopen-to-check-file-availability/#comments</comments>
		<pubDate>Thu, 07 Apr 2011 12:46:33 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[C file input/output]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[HEAD]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[Hypertext Transfer Protocol]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[Technology/Internet]]></category>

		<guid isPermaLink="false">/?p=2268</guid>
		<description><![CDATA[Zend Framework and Zend_Http_Client I&#8217;ve posted about Zend_Http_Client. Simply there you can &#8216;make&#8217; your own http client and you can request a remote file. Just to check what&#8217;s going on with this file. // new HTTP request to a file $httpClient = new Zend_Http_Client('http://www.example.com/myfile.mp4'); // get the HEAD of the response and match agains the &#8230; <a href="/2011/04/07/use-fopen-to-check-file-availability/" class="more-link">Continue reading <span class="screen-reader-text">Use fopen() to Check File Availability?</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/03/23/read-remote-file-content-type-with-zend_http_client/" rel="bookmark" title="Read Remote File Content-Type with Zend_Http_Client">Read Remote File Content-Type with Zend_Http_Client </a></li>
<li><a href="/2011/04/13/post-with-zend_http_client/" rel="bookmark" title="POST with Zend_Http_Client">POST with Zend_Http_Client </a></li>
<li><a href="/2010/06/28/send-authenticated-post-request-with-zend_http_client/" rel="bookmark" title="Send Authenticated POST Request with Zend_Http_Client">Send Authenticated POST Request with Zend_Http_Client </a></li>
<li><a href="/2010/04/14/zend_http_client-and-case-sensitivity/" rel="bookmark" title="Zend_Http_Client and Case Sensitivity">Zend_Http_Client and Case Sensitivity </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Zend Framework and Zend_Http_Client</h2>
<figure id="attachment_2287" style="width: 450px" class="wp-caption aligncenter"><a href="/wp-content/uploads/2011/04/file.jpg"><img class="size-full wp-image-2287" title="PHP's fopen() can be used to check remote file existence" src="/wp-content/uploads/2011/04/file.jpg" alt="PHP's fopen() can be used to check remote file existence" width="450" height="415" srcset="/wp-content/uploads/2011/04/file.jpg 450w, /wp-content/uploads/2011/04/file-300x276.jpg 300w" sizes="(max-width: 450px) 100vw, 450px" /></a><figcaption class="wp-caption-text">PHP&#39;s fopen() can be used to check remote file existence</figcaption></figure>
<p>I&#8217;ve posted about <a title="Zend Http Client Class Docs" href="http://framework.zend.com/manual/en/zend.http.client.advanced.html" target="_blank">Zend_Http_Client</a>. Simply there you can &#8216;make&#8217; your own http client and you can request a remote file. Just to check what&#8217;s going on with this file.</p>
<pre lang="php" escaped="true">// new HTTP request to a file
$httpClient = new Zend_Http_Client('http://www.example.com/myfile.mp4');

// get the HEAD of the response and match agains the
// Content-Length. That's because using the Content-Type is slower
$response = $httpClient-&gt;request(Zend_Http_Client::HEAD);

// if the Content-Length is 0 the file doesn't exists
if (0 === (int)$response-&gt;getHeader('Content-Length')) {
	echo 'the file doesn\'t exits';
}
</pre>
<p>However is there any other way to answer the same question?</p>
<h2>fopen()</h2>
<p>Yes and no? Perhaps yes, but you should be careful. I&#8217;m still not sure it can be used in any case. However here&#8217;s the snippet.</p>
<pre lang="php" escaped="true">if (FALSE === @fopen('http://www.example.com/myfile.mp4', 'r')) {
	echo 'the file doesn\'t exists';
}
</pre>
<p><a title="fopen() PHP Manpage" href="http://php.net/manual/en/function.fopen.php" target="_blank">fopen()</a> will return FALSE whenever the file doesn&#8217;t exists.</p>
<p>In both cases I request a remote file &#8211; an MPEG-4 file. Note that fopen()&#8217;s first parameter can be a HTTP resource.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/03/23/read-remote-file-content-type-with-zend_http_client/" rel="bookmark" title="Read Remote File Content-Type with Zend_Http_Client">Read Remote File Content-Type with Zend_Http_Client </a></li>
<li><a href="/2011/04/13/post-with-zend_http_client/" rel="bookmark" title="POST with Zend_Http_Client">POST with Zend_Http_Client </a></li>
<li><a href="/2010/06/28/send-authenticated-post-request-with-zend_http_client/" rel="bookmark" title="Send Authenticated POST Request with Zend_Http_Client">Send Authenticated POST Request with Zend_Http_Client </a></li>
<li><a href="/2010/04/14/zend_http_client-and-case-sensitivity/" rel="bookmark" title="Zend_Http_Client and Case Sensitivity">Zend_Http_Client and Case Sensitivity </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2011/04/07/use-fopen-to-check-file-availability/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
<enclosure url="http://www.example.com/myfile.mp4" length="0" type="video/mp4" />
		</item>
		<item>
		<title>Download Images with PHP</title>
		<link>/2011/01/18/download-images-with-php/</link>
		<comments>/2011/01/18/download-images-with-php/#comments</comments>
		<pubDate>Tue, 18 Jan 2011 13:48:38 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[CURL]]></category>
		<category><![CDATA[Hypertext Transfer Protocol]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[possible solution]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">/?p=2131</guid>
		<description><![CDATA[As it seems one possible solution while trying to download images with PHP is to write a &#8220;client&#8221; to do so. Will it be with cURL, Zend Framework or some other tool &#8211; it doesn&#8217;t matter. However one of the most used approaches is simply with file_get_contents and file_put_contents. I&#8217;m not sure whether I wrote &#8230; <a href="/2011/01/18/download-images-with-php/" class="more-link">Continue reading <span class="screen-reader-text">Download Images with PHP</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/09/10/automatically-upload-images-with-php-directly-from-the-uri/" rel="bookmark" title="Automatically Upload Images with PHP Directly from the URI">Automatically Upload Images with PHP Directly from the URI </a></li>
<li><a href="/2010/05/25/download-files-with-zend-framework/" rel="bookmark" title="Download Files with Zend Framework">Download Files with Zend Framework </a></li>
<li><a href="/2011/02/25/how-to-collect-the-images-and-meta-tags-from-a-webpage-with-php/" rel="bookmark" title="How to Collect the Images and Meta Tags from a Webpage with PHP">How to Collect the Images and Meta Tags from a Webpage with PHP </a></li>
<li><a href="/2011/04/07/use-fopen-to-check-file-availability/" rel="bookmark" title="Use fopen() to Check File Availability?">Use fopen() to Check File Availability? </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>As it seems one possible solution while trying to download images with PHP is to write a &#8220;client&#8221; to do so. Will it be with <a href="http://php.net/manual/en/book.curl.php" target="_blank">cURL</a>, <a href="http://zendframework.com/" target="_blank">Zend Framework</a> or some other tool &#8211; it doesn&#8217;t matter.</p>
<p>However one of the most used approaches is simply with <a href="http://php.net/manual/en/function.file-get-contents.php">file_get_contents</a> and <a href="http://www.php.net/manual/en/function.file-put-contents.php">file_put_contents</a>. I&#8217;m not sure whether I wrote already about this or not, but this solution simply looks something like this.</p>
<pre lang="php">

file_put_contents('/path/to/file', 
                  file_get_contents('http://www.example.com/source.image');

</pre>
<p>In fact a client will give you more control over the process, to handle errors, etc. So maybe this is a better solution.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/09/10/automatically-upload-images-with-php-directly-from-the-uri/" rel="bookmark" title="Automatically Upload Images with PHP Directly from the URI">Automatically Upload Images with PHP Directly from the URI </a></li>
<li><a href="/2010/05/25/download-files-with-zend-framework/" rel="bookmark" title="Download Files with Zend Framework">Download Files with Zend Framework </a></li>
<li><a href="/2011/02/25/how-to-collect-the-images-and-meta-tags-from-a-webpage-with-php/" rel="bookmark" title="How to Collect the Images and Meta Tags from a Webpage with PHP">How to Collect the Images and Meta Tags from a Webpage with PHP </a></li>
<li><a href="/2011/04/07/use-fopen-to-check-file-availability/" rel="bookmark" title="Use fopen() to Check File Availability?">Use fopen() to Check File Availability? </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2011/01/18/download-images-with-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>A Memcached Zend_Cache</title>
		<link>/2011/01/17/a-memcached-zend_cache/</link>
		<comments>/2011/01/17/a-memcached-zend_cache/#comments</comments>
		<pubDate>Mon, 17 Jan 2011 13:42:48 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[Cache]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[Computer memory]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Cross-platform software]]></category>
		<category><![CDATA[Memcached]]></category>
		<category><![CDATA[MMCache]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[ram]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[Web 2.0]]></category>
		<category><![CDATA[Zend_Cache]]></category>

		<guid isPermaLink="false">/?p=2121</guid>
		<description><![CDATA[Zend_Cache Usually Zend_Cache is used to store cache files on the file system, which can be really fast and useful in most of the cases. However there&#8217;s a faster cache mechanism and hopefully it&#8217;s supported by Zend_Cache as well. This is the Memcached backend. A Faster Cache Memcached is a really powerful tool to cache &#8230; <a href="/2011/01/17/a-memcached-zend_cache/" class="more-link">Continue reading <span class="screen-reader-text">A Memcached Zend_Cache</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/01/27/theory-of-caching-zend_cache-zend-optimizer/" rel="bookmark" title="Theory of caching. Zend_Cache &#038; Zend Optimizer.">Theory of caching. Zend_Cache &#038; Zend Optimizer. </a></li>
<li><a href="/2010/11/03/how-to-overcome-zend_cache_frontend_pages-problem-with-cookies/" rel="bookmark" title="How to Overcome Zend_Cache_Frontend_Page&#8217;s Problem with Cookies">How to Overcome Zend_Cache_Frontend_Page&#8217;s Problem with Cookies </a></li>
<li><a href="/2010/07/21/setting-up-global-cache-in-zend-framework/" rel="bookmark" title="Setting Up Global Cache in Zend Framework">Setting Up Global Cache in Zend Framework </a></li>
<li><a href="/2010/04/26/mysql-expressions-in-zend-framework/" rel="bookmark" title="MySQL Expressions in Zend Framework">MySQL Expressions in Zend Framework </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Zend_Cache</h2>
<p>Usually <a title="Zend_Cache" href="http://framework.zend.com/manual/en/zend.cache.html" target="_blank">Zend_Cache</a> is used to store cache files on the file system, which can be really fast and useful in most of the cases. However there&#8217;s a faster cache mechanism and hopefully it&#8217;s supported by Zend_Cache as well. This is the <a title="Memcached" href="http://memcached.org/" target="_blank">Memcached</a> backend.</p>
<h2>A Faster Cache</h2>
<p>Memcached is a really powerful tool to cache directly into the RAM. First, this tool has nothing to do primary with Zend Framework. It&#8217;s a server, usually started on some port, that can be called to store and get things from the memory. This of course is very fast, way faster than the cache in the hard drives.</p>
<h2>Zend_Cache and Memcached</h2>
<p>Zend_Cache has an interface to work with Memcached which is great as usual. The PHP example of Memcache (note that there are two things <a title="Memcache" href="http://php.net/manual/en/book.memcache.php" target="_blank">Memcache</a> and <a title="Memcached" href="http://memcached.org/" target="_blank">Memcached</a>, which are slight different) can be found here and as it says:</p>
<pre lang="php" escaped="true">
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");

$version = $memcache->getVersion();
echo "Server's version: ".$version."<br/>\n";

$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;

$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)<br/>\n";

$get_result = $memcache->get('key');
echo "Data from the cache:<br/>\n";

var_dump($get_result);
</pre>
<p>However this can be coded into a Zend Framework style like that:</p>
<pre lang="php">
$frontend = array('caching' => true, 'lifetime' => 1800, 'automatic_serialization' => true);

$backend = array(
    'servers' =>array(
        array('host' => '127.0.0.1', 'port' => 11211)
    ),
    'compression' => false
);

$cache = Zend_Cache::factory('Core', 'Memcached', $frontend, $backend);
</pre>
<p>Note that you don&#8217;t have the typical &#8220;cache_dir&#8221;, just because everything&#8217;s cached into the memory.</p>
<p>Now you can call the cache as it&#8217;s called with the &#8220;File&#8221; backend interface:</p>
<pre lang="php">
$key = 'mykey';

if (($result = $cache->load($key)) === false) {
	// call the slow database query here ...
	// save in $result
	
	$cache->save($result, $key);	
}

echo $result
</pre>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/01/27/theory-of-caching-zend_cache-zend-optimizer/" rel="bookmark" title="Theory of caching. Zend_Cache &#038; Zend Optimizer.">Theory of caching. Zend_Cache &#038; Zend Optimizer. </a></li>
<li><a href="/2010/11/03/how-to-overcome-zend_cache_frontend_pages-problem-with-cookies/" rel="bookmark" title="How to Overcome Zend_Cache_Frontend_Page&#8217;s Problem with Cookies">How to Overcome Zend_Cache_Frontend_Page&#8217;s Problem with Cookies </a></li>
<li><a href="/2010/07/21/setting-up-global-cache-in-zend-framework/" rel="bookmark" title="Setting Up Global Cache in Zend Framework">Setting Up Global Cache in Zend Framework </a></li>
<li><a href="/2010/04/26/mysql-expressions-in-zend-framework/" rel="bookmark" title="MySQL Expressions in Zend Framework">MySQL Expressions in Zend Framework </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2011/01/17/a-memcached-zend_cache/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to Overcome Zend_Cache_Frontend_Page&#8217;s Problem with Cookies</title>
		<link>/2010/11/03/how-to-overcome-zend_cache_frontend_pages-problem-with-cookies/</link>
		<comments>/2010/11/03/how-to-overcome-zend_cache_frontend_pages-problem-with-cookies/#comments</comments>
		<pubDate>Wed, 03 Nov 2010 14:19:25 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[Apache Corporation]]></category>
		<category><![CDATA[Cache]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[Computer memory]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[CPU cache]]></category>
		<category><![CDATA[Cross-platform software]]></category>
		<category><![CDATA[database server]]></category>
		<category><![CDATA[Google Inc.]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[HTTP cookie]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[MySQL Americas Inc.]]></category>
		<category><![CDATA[Page cache]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[script interpreter]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[web app]]></category>
		<category><![CDATA[web application caching]]></category>
		<category><![CDATA[Web application frameworks]]></category>
		<category><![CDATA[web server]]></category>
		<category><![CDATA[Zend Technologies]]></category>

		<guid isPermaLink="false">/?p=2024</guid>
		<description><![CDATA[Zend_Cache_Frontend_Page First of all there are several things to know about Zend Framework and caching. Whenever you work on a big web application caching is one of the mostly used mechanisms of speeding up the app and improve user performance. In general the task and the solution are pretty simple and natural. As the application &#8230; <a href="/2010/11/03/how-to-overcome-zend_cache_frontend_pages-problem-with-cookies/" class="more-link">Continue reading <span class="screen-reader-text">How to Overcome Zend_Cache_Frontend_Page&#8217;s Problem with Cookies</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2011/01/17/a-memcached-zend_cache/" rel="bookmark" title="A Memcached Zend_Cache">A Memcached Zend_Cache </a></li>
<li><a href="/2010/01/27/theory-of-caching-zend_cache-zend-optimizer/" rel="bookmark" title="Theory of caching. Zend_Cache &#038; Zend Optimizer.">Theory of caching. Zend_Cache &#038; Zend Optimizer. </a></li>
<li><a href="/2010/07/21/setting-up-global-cache-in-zend-framework/" rel="bookmark" title="Setting Up Global Cache in Zend Framework">Setting Up Global Cache in Zend Framework </a></li>
<li><a href="/2010/07/19/zend-framework-cache-database-table-schemes/" rel="bookmark" title="Zend Framework: Cache Database Table Schemes">Zend Framework: Cache Database Table Schemes </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Zend_Cache_Frontend_Page</h2>
<p>First of all there are several things to know about Zend Framework and caching. Whenever you work on a big web application caching is one of the mostly used mechanisms of speeding up the app and improve user performance. In general the task and the solution are pretty simple and natural.</p>
<p>As the application grows up the visitors become more and more impatient about what they receive. A single page is becoming slower and slower and the result is painful. First of all every time a user hits a page the application server uses the web server, a script interpreter, a database server and potentially the file system. But that&#8217;s not all. After all this output is generated on the server, as HTML in the most cases, it is sent to the client where again CSS and JavaScript engines parse and execute them.</p>
<p>In this scenario it&#8217;s easy to imagine how many time is spent. While there are several techniques to optimize the client side by optimizing JavaScript, CSS and the static images used for the design of the site, here I&#8217;m going to talk more about the backend.</p>
<h2>Beside the Optimization</h2>
<p>Let&#8217;s assume we&#8217;ve one of the very used combination between Apache (as a webserver), PHP (as server scripting language) and MySQL (as database server). Here you can choose to optimize all three of them. However beside the optimization of them one of the most simple steps you can do is to cache the output generated by these three branches of your web app.</p>
<h2>Caching the Content</h2>
<p>In fact you can cache only single parts of the whole process. For instance you can cache only the result returned by some slow database query. Let&#8217;s imagine a query takes about 2 seconds to execute. Now you can cache the result into a file and during the cache is active, i.e. it has not expired, the application takes it from a file stored somewhere in the file system.</p>
<p>In a typical Zend Framework scenario you can first setup the frontend and backend options of the cache.</p>
<pre lang="php">
$frontendOptions = array(
	'lifetime'                => 600, // in seconds - this is 10 seconds
	'automatic_serialization' => true,
);
$backendOptions = array('cache_dir' => 'cache/');
$cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
		
$cacheKey = md5('mykey');

if (!$cache->load($cacheKey)) {
	$slowQueryResult = $article->fetchAll();
	$cache->save($slowQueryResult, $cacheKey);
} else {
	$slowQueryResult = $cache->load($cacheKey);
}
</pre>
<p>You can setup different options here by setting up the cache directory, lifetime, etc.</p>
<p><em>Note that the cache directory must exists and with write permissions and Zend Framework doesn&#8217;t create it for you and will throw error.</em></p>
<p>The problem here is that now you cache only part of the generated content and in many cases this is still too slow for most of the users. However all the work (in most of the cases) of the web server, the script interpreter and the database server result in a simple HTML output. What if you have this output generated or cached for you and when the user hit the page the server will return this pre-generated code?</p>
<p>This is indeed very fast, because it&#8217;s similar to return a text file, as the HTML is simply formatted text.</p>
<h2>Caching the Entire Page</h2>
<p>Before I proceed, I&#8217;d like to say that I work with Zend Framework 1.9.x. Now in the latest versions of ZF there are new mechanisms of caching the output even Zend_Cache_Frontend_Page works fine on them.</p>
<p>You can simply setup the page cache within few simple lines of code:</p>
<pre lang="php">
$fo = array(
    'lifetime' => 600,
    'regexps' => array(
        '^/' => array(
	'cache' => true,
         'cache_with_cookie_variables' => true,
        ),
    )
);

$bo = array(
    'cache_dir' => 'cache/'
);

$cache = Zend_Cache::factory('Page', 'File', $fo, $bo);
$cache->start();
</pre>
<p>However my advise is to place this code as high as possible, because this will cache everything generated as output. It&#8217;s a good practice if you place this even in the bootstrap before you make the connection with the database. Actually you don&#8217;t need a database connection when you&#8217;ve to return a simple text(html) file.</p>
<p>This will improve your app&#8217;s performance a lot!</p>
<p>However there are few things to know. When you setup the cache to work even with cookie variables, you can see that hitting the page with different browsers Zend Framework will generated different cache pages. This is quite useless because than you don&#8217;t have any benefit of caching the content.</p>
<p>First of all let me say that THIS IS NOT A BUG! of ZF. Simply the framework will use the cookie variables to generate the cache key. It&#8217;s obvious that different browsers, even more different users, will have different cookie set and the framework will generate different cache keys for them.</p>
<p>Thus you&#8217;ve to change the setting to generate the cache key from cookie variable by explicitly set this option to false:</p>
<pre lang="php">
$fo = array(
    'lifetime' => 600,
    'regexps' => array(
        '^/' => array(
		 'cache' => true,
         'cache_with_cookie_variables' => true,
         'make_id_with_cookie_variables' => false,
        ),
    )
);

$bo = array(
    'cache_dir' => 'cache/'
);

$cache = Zend_Cache::factory('Page', 'File', $fo, $bo);
$cache->start();
</pre>
<p>Note that in this example we cache every single page generated by the framework explained in the regexps. This is not so good especially when the users have the possibility to login and to see customized content for them, so you can be careful what you cache.</p>
<p>A typical problem that this solution solves is when your application uses Google Analytics. As you may know Google Analytics sets up a cookie every time when an user hits the page, so every time the framework will generate a different cache for him and in result he won&#8217;t see any benefit and performance improvement from your site.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2011/01/17/a-memcached-zend_cache/" rel="bookmark" title="A Memcached Zend_Cache">A Memcached Zend_Cache </a></li>
<li><a href="/2010/01/27/theory-of-caching-zend_cache-zend-optimizer/" rel="bookmark" title="Theory of caching. Zend_Cache &#038; Zend Optimizer.">Theory of caching. Zend_Cache &#038; Zend Optimizer. </a></li>
<li><a href="/2010/07/21/setting-up-global-cache-in-zend-framework/" rel="bookmark" title="Setting Up Global Cache in Zend Framework">Setting Up Global Cache in Zend Framework </a></li>
<li><a href="/2010/07/19/zend-framework-cache-database-table-schemes/" rel="bookmark" title="Zend Framework: Cache Database Table Schemes">Zend Framework: Cache Database Table Schemes </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/11/03/how-to-overcome-zend_cache_frontend_pages-problem-with-cookies/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>HTTP POST with PHP without cURL</title>
		<link>/2010/09/08/http-post-with-php-without-curl/</link>
		<comments>/2010/09/08/http-post-with-php-without-curl/#comments</comments>
		<pubDate>Tue, 07 Sep 2010 21:47:27 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Cross-platform software]]></category>
		<category><![CDATA[CURL]]></category>
		<category><![CDATA[Curly bracket programming languages]]></category>
		<category><![CDATA[Form]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[Procedural programming languages]]></category>
		<category><![CDATA[Scripting languages]]></category>
		<category><![CDATA[Truth]]></category>

		<guid isPermaLink="false">/?p=1960</guid>
		<description><![CDATA[Awesome PHP It&#8217;s strange how powerful PHP can be. There&#8217;s a legend that cURL is the only way to perform a HTTP POST with PHP, but that isn&#8217;t the truth. An extremely useful script is by using stream_context_create: $optional_headers = null; $params = array('http' => array( 'method' => 'POST', 'content' => http_build_query(array('name' => 'my-name')))); if &#8230; <a href="/2010/09/08/http-post-with-php-without-curl/" class="more-link">Continue reading <span class="screen-reader-text">HTTP POST with PHP without cURL</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2011/04/13/post-with-zend_http_client/" rel="bookmark" title="POST with Zend_Http_Client">POST with Zend_Http_Client </a></li>
<li><a href="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
<li><a href="/2010/06/28/send-authenticated-post-request-with-zend_http_client/" rel="bookmark" title="Send Authenticated POST Request with Zend_Http_Client">Send Authenticated POST Request with Zend_Http_Client </a></li>
<li><a href="/2010/05/17/php-the-array-element-doesnt-exist-suppress-the-warnings/" rel="bookmark" title="PHP: The Array Element Doesn&#8217;t Exist &#8211; Suppress the Warnings">PHP: The Array Element Doesn&#8217;t Exist &#8211; Suppress the Warnings </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Awesome PHP</h2>
<p><a href="/wp-content/uploads/2010/09/idea.jpeg"><img src="/wp-content/uploads/2010/09/idea.jpeg" alt="a great php idea!" title="idea" width="430" height="286" class="aligncenter size-full wp-image-1975" srcset="/wp-content/uploads/2010/09/idea.jpeg 430w, /wp-content/uploads/2010/09/idea-300x199.jpg 300w" sizes="(max-width: 430px) 100vw, 430px" /></a><br />
It&#8217;s strange how powerful PHP can be. There&#8217;s a legend that cURL is the only way to perform a HTTP POST with PHP, but that isn&#8217;t the truth. An extremely useful script is by using stream_context_create:</p>
<pre lang="php">
$optional_headers = null;
$params = array('http' => array(
                'method' => 'POST',
                'content' => http_build_query(array('name' => 'my-name'))));

if ($optional_headers !== null) {
    $params['http']['header'] = $optional_headers;
}

$ctx = stream_context_create($params);
$fp = @fopen('http://example.com/post.php', 'rb', false, $ctx);
</pre>
<p>Well this is only a snippet. You can change a lot this code and perform any request, but here&#8217;s a small start up. However note that this will do the same as while posting to example.com/post.php via web form!</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2011/04/13/post-with-zend_http_client/" rel="bookmark" title="POST with Zend_Http_Client">POST with Zend_Http_Client </a></li>
<li><a href="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
<li><a href="/2010/06/28/send-authenticated-post-request-with-zend_http_client/" rel="bookmark" title="Send Authenticated POST Request with Zend_Http_Client">Send Authenticated POST Request with Zend_Http_Client </a></li>
<li><a href="/2010/05/17/php-the-array-element-doesnt-exist-suppress-the-warnings/" rel="bookmark" title="PHP: The Array Element Doesn&#8217;t Exist &#8211; Suppress the Warnings">PHP: The Array Element Doesn&#8217;t Exist &#8211; Suppress the Warnings </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/09/08/http-post-with-php-without-curl/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>The Better Way to Unset Variables in PHP</title>
		<link>/2010/09/03/the-better-way-to-unset-variables-in-php/</link>
		<comments>/2010/09/03/the-better-way-to-unset-variables-in-php/#respond</comments>
		<pubDate>Fri, 03 Sep 2010 09:42:31 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[Human Interest]]></category>
		<category><![CDATA[Parameter]]></category>
		<category><![CDATA[php functions]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[php tricks]]></category>
		<category><![CDATA[Scripting languages]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[Unset]]></category>
		<category><![CDATA[unset in php]]></category>

		<guid isPermaLink="false">/?p=1953</guid>
		<description><![CDATA[Don&#8217;t know why, but most of times I see the PHP unset() multilined with a only one parameter!? unset($var1); unset($var2); unset($var3); But as you can see from the PHP doc page of unset() this method takes optional parameter count. void unset(mixed $var [, mixed $var [, mixed $...]]) So perhaps a better solution can be: &#8230; <a href="/2010/09/03/the-better-way-to-unset-variables-in-php/" class="more-link">Continue reading <span class="screen-reader-text">The Better Way to Unset Variables in PHP</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
<li><a href="/2010/05/19/php-associative-arrays-coding-style/" rel="bookmark" title="PHP Associative Arrays Coding Style">PHP Associative Arrays Coding Style </a></li>
<li><a href="/2011/10/19/thing-to-know-about-php-arrays/" rel="bookmark" title="Thing to Know About PHP Arrays">Thing to Know About PHP Arrays </a></li>
<li><a href="/2011/10/27/object-cloning-and-passing-by-reference-in-php/" rel="bookmark" title="Object Cloning and Passing by Reference in PHP">Object Cloning and Passing by Reference in PHP </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>Don&#8217;t know why, but most of times I see the PHP unset() multilined with a only one parameter!?</p>
<pre lang="php">unset($var1);
unset($var2);
unset($var3);
</pre>
<p>But as you can see from the PHP <a title="doc page of unset()" href="http://php.net/manual/en/function.unset.php" target="_blank">doc page of unset()</a> this method takes optional parameter count.</p>
<pre lang="php">void unset(mixed $var [, mixed $var [, mixed $...]])
</pre>
<p>So perhaps a better solution can be:</p>
<pre lang="php">unset($var1, $var2, $var3);
</pre>
<p>It&#8217;s at least single lined!</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
<li><a href="/2010/05/19/php-associative-arrays-coding-style/" rel="bookmark" title="PHP Associative Arrays Coding Style">PHP Associative Arrays Coding Style </a></li>
<li><a href="/2011/10/19/thing-to-know-about-php-arrays/" rel="bookmark" title="Thing to Know About PHP Arrays">Thing to Know About PHP Arrays </a></li>
<li><a href="/2011/10/27/object-cloning-and-passing-by-reference-in-php/" rel="bookmark" title="Object Cloning and Passing by Reference in PHP">Object Cloning and Passing by Reference in PHP </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/09/03/the-better-way-to-unset-variables-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
