<?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>Yoda Condition &#187; Quicktipp</title>
	<atom:link href="http://yoda.neun12.de/artikel-category/php/quicktipp/feed" rel="self" type="application/rss+xml" />
	<link>http://yoda.neun12.de</link>
	<description>Debuggen du musst</description>
	<lastBuildDate>Sun, 18 May 2014 13:49:01 +0000</lastBuildDate>
	<language>de-DE</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.9.40</generator>
	<item>
		<title>Klassen-Konstante von außerhalb der Klasse auslesen</title>
		<link>http://yoda.neun12.de/artikel-76</link>
		<comments>http://yoda.neun12.de/artikel-76#comments</comments>
		<pubDate>Sun, 08 Jul 2012 00:12:18 +0000</pubDate>
		<dc:creator><![CDATA[Ralf]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Quicktipp]]></category>

		<guid isPermaLink="false">http://yoda.neun12.de/?p=76</guid>
		<description><![CDATA[Hin und wieder komme ich in die Verlegenheit außerhalb einer Klasse eine ihrer Konstante verwenden zu wollen oder zu müssen. Auf direkten Weg, also z.B. mit Foo:CONSTANT geht es nicht. Mit der folgenden Methode geht es jedoch wunderbar:]]></description>
				<content:encoded><![CDATA[<p>Hin und wieder komme ich in die Verlegenheit außerhalb einer Klasse eine ihrer Konstante verwenden zu wollen oder zu müssen. Auf direkten Weg, also z.B. mit <code>Foo:CONSTANT</code> geht es nicht. Mit der folgenden Methode geht es jedoch wunderbar: <div class="gistem"><div id="gist-3068674" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="cp">&lt;?php</span></div><div class='line' id='LC2'><span class="k">class</span> <span class="nc">Foo</span></div><div class='line' id='LC3'><span class="p">{</span></div><div class='line' id='LC4'>	<span class="k">const</span> <span class="no">BAR</span> <span class="o">=</span> <span class="s1">&#39;Hallo&#39;</span><span class="p">;</span></div><div class='line' id='LC5'>	<span class="k">const</span> <span class="no">BAZ</span> <span class="o">=</span> <span class="s1">&#39;Welt!&#39;</span><span class="p">;</span></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'>	<span class="sd">/**</span></div><div class='line' id='LC8'><span class="sd">	 * </span></div><div class='line' id='LC9'><span class="sd">	 * Returns the value of a class constant</span></div><div class='line' id='LC10'><span class="sd">	 * @param string $const Name of the constant</span></div><div class='line' id='LC11'><span class="sd">	 * @param string $class [optional] Name of the class (if not set, the constant will be searched in this class) </span></div><div class='line' id='LC12'><span class="sd">	 * @return mixed|false The value of the constant or false if it does not exists</span></div><div class='line' id='LC13'><span class="sd">	 */</span></div><div class='line' id='LC14'>	<span class="k">public</span> <span class="k">static</span> <span class="k">function</span> <span class="nf">get_constant</span><span class="p">(</span> <span class="nv">$const</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="nv">$class_name</span> <span class="o">=</span> <span class="nx">__CLASS__</span> <span class="p">){</span></div><div class='line' id='LC15'><br/></div><div class='line' id='LC16'>		<span class="k">if</span><span class="p">(</span> <span class="k">empty</span><span class="p">(</span> <span class="nv">$const</span> <span class="p">)</span> <span class="o">||</span> <span class="o">!</span> <span class="nb">is_string</span><span class="p">(</span> <span class="nv">$const</span> <span class="p">)</span> <span class="o">||</span> <span class="o">!</span> <span class="nb">class_exists</span><span class="p">(</span> <span class="nv">$class_name</span> <span class="p">)</span> <span class="p">)</span></div><div class='line' id='LC17'>			<span class="k">return</span> <span class="k">FALSE</span><span class="p">;</span></div><div class='line' id='LC18'><br/></div><div class='line' id='LC19'>		<span class="nv">$reflection</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ReflectionClass</span><span class="p">(</span> <span class="nv">$class_name</span> <span class="p">);</span></div><div class='line' id='LC20'><br/></div><div class='line' id='LC21'>		<span class="k">return</span> <span class="nv">$reflection</span><span class="o">-&gt;</span><span class="na">getConstant</span><span class="p">(</span> <span class="nv">$const</span> <span class="p">);</span></div><div class='line' id='LC22'><br/></div><div class='line' id='LC23'>	<span class="p">}</span></div><div class='line' id='LC24'><br/></div><div class='line' id='LC25'><span class="p">}</span></div><div class='line' id='LC26'><br/></div><div class='line' id='LC27'><span class="k">class</span> <span class="nc">Bar</span></div><div class='line' id='LC28'><span class="p">{</span></div><div class='line' id='LC29'>	<span class="k">const</span> <span class="no">FOO</span> <span class="o">=</span> <span class="s1">&#39;Horst&#39;</span><span class="p">;</span></div><div class='line' id='LC30'><span class="p">}</span></div><div class='line' id='LC31'><br/></div><div class='line' id='LC32'><span class="nv">$consts</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span> <span class="s1">&#39;BAR&#39;</span><span class="p">,</span> <span class="s1">&#39;BAZ&#39;</span><span class="p">,</span> <span class="s1">&#39;FOO&#39;</span> <span class="p">);</span></div><div class='line' id='LC33'><span class="k">foreach</span><span class="p">(</span> <span class="nv">$consts</span> <span class="k">as</span> <span class="nv">$const</span> <span class="p">)</span></div><div class='line' id='LC34'>	<span class="nb">var_dump</span><span class="p">(</span> <span class="nx">Foo</span><span class="o">::</span><span class="na">get_constant</span><span class="p">(</span> <span class="nv">$const</span> <span class="p">)</span> <span class="p">);</span></div><div class='line' id='LC35'><br/></div><div class='line' id='LC36'><span class="nb">var_dump</span><span class="p">(</span> <span class="nx">Foo</span><span class="o">::</span><span class="na">get_constant</span><span class="p">(</span> <span class="s1">&#39;FOO&#39;</span><span class="p">,</span> <span class="s1">&#39;Bar&#39;</span> <span class="p">)</span> <span class="p">);</span></div><div class='line' id='LC37'><br/></div><div class='line' id='LC38'><span class="nb">var_dump</span><span class="p">(</span> <span class="nx">Foo</span><span class="o">::</span><span class="na">get_constant</span><span class="p">(</span> <span class="s1">&#39;TEST&#39;</span><span class="p">,</span> <span class="s1">&#39;Baz&#39;</span> <span class="p">)</span> <span class="p">);</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/3068674/117dd6890c9796b733f04a24e8c792098d63fdaa/get_constant.php" style="float:right;">view raw</a>
            <a href="https://gist.github.com/3068674#file_get_constant.php" style="float:right;margin-right:10px;color:#666">get_constant.php</a>
            <a href="https://gist.github.com/3068674">This Gist</a> is brought to you using <a href="http://en.bainternet.info/2011/simple-gist-embed"><small>Simple Gist Embed</small></a>.
          </div>
        </div>
</div>
</div><style type="text/css">@import "http://gist.github.com/stylesheets/gist/embed.css"; .gistem .highlight {background: inherit; !important;}</style></p>
]]></content:encoded>
			<wfw:commentRss>http://yoda.neun12.de/artikel-76/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Gummi-Thickbox</title>
		<link>http://yoda.neun12.de/artikel-73</link>
		<comments>http://yoda.neun12.de/artikel-73#comments</comments>
		<pubDate>Wed, 30 May 2012 14:32:40 +0000</pubDate>
		<dc:creator><![CDATA[Ralf]]></dc:creator>
				<category><![CDATA[Code-Snippets]]></category>
		<category><![CDATA[Quicktipp]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://yoda.neun12.de/?p=73</guid>
		<description><![CDATA[Will man im Backend von WordPress die Thickbox verwenden, bekommt man ein Problem. Zumindest dann, wenn man mit der vorgegebenen Größe nicht einverstanden ist. Die wird nämlich von WordPress mittels eines Scriptes vorgegeben. Bindet man zusätzlich den Editor ein, wird es richtig haarig. Denn im media-upload.js steht dummerweise die Zeile $('a.thickbox').each( function() {...} Also alle [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Will man im Backend von WordPress die Thickbox verwenden, bekommt man ein Problem. Zumindest dann, wenn man mit der vorgegebenen Größe nicht einverstanden ist. Die wird nämlich von WordPress mittels eines Scriptes vorgegeben. Bindet man zusätzlich den Editor ein, wird es richtig haarig. Denn im media-upload.js steht dummerweise die Zeile <code>$('a.thickbox').each( function() {...}</code><br />
Also alle Links mit der Klasse <code>thickbox</code> werden manipuliert. Würde man die Klasse <code>thickbox</code> weg lassen, würde der Link keine Thickbox öffnen. Blöde Situation das ganze.</p>
<p>Ich suchte eine Möglichkeit wie ich die Thickbox in ihrer Größe ändern kann, darüber hinaus mir aber keine Gedanken mehr machen muss wie groß ich sie dimensionieren soll. Die Thickbox sollte sich in ihrer Größe immer schön am dargestellten Inhalt ausrichten. Nach etlichen rumprobieren und etwas suchen, kam ich dann auf folgende Lösung:<div class="gistem"><div id="gist-2837799" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">jQuery</span><span class="p">(</span><span class="nb">document</span><span class="p">).</span><span class="nx">ready</span><span class="p">(</span></div><div class='line' id='LC2'>	<span class="kd">function</span><span class="p">(</span><span class="nx">$</span><span class="p">){</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'>		<span class="nx">resize_thickbox_iframe</span><span class="p">();</span></div><div class='line' id='LC5'><span class="cm">/*</span></div><div class='line' id='LC6'><span class="cm"> *</span></div><div class='line' id='LC7'><span class="cm"> * some other jQuery code</span></div><div class='line' id='LC8'><span class="cm"> *</span></div><div class='line' id='LC9'><span class="cm"> */</span></div><div class='line' id='LC10'><br/></div><div class='line' id='LC11'>		<span class="kd">function</span> <span class="nx">resize_thickbox_iframe</span><span class="p">(){</span></div><div class='line' id='LC12'><br/></div><div class='line' id='LC13'>			<span class="c1">//padding</span></div><div class='line' id='LC14'>			<span class="kd">var</span> <span class="nx">pad</span> <span class="o">=</span> <span class="mi">50</span><span class="p">;</span></div><div class='line' id='LC15'><br/></div><div class='line' id='LC16'>			<span class="c1">// give the body a height</span></div><div class='line' id='LC17'>			<span class="nx">$</span><span class="p">(</span> <span class="s1">&#39;body&#39;</span> <span class="p">).</span><span class="nx">css</span><span class="p">(</span> <span class="s1">&#39;height&#39;</span><span class="p">,</span> <span class="s1">&#39;auto&#39;</span> <span class="p">);</span></div><div class='line' id='LC18'><br/></div><div class='line' id='LC19'>			<span class="c1">// read the bodys height and add some padding</span></div><div class='line' id='LC20'>			<span class="kd">var</span> <span class="nx">mh</span> <span class="o">=</span> <span class="nb">parseInt</span><span class="p">(</span> <span class="nx">$</span><span class="p">(</span> <span class="s1">&#39;body&#39;</span> <span class="p">).</span><span class="nx">height</span><span class="p">()</span> <span class="p">)</span> <span class="o">+</span> <span class="nx">pad</span><span class="p">;</span></div><div class='line' id='LC21'><br/></div><div class='line' id='LC22'>			<span class="kd">var</span> <span class="nx">max_height</span> <span class="o">=</span> <span class="nx">mh</span> <span class="o">+</span> <span class="s1">&#39;px&#39;</span><span class="p">;</span></div><div class='line' id='LC23'><br/></div><div class='line' id='LC24'>			<span class="c1">// get the parent window dimensions</span></div><div class='line' id='LC25'>			<span class="kd">var</span> <span class="nx">parent</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span> <span class="nb">window</span><span class="p">.</span><span class="nx">parent</span><span class="p">.</span><span class="nb">document</span> <span class="p">);</span></div><div class='line' id='LC26'>			<span class="kd">var</span> <span class="nx">ph</span> <span class="o">=</span> <span class="nx">parent</span><span class="p">.</span><span class="nx">height</span><span class="p">();</span></div><div class='line' id='LC27'><br/></div><div class='line' id='LC28'>			<span class="c1">// get the ID of this iframe and the iframe itself</span></div><div class='line' id='LC29'>			<span class="nx">frame_id</span> <span class="o">=</span> <span class="nx">frameElement</span><span class="p">.</span><span class="nx">id</span><span class="p">;</span></div><div class='line' id='LC30'>			<span class="nx">frame</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span> <span class="s1">&#39;#&#39;</span><span class="o">+</span><span class="nx">frame_id</span><span class="p">,</span> <span class="nb">window</span><span class="p">.</span><span class="nx">parent</span><span class="p">.</span><span class="nb">document</span> <span class="p">);</span></div><div class='line' id='LC31'><br/></div><div class='line' id='LC32'>			<span class="c1">// get the id of the overlay-div and the overlay-div itself</span></div><div class='line' id='LC33'>			<span class="nx">div_id</span> <span class="o">=</span> <span class="nx">frame</span><span class="p">.</span><span class="nx">parent</span><span class="p">().</span><span class="nx">attr</span><span class="p">(</span> <span class="s1">&#39;id&#39;</span> <span class="p">);</span></div><div class='line' id='LC34'>			<span class="nx">div</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span> <span class="s1">&#39;#&#39;</span><span class="o">+</span><span class="nx">div_id</span><span class="p">,</span> <span class="nb">window</span><span class="p">.</span><span class="nx">parent</span><span class="p">.</span><span class="nb">document</span> <span class="p">);</span></div><div class='line' id='LC35'><br/></div><div class='line' id='LC36'>			<span class="c1">// first set the hight for the frame, than set the hight for the overlay-div</span></div><div class='line' id='LC37'>			<span class="nx">frame</span><span class="p">.</span><span class="nx">css</span><span class="p">(</span> <span class="s1">&#39;height&#39;</span><span class="p">,</span> <span class="nx">max_height</span> <span class="p">);</span></div><div class='line' id='LC38'>			<span class="nx">div</span><span class="p">.</span><span class="nx">css</span><span class="p">(</span> <span class="s1">&#39;height&#39;</span><span class="p">,</span> <span class="nx">max_height</span> <span class="p">);</span></div><div class='line' id='LC39'><br/></div><div class='line' id='LC40'>			<span class="c1">// calculate the new top position</span></div><div class='line' id='LC41'>			<span class="kd">var</span> <span class="nx">tb</span> <span class="o">=</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">round</span><span class="p">(</span> <span class="p">(</span> <span class="p">(</span> <span class="nx">ph</span> <span class="o">-</span> <span class="nx">mh</span> <span class="p">)</span> <span class="o">/</span> <span class="mi">2</span> <span class="p">)</span> <span class="o">-</span> <span class="nx">pad</span> <span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;px&#39;</span><span class="p">;</span></div><div class='line' id='LC42'><br/></div><div class='line' id='LC43'>			<span class="c1">// set the new top position</span></div><div class='line' id='LC44'>			<span class="nx">frame</span><span class="p">.</span><span class="nx">css</span><span class="p">(</span> <span class="s1">&#39;top&#39;</span><span class="p">,</span> <span class="nx">tb</span> <span class="p">);</span></div><div class='line' id='LC45'>			<span class="nx">div</span><span class="p">.</span><span class="nx">css</span><span class="p">(</span> <span class="s1">&#39;top&#39;</span><span class="p">,</span> <span class="nx">tb</span> <span class="p">);</span></div><div class='line' id='LC46'><br/></div><div class='line' id='LC47'>		<span class="p">}</span></div><div class='line' id='LC48'><br/></div><div class='line' id='LC49'>	<span class="p">}</span></div><div class='line' id='LC50'><span class="p">);</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/2837799/230e1f0bcb365096666c10b746e6b11188b00299/resize_thickbox.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/2837799#file_resize_thickbox.js" style="float:right;margin-right:10px;color:#666">resize_thickbox.js</a>
            <a href="https://gist.github.com/2837799">This Gist</a> is brought to you using <a href="http://en.bainternet.info/2011/simple-gist-embed"><small>Simple Gist Embed</small></a>.
          </div>
        </div>
</div>
</div></p>
<p>Das Script setzt die Höhe des body-Tags im iFrame zuerst auf &#8216;auto&#8217; damit dieser eine Höhe bekommt (für den Fall das der body-Tag noch keine Höhe besitzt). Danach wird das iFrame aus dem Eltern-Dokument gefischt und seine Höhe angepasst. Da sich das iFrame allerdings innerhalb eines Overlay-Divs befindet, muss dieser ebenfalls aus dem DOM gefischt und angepasst werden. Zum Schluss werden Overlay-Div und iFrame neu ausgerichtet.</p>
<p>Das Script gehört <strong>in das</strong> iFrame hinein! Dementsprechend muss im iFrame auch noch jQuery geladen werden. Wer im iFrame auf jQuery verzichten kann, muss sihc die Funktion auf pures JavaScript umschreiben (ist auch nicht sooo schwer).</p>
]]></content:encoded>
			<wfw:commentRss>http://yoda.neun12.de/artikel-73/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quicktipp: Checkboxen auswerten</title>
		<link>http://yoda.neun12.de/artikel-61</link>
		<comments>http://yoda.neun12.de/artikel-61#comments</comments>
		<pubDate>Sat, 11 Feb 2012 14:19:19 +0000</pubDate>
		<dc:creator><![CDATA[Ralf]]></dc:creator>
				<category><![CDATA[Code-Snippets]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Quicktipp]]></category>
		<category><![CDATA[auswertung]]></category>
		<category><![CDATA[checkboxen]]></category>
		<category><![CDATA[formular]]></category>

		<guid isPermaLink="false">http://yoda.neun12.de/?p=61</guid>
		<description><![CDATA[Vor kurzem hatte ich das Problem das ich mehrere Checkboxen bzw. deren Kombination auswerten musste. Normalerweise eine Arbeit für Leute die Mutter &#38; Vadder erschlagen haben. Dazu gleich mal die Bedingungen: Wir haben drei Checkboxen (a, b und c) in einem Formular und müssen auswerten welche Checkbox ausgewählt wurde bzw. nicht ausgewählt wurde. Wenn das [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Vor kurzem hatte ich das Problem das ich mehrere Checkboxen bzw. deren Kombination auswerten musste. Normalerweise eine Arbeit für Leute die Mutter &amp; Vadder erschlagen haben. Dazu gleich mal die Bedingungen:<br />
Wir haben drei Checkboxen (a, b und c) in einem Formular und müssen auswerten welche Checkbox ausgewählt wurde bzw. <em>nicht</em> ausgewählt wurde. Wenn das Formular abgeschickt wird, bekommen wir ein POST- bzw. GET-Array mit den Werten (ich gehe im weiteren mal von einem POST-Array aus).<br />
Das Böse an Checkboxen ist, wenn eine Checkbox nicht ausgewählt wurde, wird auch kein Wert im POST- bzw. GET-Array gesetzt.<br />
Nach dem Absenden des Formulars bekommen wir also in etwa so etwas:</p>
<pre class="brush:php">$mPost = array(
		'action'	=&gt; 'send',
		'user'		=&gt; 'Horst',
		'a'		=&gt; 'On',
		'c'		=&gt; 'On',
		'foo'		=&gt; 'bar',
		'baz'		=&gt; '',
	);</pre>
<p>Dies ist nun eine Simulation des POST-Arrays und man kann sehen das für &#8216;b&#8217; kein Wert gesetzt wurde sofern die Checkbox &#8216;b&#8217; nicht ausgewählt wurde.<br />
Wie wertet man nun üblicherweise die drei Checkboxen aus? Wahrscheinlich mit vielen <code>If-ElseIf-Else</code> Konstrukten und <code>isset()</code>:</p>
<pre class="brush:php">if( isset( $_POST['a'] ) )
...
elseif( isset( $_POST['a'] ) &amp;&amp; isset( $_POST['b'] ) )
...
elseif( isset( $_POST['a' ) &amp;&amp; ! isset( $_POST['b' ) )
...

if( isset( $_POST['b'] ) &amp;&amp; isset( $_POST['c'] ) )
...</pre>
<p>So wühlt man sich Zeile für Zeile durch alle möglichen Kombinationen und versucht keine zu vergessen. <strong>Das. Ist. Blöd!</strong> Und nicht nur blöd, sondern auch verdammt unübersichtlich. Bei drei Checkboxen mag es noch machbar sein, aber wenn man fünf, acht oder mehr Checkboxen hat, dann wird es fast schon unmöglich alle Kombinationen von ausgewählten und nicht ausgewählten Checkboxen zu berücksichtigen.</p>
<p>Da eine Checkbox nur ein Ja-Nein-Wert (Wahr oder Falsch) darstellt, habe ich mich recht schnell an die Zeit erinnert als ich mit der Computerei angefangen habe. Damals hatte ich einen C16 mit 16kB RAM. Dort war jedes Bit kostbarer als Gold und durfte nicht verschwendet werden. Deswegen hatte man oft mit Bitmasken gearbeitet. In einer Bitmaske steht jedes Bit für einen Ja oder einen Nein-Wert. Das war und ist recht effizient, denn in einem Byte kann ich so 8 Werte speichern. Und natürlich auch wieder abfragen.</p>
<p>Nun stehen wir vor dem Problem unsere drei Checkboxen in eine Bitmaske zu bekommen. Dazu benötigen wir erst einmal eine entsprechende Maske. Mit <code>printf()</code> bzw. <code>sprintf()</code> können wir eine solche Maske leicht erstellen:</p>
<pre class="brush:php">$bin = sprintf( '%b%b%b', $mPost['a'], $mPost['b'], $mPost['c'] );</pre>
<p>Das PHP-Manual sagt zum Platzhalter <code>%b</code> folgendes:</p>
<blockquote><p>das Argument wird als Integer angesehen und als Binär-Wert ausgegeben</p></blockquote>
<p><code>%b</code> wird also immer zu 1 oder 0 umgewandelt. Wandeln wir nun unsere Variablen (a, b und c) in Boolsche Werte um, so würde PHP <code>%b</code> durch 1 bzw. 0 ersetzen, je nachdem ob die Variable <code>TRUE</code> oder <code>FALSE</code> ist. Denn zuerst würde der boolsche Wert in einen Integer (1 bzw. 0) umgewandelt und dann in einen einstelligen Binärwert. Dies erreichen wir dadurch, dass wir mit <code>isset()</code> abfragen ob die Variable überhaupt existiert. Somit umschiffen wir gleichzeitig das Problem das nicht ausgewählte Checkboxen keinen Wert übertragen.</p>
<pre class="brush:php">$bin = printf( '%b%b%b', isset( $mPost['a'] ), isset( $mPost['b'] ), isset( $mPost['c'] ) );</pre>
<p>Das klappt soweit schon wunderbar und bei drei Checkboxen ist es auch noch recht übersichtlich.<br />
Als Ergebnis erhalten wir einen String der z.B. &#8220;101&#8243; enthält. Je nachdem welche Variable gesetzt ist und welche nicht. Diesen String können wir nun ganz einfach mit einem <code>switch-case </code>abfragen und darauf reagieren:</p>
<pre class="brush:php">switch( $bin ){
	case '000':
		//nichts ausgewaehlt
	break;

	case '100':
		// nur checkbox 'a' wurde ausgewählt
	break;

	case '101':
		// checkbox 'a' wurde ausgewaehlt, checkbox 'b' jedoch NICHT, checkbox 'c' ist uns egal
	break;

	default:
		// alle anderen Faelle
	break;
}</pre>
<p>Bei drei Checkboxen ist das schon eine ganz brauchbare Lösung. Aber was ist wenn wir jetzt, sagen wir mal, 20 Checkboxen haben. Zum Beispiel eine Umfrage oder ähnliches?<br />
Hier stehen wir vor zwei Problemen. Zum einen wäre der <code>sprintf()</code> nicht mehr übersichtlich und recht unbrauchbar. Zum anderen müssen wir ja gewähren das jede Variable an ihren Platz ist und nicht z.B. &#8216;a&#8217; und &#8216;g&#8217; vertauscht sind.</p>
<p>Als erstes legen wir uns mal ein Muster fest welche Variablen uns in welcher Reihenfolge interessieren. Das machen wir einfach mit einem Array:</p>
<pre class="brush:php">$defaults = array(
		'a'	=&gt; '',
		'b'	=&gt; '',
		'c'	=&gt; '',
	);</pre>
<p>Damit wir nicht mit uninitialisierten Variablen arbeiten müssen, mischen wir das POST-Array mit unserer Vorgabe. Dadurch gehen wir sicher das alle benötigten Variablen auch mit einem (leeren) Wert initialisiert sind.</p>
<pre class="brush:php">$data = array_merge( $defaults, $mPost );</pre>
<p>Nun müssen wir nur noch über unser Vorgabe-Array (<code>$defaults</code>) laufen und abfragen ob die entsprechenden Schlüssel im Daten-Array (<code>$data</code>) gesetzt sind.</p>
<pre class="brush:php">$bin = '';
foreach( $defaults as $key =&gt; $val )
	$bin .= sprintf( '%b', !!$data[$key] );</pre>
<p>Dies kann man auch anders lösen, z.B. mit einem Trinären Operator. Je nach Geschmack des Programmierers halt:</p>
<pre class="brush:php">$bin .= ! empty( $data[$key] ) ? '1' : '0';</pre>
<p>Wenn der entsprechende Wert im Daten-Array <em>nicht</em> leer ist, wird eine 1 ausgegeben, ansonsten eine 0.<br />
Als Ergebnis erhalten wir wieder einen String mit vielen Einsen und Nullen den wir im Switch-Case abfragen können. Wandelt man den String mit <code>bindec()</code> in eine Dezimalzahl um, kann man die Auswahl schön platzsparend speichern. Wenn man mit Cookies arbeitet, wird man nämlich schnell wieder daran erinnert das Speicherplatz Mangelware ist. Odre man nutzt den Dezimalwert um eine statistische Auswertung durchzuführen. Oder verwendet ihn im Switch-Case. Oder mit If-Abfragen. Oder, oder, oder&#8230;</p>
<p>Und hier das ganze dann  noch mal als komplettes Script: <div class="gistem"><div id="gist-1799597" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="x">$mPost = array(</span></div><div class='line' id='LC2'><span class="x">		&#39;action&#39;	=&gt; &#39;send&#39;,</span></div><div class='line' id='LC3'><span class="x">		&#39;user&#39;		=&gt; &#39;Horst&#39;,</span></div><div class='line' id='LC4'><span class="x">		&#39;a&#39;			=&gt; &#39;On&#39;,</span></div><div class='line' id='LC5'><span class="x">		&#39;c&#39;			=&gt; &#39;On&#39;,</span></div><div class='line' id='LC6'><span class="x">		&#39;foo&#39;		=&gt; &#39;bar&#39;,</span></div><div class='line' id='LC7'><span class="x">		&#39;baz&#39;		=&gt; &#39;&#39;,</span></div><div class='line' id='LC8'><span class="x">	);</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'><span class="x">$defaults = array(</span></div><div class='line' id='LC11'><span class="x">		&#39;a&#39;	=&gt; &#39;&#39;,</span></div><div class='line' id='LC12'><span class="x">		&#39;b&#39;	=&gt; &#39;&#39;,</span></div><div class='line' id='LC13'><span class="x">		&#39;c&#39;	=&gt; &#39;&#39;,</span></div><div class='line' id='LC14'><span class="x">	);</span></div><div class='line' id='LC15'><br/></div><div class='line' id='LC16'><span class="x">$data = array_merge( $defaults, $mPost );</span></div><div class='line' id='LC17'><br/></div><div class='line' id='LC18'><span class="x">$bin = &#39;&#39;;</span></div><div class='line' id='LC19'><br/></div><div class='line' id='LC20'><span class="x">foreach( $defaults as $key =&gt; $val ){</span></div><div class='line' id='LC21'><span class="x">	$bin .= sprintf( &#39;%b&#39;, !!$data[$key] );</span></div><div class='line' id='LC22'><span class="x">	//$bin .= ! empty( $data[$key] ) ? &#39;1&#39; : &#39;0&#39;;</span></div><div class='line' id='LC23'><span class="x">}	</span></div><div class='line' id='LC24'><br/></div><div class='line' id='LC25'><span class="x">$dec = bindec( $bin );</span></div><div class='line' id='LC26'><br/></div><div class='line' id='LC27'><span class="x">var_dump( $bin );</span></div><div class='line' id='LC28'><span class="x">var_dump( $dec );</span></div><div class='line' id='LC29'><br/></div><div class='line' id='LC30'><span class="x">switch( $bin ){</span></div><div class='line' id='LC31'><span class="x">	case &#39;000&#39;:</span></div><div class='line' id='LC32'><span class="x">		//nichts ausgewaehlt</span></div><div class='line' id='LC33'><span class="x">	break;</span></div><div class='line' id='LC34'><span class="x">	</span></div><div class='line' id='LC35'><span class="x">	case &#39;100&#39;:</span></div><div class='line' id='LC36'><span class="x">		// nur checkbox &#39;a&#39; wurde ausgewählt</span></div><div class='line' id='LC37'><span class="x">	break;</span></div><div class='line' id='LC38'><span class="x">	</span></div><div class='line' id='LC39'><span class="x">	case &#39;101&#39;:</span></div><div class='line' id='LC40'><span class="x">		// checkbox &#39;a&#39; wurde ausgewaehlt, checkbox &#39;b&#39; jedoch NICHT</span></div><div class='line' id='LC41'><span class="x">	break;</span></div><div class='line' id='LC42'><span class="x">	</span></div><div class='line' id='LC43'><span class="x">	default:</span></div><div class='line' id='LC44'><span class="x">		// alle anderen Faelle</span></div><div class='line' id='LC45'><span class="x">	break;</span></div><div class='line' id='LC46'><span class="x">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1799597/a2eab75bf40e1ed29ddde59a7172caa57af58568/file1.php" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1799597#file_file1.php" style="float:right;margin-right:10px;color:#666">file1.php</a>
            <a href="https://gist.github.com/1799597">This Gist</a> is brought to you using <a href="http://en.bainternet.info/2011/simple-gist-embed"><small>Simple Gist Embed</small></a>.
          </div>
        </div>
</div>
</div></p>
<p>Zum Schluss noch etwas &#8220;dreckiges&#8221; PHP. Hier möchte ich mal schnell den NOTNOT-Operator einwerfen auf den ich bei der Suche nach der Lösung gestoßen bin. <code>!$data[$key]</code> wird Wahr (TRUE) wenn $data[$key] <em>leer</em> ist. Ich möchte aber wissen ob eine Variable <em>nicht</em> leer ist, denn <code>printf()</code> soll ja aus &#8216;%b&#8217; eine &#8217;1&#8242; machen wenn die Variable gesetzt ist. Also muss ich die Bedingung <code>!$data[$key]</code> noch einmal negieren. Aus <code>!$data[$key]</code> wird also <code>!!$data[$key]</code>.<br />
Dies ist also im Grunde genommen nur eine fürchterliche Kurzschreibweise für <code> ! empty( $data[$key] )</code>. Für das schnelle Programmieren sind solche Kurzschreibweisen ganz nützlich. Sie sparen eine Menge Tipperei wenn man viel Code schreiben muss. Für die Lesbarkeit des Codes sind sie jedoch sehr kontraproduktiv. Man muss schon genau überlegen was da gerade passiert und sicher gehen das die Variable auch initialisiert ist, ansonsten hagelt es Notices.</p>
]]></content:encoded>
			<wfw:commentRss>http://yoda.neun12.de/artikel-61/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
