<?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 Reference Book Blog &#187; isset</title>
	<atom:link href="http://www.phpreferencebook.com/tag/isset/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.phpreferencebook.com</link>
	<description>PHP Reference: Beginner to Intermediate PHP5</description>
	<lastBuildDate>Thu, 29 Sep 2011 03:50:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Search Speed: Isset vs In_array vs MySQL Query</title>
		<link>http://www.phpreferencebook.com/tips/isset-in_array-mysql/</link>
		<comments>http://www.phpreferencebook.com/tips/isset-in_array-mysql/#comments</comments>
		<pubDate>Thu, 18 Aug 2011 03:07:33 +0000</pubDate>
		<dc:creator>Mario Lurig</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[in_array]]></category>
		<category><![CDATA[isset]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.phpreferencebook.com/?p=445</guid>
		<description><![CDATA[As databases and visitors grow, sometimes &#8216;old code&#8217; doesn&#8217;t work like it use to: it takes too long or times out for exceeding the maximum time allowed by your server for PHP execution. On a MySQL table with 30,000 rows (not too many), the query was checking one column for &#60;3 and then matching a [...]]]></description>
			<content:encoded><![CDATA[<p>As databases and visitors grow, sometimes &#8216;old code&#8217; doesn&#8217;t work like it use to: it takes too long or times out for exceeding the maximum time allowed by your server for PHP execution. On a MySQL table with 30,000 rows (not too many), the query was checking one column for &lt;3 and then matching a long list (10,000) of values in a second column using a long WHERE column2=&#8217;value&#8217; clause. Here is an example of the table:</p>
<pre>
|--column1--|--column2--|
|     0     |    1234   |
|     3     |    5678   |
-------------------------
</pre>
<p>A MySQL query took a while, but didn&#8217;t time out when the list of WHERE column2=&#8217;value&#8217; was less than 7000, but it was just overwhelmed at 10,000. Okay, fine, time to go to phase two: <strong>in_array()</strong>.<br />
There was an existing array of values for column2, so I could simply pull a list of &lt;3 from MySQL (quick) and then loop through (foreach) each of the results and check whether they are <strong>in_array()</strong> for the original source. If they were, just save them to a brand new array that I&#8217;ll use to work further through the problem. For clarification, that means that the system should go through 10,000+ iterations and check if each value is <strong>in_array()</strong>. If you had not already guessed, this was disastrous. <strong>In_Array()</strong> is very inefficient, and if you are dealing with an array that is more than 100 or so values, you&#8217;ll quickly learn this fact. So what to do? Use <strong>isset()</strong>.<br />
<strong>Isset()</strong>, as a function, is incredibly fast. It only checks whether something exists, and is a great way to check for whether something exists in an array. However, the trick is using <strong>isset()</strong> will look at the KEYS, while in_array() will look at the VALUES. Is that problem? Not if you build the original array as <strong>[value]=&gt;value</strong> (set the KEY and VALUE the same). Now, as long as your values are valid keys and unique, this won&#8217;t be a problem. Then, you can use a foreach loop to check whether it is found in the original array. Here is some sample code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$array_source</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'abc'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'abc'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'def'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'def'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'ghi'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'ghi'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$array_results</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'xyz'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'ghi'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$array_results</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array_source</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">//fast</span>
    <span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$v</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//if found, save it to a fresh array</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>What was the final result? The query, run 6 different times for 6 different data sets, went from a total execution time exceeding 2 minutes (MySQL), to not working at all (in_array), to <strong>0.18 seconds</strong>. Yes, less than one second! That is the power for checking whether a value is set when comparing arrays. <strong>Isset()</strong> is even faster by a notable amount when compared to <strong>array_diff()</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpreferencebook.com/tips/isset-in_array-mysql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Use isset() Instead of strlen()</title>
		<link>http://www.phpreferencebook.com/tips/use-isset-instead-of-strlen/</link>
		<comments>http://www.phpreferencebook.com/tips/use-isset-instead-of-strlen/#comments</comments>
		<pubDate>Wed, 15 Apr 2009 04:35:04 +0000</pubDate>
		<dc:creator>Mario Lurig</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[isset]]></category>
		<category><![CDATA[strlen]]></category>

		<guid isPermaLink="false">http://www.phpreferencebook.com/?p=230</guid>
		<description><![CDATA[This tip is courtesy of a great article, 10 Advanced PHP Tips Revisited When referencing an array&#8217;s value via a numeric key, you follow the variable name ($variable) with the numeric index, enclosed by square brackets [5]. e.g. $variable[5] = &#8216;value&#8217; However, when $variable represents a string, using the same syntax will return a string [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>This tip is courtesy of a great article, <a href="http://www.smashingmagazine.com/2009/03/24/10-useful-php-tips-revisited/">10 Advanced PHP Tips Revisited</a></p></blockquote>
<p>When referencing an array&#8217;s value via a numeric key, you follow the variable name ($variable) with the numeric index, enclosed by square brackets [5]. <em>e.g.</em> $variable[5] = &#8216;value&#8217;<br />
However, when $variable represents a string, using the same syntax will return a string with the character at the position specified by the numeric index (0 marks the first character in the $variable string). An example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'abcdefg'</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Output: </strong>string(1) &#8220;c&#8221; </p>
<p>Where this comes into play is when using isset() rather than strlen(). Consider the following example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'abcdefg'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$string</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' found!'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Output: </strong>f found!</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'abcdefg'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$string</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' found!'</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
     <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'No character found at position 7!'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Output: </strong>No character found at position 7!</p>
<p>This is faster than using strlen() because, &#8220;&#8230; calling a function is more expensive than using a language construct.&#8221; It&#8217;s little tricks like this that will keep your code lean and efficient. Thanks to <a href="http://shiflett.org/">Chris Shiflett</a> and <a href="http://seancoates.com/">Sean Coates</a> for their contributions to the referenced article.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpreferencebook.com/tips/use-isset-instead-of-strlen/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

