<?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>Julius Beckmann &#187; PHP</title>
	<atom:link href="http://juliusbeckmann.de/blog/category/php/feed" rel="self" type="application/rss+xml" />
	<link>http://juliusbeckmann.de/blog</link>
	<description>Ich bin nicht verrückt, nur technisch begabt ...</description>
	<lastBuildDate>Tue, 07 Sep 2010 18:34:55 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP: Micro vs. Macro optimization, or &quot;Get the low hanging fruit first&quot;</title>
		<link>http://juliusbeckmann.de/blog/php-micro-vs-macro-optimization-or-get-the-low-hanging-fruit-first.html</link>
		<comments>http://juliusbeckmann.de/blog/php-micro-vs-macro-optimization-or-get-the-low-hanging-fruit-first.html#comments</comments>
		<pubDate>Tue, 16 Feb 2010 12:56:46 +0000</pubDate>
		<dc:creator>Julius</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[fruits]]></category>
		<category><![CDATA[macro]]></category>
		<category><![CDATA[micro]]></category>
		<category><![CDATA[optimieren]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=549</guid>
		<description><![CDATA[After using and developing PHP for a few years now, i heard pretty much stuff about optimization, special technice's and other myths and fairy tales. But this article is not about tips or tricks, it is about if this the real key to better performance.

Nearly all of us have heard about hints like "static method [...]]]></description>
			<content:encoded><![CDATA[<p>After using and developing PHP for a few years now, i heard pretty much stuff about optimization, special technice's and other myths and fairy tales. But this article is not about tips or tricks, it is about if this the real key to better performance.<br />
<span id="more-549"></span><br />
Nearly all of us have heard about hints like "static method calls are faster" or "++$i is faster than $i++". First is wrong, second is true by the way, but more important is the question, does it really perform better in overall?</p>
<p>I would like to categorize the optimization possibility's into micro-optimization and macro-optimization.</p>
<h2>Micro-optimization:</h2>
<p>I already mentioned the ++$i tip. This would be a perfect example for a micro-optimization. Imagine you have a website, just a normal one with a few thousand hits a day. Simple fetching articles from database and formatting/outputting these. Of course you could use ++$i instead of $i++, but what will be the achievement? Lets guess there are about 1000 PHP operations done on a normal page request. About 50 of them could be optimized with ++$i which is guessed 10% faster. </p>
<div class="codesnip-container" >950 + 50 * (1-0.10) = 950 + 45 = 995 calls left.</div>
<p>You saved 5 simple operations, what are about 0,5%. </p>
<p>This might look noticeable at first, but this is only the number of operations, not the real spend time where these 5 calls will not be measurable any more. Every request the PHP parser has to read and parse the PHP file. On every SQL SELECT there is the database overhead which also depends on database server load. Our previous result combined with that knowledge might be ~0.1% at the end. Conclusion: This kind of optimization simply does not make the page significantly  faster.</p>
<h2>Macro-optimization:</h2>
<p>First, this is not about using macros like in C. No simple code substitution. It is the opposite of the micro-optimization. Once again a example with the simple imaginary PHP website. The main menu using JavaScript is generated by a complex SQL query on every request. This whole procedure takes about 10ms (unrealistic i know, but it is _just_ for the example calculation). 9ms for the SQL query and 1 ms PHP. For all of you who do not know why SQL is slower: Because there are sockets / TCP, database-daemon and harddisk access involved.</p>
<p>THIS is the point where we could use a macro-optimization.<br />
Either, we could invest much work and create a different menu system without javascript and faster SQL queries, or we could do it a much easier. </p>
<p>If we have APC for caching (other anything else), we could use the following strategy: </p>
<div class="codesnip-container" >1. Check for a cached result of our SQL query.<br />
2. If yes, skip to 5.<br />
3. We have no cached entry and perform the SQL query now.<br />
4. The result of the query will be saved in our cache for 1 hours.<br />
5. Use the result to generate the menu.</div>
<p>After this optimization that _just_ affect the SQL fetching part, we have a APC cache request that might take ~1 ms. So we reduced our 9ms SQL to 1ms APC. Of course the overall benefit will not be that big, but will be notice- and measurable. But what really makes the point here is, that we need to query only 24 times a day and not on _every_ request.</p>
<h2>Low hanging fruit</h2>
<p><img alt="" src="/blog/files/images/lowapples.png" class="alignright" width="224" height="261" /><br />
After reading about micro and macro optimization, you might understand the meaning of this much better.<br />
There are several ways to optimize, some of them are easy and fast. These are the "low hanging fruit" - easy to pick. This fruits should be always the first thought when it comes to optimizing. Some of them can be found easily with profiling.<br />
I want to give you a few hints for optimizing your PHP (Python, Ruby, ...) application.:</p>
<p>1. Keep an eye on the IO load. The harddisk of the server is mostly the slowest part and should be used only if necessary.</p>
<p>2. Try to avoid complex SQL querys. The database servers are often under heavy load, even if they have some integrated caching, there is still the SQL overhead.</p>
<p>3. Try to store results. If you need to analyse the browser of your clients, do not do this every request, save the needed values in their $_SESSION and check only if the UserAgent has been changed with a simple crc32 hash.</p>
<p>4. Try to cache. If you have parts in your code that are called nearly every request, have the same result and do not change often. Cache them! Some options are: APC, XCache, Memcache, eAccelerator, tmpFS.</p>
<p>5. Try to avoid unnecessary function calls. Try to store the result of that function and use it multiple times.</p>
<h2>High hanging fruit</h2>
<p><img alt="" src="/blog/files/images/tallapples.png" class="alignright" width="224" height="261" /><br />
I do not say you should not use ++$i or static methods. I want to clarify that there are more important parts worth optimizing.<br />
If you write new code, it should always be your aim writing the fastest code possible. Some of these tips floating on the internet might be helpful, but please check if they are true first!<br />
Some tips i tested myself:<br />
1. ++$i is faster than $i++<br />
2. Static methods are not always faster. (<a href="http://www.ingo-schramm.de/blog/archives/4-PHP-Myth-static-function-call-faster-than-instance-function-call.html">source</a>)<br />
3. Relative paths are slower to include than absolute.<br />
4. foreach is fastest for iterating over arrays. (<a href="/blog/php-foreach-vs-while-vs-for-the-loop-battle.html">benchmark</a>)<br />
5. floor and ceil can be replaced bei (int) casts. (<a href="/blog/php-floor-and-ceil-are-slow.html">benchmark</a>)<br />
6. Try to avoid preg_* function if possible. (<a href="/blog/php-do-not-rebuild-preg_-functions.html">benchmark</a>)</p>
<h2>Conclusion</h2>
<p>If you are hungry, profile the tree first, then pick some of the large low hanging fruits.<br />
While coding pick some of the tiny high hanging.<br />
I hope you had fun reading about my point of view.</p>
]]></content:encoded>
			<wfw:commentRss>http://juliusbeckmann.de/blog/php-micro-vs-macro-optimization-or-get-the-low-hanging-fruit-first.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP: Easy and secure password hashing class</title>
		<link>http://juliusbeckmann.de/blog/php-easy-and-secure-password-hashing-class.html</link>
		<comments>http://juliusbeckmann.de/blog/php-easy-and-secure-password-hashing-class.html#comments</comments>
		<pubDate>Fri, 29 Jan 2010 13:26:06 +0000</pubDate>
		<dc:creator>Julius</dc:creator>
				<category><![CDATA[Classes]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Class]]></category>
		<category><![CDATA[Hash]]></category>
		<category><![CDATA[MD5]]></category>
		<category><![CDATA[Password]]></category>
		<category><![CDATA[SHA1]]></category>

		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=526</guid>
		<description><![CDATA[Everybody talks about security but most of the people still save md5(password) in their databases. This is not funny. Reversing a simple and even a average password is not that hard.
I once wrote this tiny class that generates secure enough password hashes.

I build in salt and variable interations.
License is GPL so everybody can use it [...]]]></description>
			<content:encoded><![CDATA[<p>Everybody talks about security but most of the people still save md5(password) in their databases. This is not funny. Reversing a simple and even a average password is not that hard.<br />
I once wrote this tiny class that generates secure enough password hashes.<br />
<span id="more-526"></span><br />
I build in salt and variable interations.<br />
License is GPL so everybody can use it :D</p>
<h2>Download / Source</h2>
<p><a href="http://juliusbeckmann.de/code/class.password.phps">http://juliusbeckmann.de/code/class.password.phps</a><br />
<a href="http://juliusbeckmann.de/code/class.password.php.txt">http://juliusbeckmann.de/code/class.password.php.txt</a></p>
<h2>Usage / Example</h2>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="re0">$pass</span> <span class="sy0">=</span> <span class="st_h">'MyPassword'</span><span class="sy0">;</span><br />
<span class="kw1">echo</span> <span class="re0">$pass</span><span class="sy0">,</span> <span class="st_h">' =&gt; '</span><span class="sy0">,</span> password<span class="sy0">::</span><a href="http://www.php.net/hash"><span class="kw3">hash</span></a><span class="br0">&#40;</span><span class="re0">$pass</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</div>
<h2>Background</h2>
<p><strong>What is a "salt" good for?</strong><br />
A salt adds a pretty random string so passwords to make the hash more secure.<br />
The saltless password <strong>grandma becomes a5d19cdd5fd1a8f664c0ee2b5e293167</strong>.<br />
If you use the salt="!24sf+fs5SDG65-54" then <strong>grandma!24sf+fs5SDG65-54 becomes bab9015f430ad28f420581f069f5736f</strong><br />
And nobody would know that bab9015f430ad28f420581f069f5736f was "grandma"</p>
<p>Check it yourself:<br />
<a href="http://www.google.com/search?q=a5d19cdd5fd1a8f664c0ee2b5e293167">Google a5d19cdd5fd1a8f664c0ee2b5e293167 = "grandma"</a><br />
<a href="http://www.google.com/search?q=bab9015f430ad28f420581f069f5736f">Google bab9015f430ad28f420581f069f5736f = "grandma!24sf+fs5SDG65-54"</a><br />
You can clearly see yourself the salted password hash is not known by Google.</p>
<p><strong>What are iterations good for?</strong><br />
Iterations mean you make a hash from a hash.<br />
Again the "grandma" example:<br />
md5(grandma) = a5d19cdd5fd1a8f664c0ee2b5e293167<br />
md5(a5d19cdd5fd1a8f664c0ee2b5e293167) = ce807f095fa160ccce736e007fe74ff1<br />
md5(ce807f095fa160ccce736e007fe74ff1) = e720fe3e6cc002a0eaabf5300283bd56<br />
md5(e720fe3e6cc002a0eaabf5300283bd56) = ...<br />
But be carefull, plain rehashing is not more secure than single hashing.<br />
What makes rehashing more secure is using the salt again what makes the new hash dependent from the previous hash AND the salt.</p>
]]></content:encoded>
			<wfw:commentRss>http://juliusbeckmann.de/blog/php-easy-and-secure-password-hashing-class.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>PHP: FIFO queue with APC</title>
		<link>http://juliusbeckmann.de/blog/php-fifo-queue-with-apc.html</link>
		<comments>http://juliusbeckmann.de/blog/php-fifo-queue-with-apc.html#comments</comments>
		<pubDate>Fri, 29 Jan 2010 08:04:42 +0000</pubDate>
		<dc:creator>Julius</dc:creator>
				<category><![CDATA[APC]]></category>
		<category><![CDATA[Classes]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Class]]></category>
		<category><![CDATA[FIFO]]></category>
		<category><![CDATA[Memcache]]></category>
		<category><![CDATA[Queue]]></category>

		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=520</guid>
		<description><![CDATA[A few days ago i needed a simple queue class that would also be persistent after the pagerequest. I first thought about putting this queue in SQL but did not want the whole SQL overhead and such and remembered a blog artice about memcache queues a once read. But i had no memcache for that [...]]]></description>
			<content:encoded><![CDATA[<p>A few days ago i needed a simple queue class that would also be persistent after the pagerequest. I first thought about putting this queue in SQL but did not want the whole SQL overhead and such and remembered a <a href="http://3.rdrail.net/blog/memcached-based-message-queues/">blog artice about memcache queues</a> a once read. But i had no memcache for that project and used <a href="http://pecl.php.net/package/APC">APC</a> instead.<br />
<span id="more-520"></span></p>
<h2>Basic idea</h2>
<p>The basic idea looks like this:<br />
<a href="http://juliusbeckmann.de/blog/static/memqueue.jpg"><img src="http://juliusbeckmann.de/blog/static/memqueue-600x397.jpg" alt="memqueue" title="memqueue" width="600" height="397" class="aligncenter size-medium wp-image-521" /></a><br />
There are 2 counters, one for the current head key, and the other for the current tail key.<br />
The main part of the code is from the mentioned blog artice, but i ported it to PHP and APC instead of Memcache.</p>
<h2>Documentation</h2>
<p>A generated documentation can be found here:<br />
<a href="http://juliusbeckmann.de/classes/li_apc_queue.html">http://juliusbeckmann.de/classes/li_apc_queue.html</a></p>
<h2>Source</h2>
<p>The source can be found here:<br />
<a href="http://juliusbeckmann.de/classes/src/apc_queue/class.apc_queue.phps">http://juliusbeckmann.de/classes/src/apc_queue/class.apc_queue.phps</a><br />
<a href="http://juliusbeckmann.de/classes/src/apc_queue/class.apc_queue.php.txt">http://juliusbeckmann.de/classes/src/apc_queue/class.apc_queue.php.txt</a></p>
<h2>Usage</h3>
<p>Here is a example how to use the queue:</p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="re0">$q</span> <span class="sy0">=</span> <span class="kw2">new</span> apc_queue<span class="br0">&#40;</span><span class="st_h">'test'</span><span class="sy0">,</span> <a href="http://www.php.net/isset"><span class="kw3">isset</span></a><span class="br0">&#40;</span><span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st_h">'force'</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></p>
<p><span class="kw1">echo</span> <span class="st0">&quot;STORE: &quot;</span><span class="sy0">;</span><br />
<span class="kw1">for</span><span class="br0">&#40;</span><span class="re0">$i</span><span class="sy0">=</span><span class="nu0">0</span><span class="sy0">;</span> <span class="re0">$i</span><span class="sy0">&lt;</span><span class="nu0">10</span><span class="sy0">;</span> <span class="sy0">++</span><span class="re0">$i</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="re0">$rand</span> <span class="sy0">=</span> <a href="http://www.php.net/rand"><span class="kw3">rand</span></a><span class="br0">&#40;</span>0<span class="sy0">,</span>9<span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; <span class="kw1">echo</span> <span class="re0">$rand</span><span class="sy0">,</span><span class="st_h">','</span><span class="sy0">;</span><br />
&nbsp; &nbsp; <span class="re0">$q</span><span class="sy0">-&gt;</span><span class="me1">add</span><span class="br0">&#40;</span><span class="re0">$rand</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="br0">&#125;</span><br />
<span class="kw1">echo</span> <span class="st0">&quot;<span class="es1">\n</span>FETCH: &quot;</span><span class="sy0">;</span><br />
<span class="kw1">while</span><span class="br0">&#40;</span><span class="br0">&#40;</span><span class="re0">$g</span> <span class="sy0">=</span> <span class="re0">$q</span><span class="sy0">-&gt;</span><span class="me1">get</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="sy0">!==</span> <span class="kw4">FALSE</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">echo</span> <span class="re0">$g</span><span class="sy0">,</span><span class="st_h">','</span><span class="sy0">;</span><br />
<span class="br0">&#125;</span><br />
<span class="kw1">echo</span> <span class="st0">&quot;<span class="es1">\n</span>LENGTH: &quot;</span><span class="sy0">,</span> <span class="re0">$q</span><span class="sy0">-&gt;</span><span class="me1">length</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://juliusbeckmann.de/blog/php-fifo-queue-with-apc.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP/MySQL: Warning: mysql_query(): Unable to save result set ...</title>
		<link>http://juliusbeckmann.de/blog/php-mysql-warning-mysql_query-unable-to-save-result-set.html</link>
		<comments>http://juliusbeckmann.de/blog/php-mysql-warning-mysql_query-unable-to-save-result-set.html#comments</comments>
		<pubDate>Mon, 18 Jan 2010 15:08:36 +0000</pubDate>
		<dc:creator>Julius</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Error]]></category>
		<category><![CDATA[Query]]></category>
		<category><![CDATA[Solution]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=507</guid>
		<description><![CDATA[Today i found a error in a tiny script i was working on.
The error Message looked like this:
Warning: mysql_query() [function.mysql-query]: Unable to save result set in /var/www/example.php on line 55
First i found a few hints about repairing or optimizing MySQL tables, but this did not work out.
After a few debugging outputs a realized that the [...]]]></description>
			<content:encoded><![CDATA[<p>Today i found a error in a tiny script i was working on.<br />
The error Message looked like this:</p>
<div class="codesnip-container" >Warning: mysql_query() [function.mysql-query]: Unable to save result set in /var/www/example.php on line 55</div>
<p>First i found a few hints about repairing or optimizing MySQL tables, but this did not work out.<br />
After a few debugging outputs a realized that the query was not properly written.<br />
<span id="more-507"></span><br />
The original query looked like this:</p>
<div class="codesnip-container" >
<div class="sql codesnip" style="font-family:monospace;"><span class="kw1">SELECT</span> id<br />
<span class="kw1">FROM</span> <span class="st0">`table`</span> <br />
<span class="kw1">WHERE</span> <span class="st0">`order`</span> <span class="sy0">&gt;</span><br />
<span class="br0">&#40;</span><br />
&nbsp; <span class="kw1">SELECT</span> <span class="st0">`order`</span> <br />
&nbsp; <span class="kw1">FROM</span> <span class="st0">`table`</span> <br />
&nbsp; <span class="kw1">WHERE</span> active <span class="sy0">=</span> 1<br />
<span class="br0">&#41;</span><br />
<span class="kw1">LIMIT</span> <span class="nu0">1</span>;</div>
</div>
<p>As you can see the query as a non correlated subquery. It worked fine nearly all the time, but only when the subquery returns only zero or one lines. If there are more lines MySQL generates the "Unable to save result set", because `order` cant be bigger then 2 resultvalues at the same time.<br />
The right query looks like this:</p>
<div class="codesnip-container" >
<div class="sql codesnip" style="font-family:monospace;"><span class="kw1">SELECT</span> id<br />
<span class="kw1">FROM</span> <span class="st0">`table`</span> <br />
<span class="kw1">WHERE</span> <span class="st0">`order`</span> <span class="sy0">&gt;</span><br />
<span class="br0">&#40;</span><br />
&nbsp; <span class="kw1">SELECT</span> <span class="st0">`order`</span> <br />
&nbsp; <span class="kw1">FROM</span> <span class="st0">`table`</span> <br />
&nbsp; <span class="kw1">WHERE</span> active <span class="sy0">=</span> 1<br />
&nbsp; <span class="kw1">LIMIT</span> 1<br />
<span class="br0">&#41;</span><br />
<span class="kw1">LIMIT</span> <span class="nu0">1</span>;</div>
</div>
<p>A extra LIMIT 1 makes the deal perfect, and saved a little bit of my day.</p>
]]></content:encoded>
			<wfw:commentRss>http://juliusbeckmann.de/blog/php-mysql-warning-mysql_query-unable-to-save-result-set.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: phpMyAdmin3 - deactivate slider effects</title>
		<link>http://juliusbeckmann.de/blog/php-phpmyadmin-deactivate-slider-effects.html</link>
		<comments>http://juliusbeckmann.de/blog/php-phpmyadmin-deactivate-slider-effects.html#comments</comments>
		<pubDate>Wed, 06 Jan 2010 16:41:21 +0000</pubDate>
		<dc:creator>Julius</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=498</guid>
		<description><![CDATA[For all of you who liked phpMyAdmin2 where all relevant information was visible at any time, sould be annoyed be the new slider effects in phpMyAdmin3.
I did a short search for the config value to change this and found this solution.
Add this to your config.inc.php:

// Change slider behaviour
$cfg&#91;'InitialSlidersState'&#93; = 'open';

And your sliders will start in [...]]]></description>
			<content:encoded><![CDATA[<p>For all of you who liked phpMyAdmin2 where all relevant information was visible at any time, sould be annoyed be the new slider effects in phpMyAdmin3.<br />
I did a short search for the config value to change this and found this solution.</p>
<p>Add this to your config.inc.php:</p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="co1">// Change slider behaviour</span><br />
<span class="re0">$cfg</span><span class="br0">&#91;</span><span class="st_h">'InitialSlidersState'</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="st_h">'open'</span><span class="sy0">;</span></div>
</div>
<p>And your sliders will start in visible state.</p>
<p>phpMyAdmin download:<br />
<a href="http://www.phpmyadmin.net/home_page/downloads.php">http://www.phpmyadmin.net/home_page/downloads.php</a></p>
]]></content:encoded>
			<wfw:commentRss>http://juliusbeckmann.de/blog/php-phpmyadmin-deactivate-slider-effects.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: floor() and ceil() are slow!</title>
		<link>http://juliusbeckmann.de/blog/php-floor-and-ceil-are-slow.html</link>
		<comments>http://juliusbeckmann.de/blog/php-floor-and-ceil-are-slow.html#comments</comments>
		<pubDate>Sun, 03 Jan 2010 18:13:42 +0000</pubDate>
		<dc:creator>Julius</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=494</guid>
		<description><![CDATA[A workmate mentioned, that floor($i) could be replaced with (int)$i.
I found this very interresting and did some researched about that.
floor($i); will return a float that will have the lower value of $i.
A cast will just convert $i to an integer which will cut of all decimals.
So by symptoms, these two methods are equivalent. Even if [...]]]></description>
			<content:encoded><![CDATA[<p>A workmate mentioned, that floor($i) could be replaced with (int)$i.<br />
I found this very interresting and did some researched about that.</p>
<p><a href="http://php.net/floor">floor($i);</a> will return a float that will have the lower value of $i.<br />
A <a href="http://php.net/manual/de/language.types.type-juggling.php">cast</a> will just convert $i to an integer which will cut of all decimals.<br />
So by symptoms, these two methods are equivalent. Even if floor returns a float, PHP does dynamic casting of its variables.</p>
<p><span id="more-494"></span></p>
<h3>Benchmark</h3>
<p>Next question for me was, which one is faster?<br />
The benchmark script can be found here:<br />
<a href="http://juliusbeckmann.de/static/scripts/bench_floor_and_ceil_vs_cast.phps">bench_floor_and_ceil_vs_cast.phps</a> - with highlighting<br />
<a href="http://juliusbeckmann.de/static/scripts/bench_floor_and_ceil_vs_cast.php.txt">bench_floor_and_ceil_vs_cast.php.txt</a> - without highlighting</p>
<h3>Results</h3>
<div class="codesnip-container" >E:>php bench_floor_and_ceil_vs_cast.php<br />
Test: floor() vs. (int)-cast function:</p>
<p>0.628 seconds - floor()<br />
0.239 seconds - (int)-cast</p>
<p>Test: ceil() vs. +1 &#038;&#038; (int)-cast function:</p>
<p>0.624 seconds - ceil()<br />
0.284 seconds - +1 &#038;&#038; (int)-cast</p></div>
<p>The floor() and the ceil() function are about 60% (!) slower than the cast method.</p>
<h3>Why?</h3>
<p>Why is that so?<br />
I think because of the function call, more resources are needed instead of the dynamic cast which should be done by PHP very fast.</p>
<h3>Problems</h3>
<p>One problem i could find was: What happens with large numbers?<br />
Something bad will happen if you (int) cast a number greater equal than 2147483648. In this case it will revert to -2147483648 because PHP handles this as some kind of overflow.<br />
Normaly, PHP will interpret such a number as a float, but because of the cast this cant be done.<br />
This is also the cause why floor() returns a float instead of a int.<br />
Some more information on this topic can be found here:<br />
<a href="http://php.net/manual/en/language.types.integer.php">http://php.net/manual/en/language.types.integer.php</a></p>
]]></content:encoded>
			<wfw:commentRss>http://juliusbeckmann.de/blog/php-floor-and-ceil-are-slow.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP: Do NOT rebuild preg_* functions!</title>
		<link>http://juliusbeckmann.de/blog/php-do-not-rebuild-preg_-functions.html</link>
		<comments>http://juliusbeckmann.de/blog/php-do-not-rebuild-preg_-functions.html#comments</comments>
		<pubDate>Wed, 23 Dec 2009 08:14:07 +0000</pubDate>
		<dc:creator>Julius</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=487</guid>
		<description><![CDATA[In a PHP Project i am currently helping out, i found this function:

/**
&#160;* Replaces every char in $str that does is not in $valid array
&#160;* 
&#160;* @param array $valid
&#160;* @param string $str
&#160;* @return string
&#160;*/
function cleanString&#40;$valid, $str&#41; &#123;
&#160; $ret = '';
&#160; $len = strlen&#40;$str&#41;;
&#160; for&#40;$i = 0; $i &#60; $len; ++$i&#41; &#123;
&#160; &#160; if&#40;in_array&#40;$str&#91;$i&#93;, $valid, true&#41;&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>In a PHP Project i am currently helping out, i found this function:</p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="co4">/**<br />
&nbsp;* Replaces every char in $str that does is not in $valid array<br />
&nbsp;* <br />
&nbsp;* @param array $valid<br />
&nbsp;* @param string $str<br />
&nbsp;* @return string<br />
&nbsp;*/</span><br />
<span class="kw2">function</span> cleanString<span class="br0">&#40;</span><span class="re0">$valid</span><span class="sy0">,</span> <span class="re0">$str</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; <span class="re0">$ret</span> <span class="sy0">=</span> <span class="st_h">''</span><span class="sy0">;</span><br />
&nbsp; <span class="re0">$len</span> <span class="sy0">=</span> <a href="http://www.php.net/strlen"><span class="kw3">strlen</span></a><span class="br0">&#40;</span><span class="re0">$str</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; <span class="kw1">for</span><span class="br0">&#40;</span><span class="re0">$i</span> <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span> <span class="re0">$i</span> <span class="sy0">&lt;</span> <span class="re0">$len</span><span class="sy0">;</span> <span class="sy0">++</span><span class="re0">$i</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><a href="http://www.php.net/in_array"><span class="kw3">in_array</span></a><span class="br0">&#40;</span><span class="re0">$str</span><span class="br0">&#91;</span><span class="re0">$i</span><span class="br0">&#93;</span><span class="sy0">,</span> <span class="re0">$valid</span><span class="sy0">,</span> <span class="kw4">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span class="re0">$ret</span> <span class="sy0">.=</span> <span class="re0">$str</span><span class="br0">&#91;</span><span class="re0">$i</span><span class="br0">&#93;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; <span class="kw1">return</span> <span class="re0">$ret</span><span class="sy0">;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>It is a simple function for replacing chars. It works quite well, but was mainly only used for checking [^a-z0-9]. So i tested the speed of this code.<br />
<span id="more-487"></span></p>
<h3>Benchmark script</h3>
<p>Wrote this short bench script:<br />
<a href="http://juliusbeckmann.de/static/scripts/bench_strcheck.phps">http://juliusbeckmann.de/static/scripts/bench_strcheck.phps</a><br />
<a href="http://juliusbeckmann.de/static/scripts/bench_strcheck.php.txt">http://juliusbeckmann.de/static/scripts/bench_strcheck.php.txt</a></p>
<h3>Result</h3>
<p>The result of it is impressive.</p>
<div class="codesnip-container" >php bench_strcheck.php<br />
Test: Rebuilding preg_replace function:</p>
<p>gstzfg87i86ef6798zwefogflwef<br />
should be the same like:<br />
gstzfg87i86ef6798zwefogflwef</p>
<p>1.039 seconds - preg_replace()<br />
1.117 seconds - preg_cleanString()<br />
12.160 seconds - cleanString()</p></div>
<p>The pure preg_replace was the fastes, as expected.<br />
The capsuled preg function inside preg_cleanString was 7,5% slower than direct. 7.5% just for calling a non void function with 1 parameter is too much in my opinion.<br />
The cleanString() functions was, as expected, the slowest of the three. But 1070% more time is way too much.</p>
<h3>Conclusion</h3>
<p>My conclusion is:<br />
Do NOT rewrite optimized functions like preg_replace and others. The will not be faster. Only in case preg_* functions are not available, a replacement functions is acceptable.</p>
]]></content:encoded>
			<wfw:commentRss>http://juliusbeckmann.de/blog/php-do-not-rebuild-preg_-functions.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP: Easy Memcache handling</title>
		<link>http://juliusbeckmann.de/blog/php-easy-memcache-handling.html</link>
		<comments>http://juliusbeckmann.de/blog/php-easy-memcache-handling.html#comments</comments>
		<pubDate>Tue, 03 Nov 2009 07:58:10 +0000</pubDate>
		<dc:creator>Julius</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Library]]></category>
		<category><![CDATA[Memcache]]></category>
		<category><![CDATA[PHPClasses]]></category>

		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=475</guid>
		<description><![CDATA[In a current project of mine i needed the use of a universal caching system.
After testing a little bit with APC i found memcache that will do the job perfectly.
So i wrote a few tiny functions for easy memcache handling.

The code can be found here:
With highlighting:
http://juliusbeckmann.de/code/memcache.lib.phps
Plain download:
http://juliusbeckmann.de/code/memcache.lib.txt
I wrote the code with the idea in mind [...]]]></description>
			<content:encoded><![CDATA[<p>In a current project of mine i needed the use of a universal caching system.<br />
After testing a little bit with <a href="http://pecl.php.net/apc">APC</a> i found <a href="http://www.danga.com/memcached/">memcache</a> that will do the job perfectly.<br />
So i wrote a few tiny functions for easy memcache handling.<br />
<span id="more-475"></span><br />
The code can be found here:<br />
<strong>With highlighting:</strong><br />
<a href="http://juliusbeckmann.de/code/memcache.lib.phps">http://juliusbeckmann.de/code/memcache.lib.phps</a><br />
<strong>Plain download:</strong><br />
<a href="http://juliusbeckmann.de/code/memcache.lib.txt">http://juliusbeckmann.de/code/memcache.lib.txt</a></p>
<p>I wrote the code with the idea in mind that i could simply use this 3 cache_* functions without having to handle a memcache object. Till now this code works flawlessly on my project.<br />
<br/><br />
I added PHPDoc Comments but want to give some advices here:<br />
<br/><br />
You can use 2 constants for the functions if you want to run multiple systems with one memcache cache:</p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;">MEMCACHE_PREFIX</div>
</div>
<p>This will add a prefix to every $key. This is done inside the functions, no need to interact from outside.</p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;">MEMCACHE_SUFFIX</div>
</div>
<p>This will add a suffix to every $key.<br />
If you do not use this constants, you can safely remove the 4 lines in every cache_* function.<br />
<br/></p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="kw2">function</span> <span class="sy0">&amp;</span> memcache_instance<span class="br0">&#40;</span><span class="re0">$ip</span><span class="sy0">=</span><span class="st_h">'127.0.0.1'</span><span class="sy0">,</span> <span class="re0">$port</span><span class="sy0">=</span><span class="nu0">11211</span><span class="br0">&#41;</span></div>
</div>
<p>This functions returns a <strong>reference</strong> to the current memcache connection.<br />
If you want to check if memcache connection is available:</p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="kw1">if</span><span class="br0">&#40;</span><span class="sy0">!</span>memcache_instance<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw1">echo</span> <span class="st0">&quot;Memcache not available!&quot;</span><span class="sy0">;</span></div>
</div>
<p><br/></p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="kw2">function</span> cache_set<span class="br0">&#40;</span><span class="re0">$key</span><span class="sy0">,</span> <span class="re0">$val</span><span class="sy0">,</span> <span class="re0">$ttl</span><span class="sy0">=</span>0<span class="sy0">,</span> <span class="re0">$compress</span><span class="sy0">=</span><span class="kw4">false</span><span class="br0">&#41;</span></div>
</div>
<p>This is the set function. The name is selected universally so you can exchange the code easy. It will return FALSE if $key could not be set.<br />
$ttl = 0 means caching forever, or it gets purged.<br />
$compress = true would let memcache compress the $val with zlib. This only makes sense on huge arrays or objects.<br />
<br/></p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="kw2">function</span> cache_get<span class="br0">&#40;</span><span class="re0">$key</span><span class="br0">&#41;</span></div>
</div>
<p>Simple get function returns FALSE or the $val of $key.<br />
<br/></p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="kw2">function</span> cache_del<span class="br0">&#40;</span><span class="re0">$key</span><span class="sy0">,</span> <span class="re0">$timeout</span><span class="sy0">=</span><span class="nu0">0</span><span class="br0">&#41;</span></div>
</div>
<p>This is the delete function. It will return TRUE if the delete command was successful.<br />
$timout = 0 means deleting immediately. A value of 60 would delete the $key in 60 seconds. </p>
]]></content:encoded>
			<wfw:commentRss>http://juliusbeckmann.de/blog/php-easy-memcache-handling.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rewrite: PHP String Flipper Class</title>
		<link>http://juliusbeckmann.de/blog/rewrite-php-string-flipper-class.html</link>
		<comments>http://juliusbeckmann.de/blog/rewrite-php-string-flipper-class.html#comments</comments>
		<pubDate>Sun, 04 Oct 2009 09:59:49 +0000</pubDate>
		<dc:creator>Julius</dc:creator>
				<category><![CDATA[Classes]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=469</guid>
		<description><![CDATA[I found this class on Twitter. Nice idea flipping chars by replacing them with their unicode upside down alternative.
But what bugged me was the code, it looked awful. So id did a rewrite to clean it up a little bit.

The class can be viewed here:
http://juliusbeckmann.de/static/scripts/flipper.phps
and downloaded here:
http://juliusbeckmann.de/static/scripts/flipper.php.txt
From the 150 lines of original code, i reduced [...]]]></description>
			<content:encoded><![CDATA[<p>I found <a href="http://masnun.com/2009/09/28/php-text-flipper-library/">this class</a> on Twitter. Nice idea flipping chars by replacing them with their unicode upside down alternative.<br />
But what bugged me was the code, it looked awful. So id did a rewrite to clean it up a little bit.<br />
<span id="more-469"></span></p>
<p>The class can be viewed here:<br />
<a href="http://juliusbeckmann.de/static/scripts/flipper.phps">http://juliusbeckmann.de/static/scripts/flipper.phps</a><br />
and downloaded here:<br />
<a href="http://juliusbeckmann.de/static/scripts/flipper.php.txt">http://juliusbeckmann.de/static/scripts/flipper.php.txt</a></p>
<p>From the 150 lines of original code, i reduced it to only ~80 lines. I also added a few comments and PHPDoc.</p>
]]></content:encoded>
			<wfw:commentRss>http://juliusbeckmann.de/blog/rewrite-php-string-flipper-class.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HowTo: Use the ternary operator in PHP</title>
		<link>http://juliusbeckmann.de/blog/howto-use-the-ternary-operator-in-php.html</link>
		<comments>http://juliusbeckmann.de/blog/howto-use-the-ternary-operator-in-php.html#comments</comments>
		<pubDate>Sat, 03 Oct 2009 11:31:11 +0000</pubDate>
		<dc:creator>Julius</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Operator]]></category>
		<category><![CDATA[Ternary]]></category>

		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=466</guid>
		<description><![CDATA[A good program is sometimes equalized with few lines of code. But how do we program this way? One step is using the ternary operator...

What is it?
Ternary means 3. But nearly every operator you know is binary.
Some binary operator examples:

$a = $b + $c;
$a = $b - $c;
$a = $b % $c;
$a = $b * [...]]]></description>
			<content:encoded><![CDATA[<p>A good program is sometimes equalized with few lines of code. But how do we program this way? One step is using the ternary operator...<br />
<span id="more-466"></span></p>
<h3>What is it?</h3>
<p>Ternary means 3. But nearly every operator you know is binary.</p>
<p>Some binary operator examples:</p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="re0">$a</span> <span class="sy0">=</span> <span class="re0">$b</span> <span class="sy0">+</span> <span class="re0">$c</span><span class="sy0">;</span><br />
<span class="re0">$a</span> <span class="sy0">=</span> <span class="re0">$b</span> <span class="sy0">-</span> <span class="re0">$c</span><span class="sy0">;</span><br />
<span class="re0">$a</span> <span class="sy0">=</span> <span class="re0">$b</span> <span class="sy0">%</span> <span class="re0">$c</span><span class="sy0">;</span><br />
<span class="re0">$a</span> <span class="sy0">=</span> <span class="re0">$b</span> <span class="sy0">*</span> <span class="re0">$c</span><span class="sy0">;</span></div>
</div>
<p>As you can see, +,-,% and * are binary operators because they need 2 variables for their action. But the ternary operator has one variable more!</p>
<h3>How to use it?</h3>
<p>The ternary operator has 3 "variables" and looks like this:</p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="re0">$result</span> <span class="sy0">=</span> <span class="re0">$when</span> ? <span class="re0">$then</span> <span class="sy0">:</span> <span class="re0">$else</span><span class="sy0">;</span></div>
</div>
<p>We can also write this in a normal if then else code:</p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$when</span><span class="br0">&#41;</span><br />
&nbsp; <span class="re0">$result</span> <span class="sy0">=</span> <span class="re0">$then</span><span class="sy0">;</span><br />
<span class="kw1">else</span><br />
&nbsp; <span class="re0">$result</span> <span class="sy0">=</span> <span class="re0">$else</span><span class="sy0">;</span></div>
</div>
<h3>Example</h3>
<p>Here a real life example i used in my code.<br />
If i want to initialize a variable correct, this code is fast and tiny:</p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="re0">$iterations_max</span> <span class="sy0">=</span> <span class="br0">&#40;</span><a href="http://www.php.net/defined"><span class="kw3">defined</span></a><span class="br0">&#40;</span><span class="st_h">'ITERATIONS_MAX'</span><span class="br0">&#41;</span><span class="br0">&#41;</span> ? ITERATIONS_MAX <span class="sy0">:</span> 10 <span class="sy0">;</span></div>
</div>
<p>The if else version looks like this:</p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="kw1">if</span> <span class="br0">&#40;</span><span class="br0">&#40;</span><a href="http://www.php.net/defined"><span class="kw3">defined</span></a><span class="br0">&#40;</span><span class="st_h">'ITERATIONS_MAX'</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; <span class="re0">$iterations_max</span> <span class="sy0">=</span> ITERATIONS_MAX<span class="sy0">;</span><br />
<span class="kw1">else</span><br />
&nbsp; <span class="re0">$iterations_max</span> <span class="sy0">=</span> <span class="nu0">10</span><span class="sy0">;</span></div>
</div>
<h3>Documentation</h3>
<p>Hope you understood the usage of the ternary operator and might use it in your future code.</p>
<p>The documentation for this operator can be found here:<br />
<a href="http://php.net/ternary">http://php.net/ternary</a></p>
]]></content:encoded>
			<wfw:commentRss>http://juliusbeckmann.de/blog/howto-use-the-ternary-operator-in-php.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
