views:

297

answers:

2

I have the following line:

$this->magicQuotes = (bool) get_magic_quotes_gpc();

I am taking the get_magic_quotes_gpc() storing it in my object as it's used many times as I generate the SQL. I am also converting it to a bool.

I'm wondering if it's worth while converting it to bool. The main reason I am is for speed as the statement that checks is:

if ($this->magicQuotes) { ... }

which I think would be slightly faster if the test is strictly a bool value.

Is there any reason this it isn't faster or if there are any other reasons not to do this?

+2  A: 

If you don't convert to bool, PHP will have to convert to bool anyways when evaluating the expression in the if statement. So whether you convert it explicitly or not, it will get converted to bool at one time or another, and I'd prefer the way that lets you type less.

Also, with this kind of tiny optimization, you should be worrying more about which is more readable than worrying about which is 0.0...01 seconds faster than the other.

yjerem
+1  A: 

It shouldn't be any faster... depending on how php converts both bool and int at the end.

Typically, on x86, both bool and int, in if(foo) would be a test against zero, and jump accordingly. So the cast shouldn't improve performance.

Calyth