<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Type Casting In PHP &#8211; What&#8217;s the Point?</title>
	<atom:link href="http://www.dustinweber.com/main-page/type-casting-in-php-whats-the-point/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dustinweber.com/main-page/type-casting-in-php-whats-the-point/</link>
	<description>Dustin Weber's Take On Web Development &#038; Other Random Diversions.</description>
	<lastBuildDate>Tue, 27 Jul 2010 14:42:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Protection against SQL Injection using PDO and Zend Framework &#8211; part 2 &#187; DotKernel</title>
		<link>http://www.dustinweber.com/main-page/type-casting-in-php-whats-the-point/comment-page-1/#comment-924</link>
		<dc:creator>Protection against SQL Injection using PDO and Zend Framework &#8211; part 2 &#187; DotKernel</dc:creator>
		<pubDate>Fri, 18 Jun 2010 12:37:44 +0000</pubDate>
		<guid isPermaLink="false">http://dustinweber.com/web-development/php/type-casting-in-php-whats-the-point/#comment-924</guid>
		<description>[...] A short tip, you can use cast type to avoid SQL Injection in WHERE clause where is possible. $sql= &#039;SELECT * [...]</description>
		<content:encoded><![CDATA[<p>[...] A short tip, you can use cast type to avoid SQL Injection in WHERE clause where is possible. $sql= &#39;SELECT * [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: rich97</title>
		<link>http://www.dustinweber.com/main-page/type-casting-in-php-whats-the-point/comment-page-1/#comment-902</link>
		<dc:creator>rich97</dc:creator>
		<pubDate>Sat, 24 Apr 2010 02:29:03 +0000</pubDate>
		<guid isPermaLink="false">http://dustinweber.com/web-development/php/type-casting-in-php-whats-the-point/#comment-902</guid>
		<description>I use typecasting all of the time. I think it can be quite a nice way to slim down your, code in some cases. For instance:

if (!is_array($data)) {
    $data = array($data);
}
//foreach over $data

With typecasting I can do this:

$data = (array) $data;
//foreach over $data

I also use it regularly to ensure that the correct type is returned by a method.

Please correct me if you think this is wrong.</description>
		<content:encoded><![CDATA[<p>I use typecasting all of the time. I think it can be quite a nice way to slim down your, code in some cases. For instance:</p>
<p>if (!is_array($data)) {<br />
    $data = array($data);<br />
}<br />
//foreach over $data</p>
<p>With typecasting I can do this:</p>
<p>$data = (array) $data;<br />
//foreach over $data</p>
<p>I also use it regularly to ensure that the correct type is returned by a method.</p>
<p>Please correct me if you think this is wrong.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris</title>
		<link>http://www.dustinweber.com/main-page/type-casting-in-php-whats-the-point/comment-page-1/#comment-884</link>
		<dc:creator>Chris</dc:creator>
		<pubDate>Wed, 24 Mar 2010 11:10:58 +0000</pubDate>
		<guid isPermaLink="false">http://dustinweber.com/web-development/php/type-casting-in-php-whats-the-point/#comment-884</guid>
		<description>Eugene, 

The reason why is you have to force the variable to become an integer for security.

Consider a page which uses a GET request:

product.php?id=123

If I&#039;m a nasty hacker, I can potentially do this:

product.php?id=&#039; or &#039;1&#039; = &#039;1

Which could turn (behind the scenes) into 

&quot;SELECT * FROM admin_users WHERE username = &#039;admin&#039; and password = &#039;xxx&#039; OR &#039;1&#039; = &#039;1&#039;;&quot;

PHP won&#039;t automatically convert my GET variable into an integer. However, if I force it to, by casting, the string: &#039; or &#039;1&#039; = &#039;1 will never be passed to my database, but the string 123 will be passed because it can be cast.

Hope that explains it.</description>
		<content:encoded><![CDATA[<p>Eugene, </p>
<p>The reason why is you have to force the variable to become an integer for security.</p>
<p>Consider a page which uses a GET request:</p>
<p>product.php?id=123</p>
<p>If I&#8217;m a nasty hacker, I can potentially do this:</p>
<p>product.php?id=&#8217; or &#8216;1&#8242; = &#8216;1</p>
<p>Which could turn (behind the scenes) into </p>
<p>&#8220;SELECT * FROM admin_users WHERE username = &#8216;admin&#8217; and password = &#8216;xxx&#8217; OR &#8216;1&#8242; = &#8216;1&#8242;;&#8221;</p>
<p>PHP won&#8217;t automatically convert my GET variable into an integer. However, if I force it to, by casting, the string: &#8216; or &#8216;1&#8242; = &#8216;1 will never be passed to my database, but the string 123 will be passed because it can be cast.</p>
<p>Hope that explains it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eugene</title>
		<link>http://www.dustinweber.com/main-page/type-casting-in-php-whats-the-point/comment-page-1/#comment-835</link>
		<dc:creator>Eugene</dc:creator>
		<pubDate>Tue, 08 Dec 2009 07:04:25 +0000</pubDate>
		<guid isPermaLink="false">http://dustinweber.com/web-development/php/type-casting-in-php-whats-the-point/#comment-835</guid>
		<description>Thats all good, but has anyone ever notice that php pretty much sorts the tyes out for herself.
The following 3  all return the same result (10.24) ...

echo ((int)&#039;1024&#039;) / 100;
echo &#039;1024&#039; / 100;
echo &#039;1024&#039; / &#039;100&#039;;

So why?</description>
		<content:encoded><![CDATA[<p>Thats all good, but has anyone ever notice that php pretty much sorts the tyes out for herself.<br />
The following 3  all return the same result (10.24) &#8230;</p>
<p>echo ((int)&#8217;1024&#8242;) / 100;<br />
echo &#8216;1024&#8242; / 100;<br />
echo &#8216;1024&#8242; / &#8216;100&#8242;;</p>
<p>So why?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Konsumer</title>
		<link>http://www.dustinweber.com/main-page/type-casting-in-php-whats-the-point/comment-page-1/#comment-811</link>
		<dc:creator>David Konsumer</dc:creator>
		<pubDate>Tue, 06 Oct 2009 22:22:30 +0000</pubDate>
		<guid isPermaLink="false">http://dustinweber.com/web-development/php/type-casting-in-php-whats-the-point/#comment-811</guid>
		<description>Alex, hans: if you make a query function, and keep your queries as strings, you can automate it, and use %s, without SQL injection.</description>
		<content:encoded><![CDATA[<p>Alex, hans: if you make a query function, and keep your queries as strings, you can automate it, and use %s, without SQL injection.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://www.dustinweber.com/main-page/type-casting-in-php-whats-the-point/comment-page-1/#comment-748</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Wed, 28 Jan 2009 08:43:05 +0000</pubDate>
		<guid isPermaLink="false">http://dustinweber.com/web-development/php/type-casting-in-php-whats-the-point/#comment-748</guid>
		<description>I agree with Hans. Why use mysql_real_escape_string if you know that it should be an integer?? Casting (int) will do. 

It is not only an overkill but also contributing to the global warming. I mean performing senseless computations. Besides calling a function takes time, while (int) is really fast.</description>
		<content:encoded><![CDATA[<p>I agree with Hans. Why use mysql_real_escape_string if you know that it should be an integer?? Casting (int) will do. </p>
<p>It is not only an overkill but also contributing to the global warming. I mean performing senseless computations. Besides calling a function takes time, while (int) is really fast.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Hans</title>
		<link>http://www.dustinweber.com/main-page/type-casting-in-php-whats-the-point/comment-page-1/#comment-737</link>
		<dc:creator>Hans</dc:creator>
		<pubDate>Mon, 08 Dec 2008 18:16:26 +0000</pubDate>
		<guid isPermaLink="false">http://dustinweber.com/web-development/php/type-casting-in-php-whats-the-point/#comment-737</guid>
		<description>Actually the best way would be:
$SQL = &#039;SELECT * FROM table WHERE id = &#039; . (int) $_POST[&#039;input&#039;];
you do not need mysql_real_escape_string when you use (int), because integers cannot be used for SQL injections.

to computerzworld:
if implode and (string) don&#039;t do what you want then use foreach</description>
		<content:encoded><![CDATA[<p>Actually the best way would be:<br />
$SQL = &#8216;SELECT * FROM table WHERE id = &#8216; . (int) $_POST['input'];<br />
you do not need mysql_real_escape_string when you use (int), because integers cannot be used for SQL injections.</p>
<p>to computerzworld:<br />
if implode and (string) don&#8217;t do what you want then use foreach</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Briddo</title>
		<link>http://www.dustinweber.com/main-page/type-casting-in-php-whats-the-point/comment-page-1/#comment-736</link>
		<dc:creator>Briddo</dc:creator>
		<pubDate>Fri, 05 Dec 2008 12:33:57 +0000</pubDate>
		<guid isPermaLink="false">http://dustinweber.com/web-development/php/type-casting-in-php-whats-the-point/#comment-736</guid>
		<description>computerzworld

serialize(array) will turn your array into a string. 
unserialize(string) will turn it back into an array.

Or &gt;&gt;

$string = &quot;&quot;;
foreach ($array as $key =&gt; $value):
    $string .= $value . &#039; &#039;;
endforeach;</description>
		<content:encoded><![CDATA[<p>computerzworld</p>
<p>serialize(array) will turn your array into a string.<br />
unserialize(string) will turn it back into an array.</p>
<p>Or &gt;&gt;</p>
<p>$string = &#8220;&#8221;;<br />
foreach ($array as $key =&gt; $value):<br />
    $string .= $value . &#8216; &#8216;;<br />
endforeach;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: computerzworld</title>
		<link>http://www.dustinweber.com/main-page/type-casting-in-php-whats-the-point/comment-page-1/#comment-646</link>
		<dc:creator>computerzworld</dc:creator>
		<pubDate>Mon, 25 Aug 2008 09:12:57 +0000</pubDate>
		<guid isPermaLink="false">http://dustinweber.com/web-development/php/type-casting-in-php-whats-the-point/#comment-646</guid>
		<description>Hi.... I want to convert my array into string using typecasting without using implode. I tried with (string)Array. But it didn&#039;t worked. How is it possible to convert array to string using typecasting?</description>
		<content:encoded><![CDATA[<p>Hi&#8230;. I want to convert my array into string using typecasting without using implode. I tried with (string)Array. But it didn&#8217;t worked. How is it possible to convert array to string using typecasting?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jack B</title>
		<link>http://www.dustinweber.com/main-page/type-casting-in-php-whats-the-point/comment-page-1/#comment-589</link>
		<dc:creator>Jack B</dc:creator>
		<pubDate>Fri, 27 Jun 2008 17:47:05 +0000</pubDate>
		<guid isPermaLink="false">http://dustinweber.com/web-development/php/type-casting-in-php-whats-the-point/#comment-589</guid>
		<description>Wouldn&#039;t it be just as secure to use the following:

$id = number_format($_POST[&#039;id&#039;], 0, &quot;&quot;, &quot;&quot;);
$sql = &quot;SELECT * FROM table WHERE id = $id&quot;;

This way, if the posted value is not a integer, 0 is returned at the query doesn&#039;t fail?</description>
		<content:encoded><![CDATA[<p>Wouldn&#8217;t it be just as secure to use the following:</p>
<p>$id = number_format($_POST['id'], 0, &#8220;&#8221;, &#8220;&#8221;);<br />
$sql = &#8220;SELECT * FROM table WHERE id = $id&#8221;;</p>
<p>This way, if the posted value is not a integer, 0 is returned at the query doesn&#8217;t fail?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Konsumer</title>
		<link>http://www.dustinweber.com/main-page/type-casting-in-php-whats-the-point/comment-page-1/#comment-470</link>
		<dc:creator>David Konsumer</dc:creator>
		<pubDate>Sat, 29 Sep 2007 04:04:51 +0000</pubDate>
		<guid isPermaLink="false">http://dustinweber.com/web-development/php/type-casting-in-php-whats-the-point/#comment-470</guid>
		<description>Bah, bad formatting.

sprintf(&#039;SELECT * FROM table WHERE id = %d&#039;, mysql_real_escape_string($_POST[&#039;input&#039;]));</description>
		<content:encoded><![CDATA[<p>Bah, bad formatting.</p>
<p>sprintf(&#8216;SELECT * FROM table WHERE id = %d&#8217;, mysql_real_escape_string($_POST['input']));</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Konsumer</title>
		<link>http://www.dustinweber.com/main-page/type-casting-in-php-whats-the-point/comment-page-1/#comment-469</link>
		<dc:creator>David Konsumer</dc:creator>
		<pubDate>Sat, 29 Sep 2007 04:03:31 +0000</pubDate>
		<guid isPermaLink="false">http://dustinweber.com/web-development/php/type-casting-in-php-whats-the-point/#comment-469</guid>
		<description>I use sprintf for the same purpose, with the plus side being that you can keep all your queries in a config file.
&lt;code&gt;

&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>I use sprintf for the same purpose, with the plus side being that you can keep all your queries in a config file.<br />
<code></p>
<p></code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jim Kane</title>
		<link>http://www.dustinweber.com/main-page/type-casting-in-php-whats-the-point/comment-page-1/#comment-468</link>
		<dc:creator>Jim Kane</dc:creator>
		<pubDate>Fri, 28 Sep 2007 13:51:05 +0000</pubDate>
		<guid isPermaLink="false">http://dustinweber.com/web-development/php/type-casting-in-php-whats-the-point/#comment-468</guid>
		<description>Great post, Ned!  That is your name from now on.</description>
		<content:encoded><![CDATA[<p>Great post, Ned!  That is your name from now on.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
