Re: [PHP] Looking for caveats to the following code
On Thu, August 17, 2006 5:19 pm, Chris W. Parker wrote: The issue is whether or not this is a safe test. My initial thought is that it is safe since I'm simply checking for true/false-ness. I either check for '!== false' explicitly or (in the case of the latter example) check that something other than 'false' is returned. It would probably be better to check: if (do_something('hello') !== false){ } It's slightly less readable but it seems more efficient (if nothing more than to save on the number of lines typed). Really, though, the question is more about 'hello' and where it comes from than anything else. If you have already scrubbed and validated 'hello' and you know it does not start with the digit 0, and it's not an array, and... Actually, to be correct, you should be able to say what 'hello' *IS* rather than what it is not. The rest of the code all hinges on the question of what's in the REAL world for 'hello' -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Looking for caveats to the following code
Chris W. Parker wrote: Hello, ?php if($result = do_something('hello')) { // do something with $result } else { // do some other stuff } ? nothing wrong with this. like Adam mentioned you can check the expression to see if it is exactly false - and if you don't need $result you can also do it like so: if (do_something('hello') !== false) { } or // some consider this safer because accidentally assigning // to a literal will cause an error - so you know right away when you made a type if (false !== do_something('hello')) { } to make the 'safety' issue clearer consider these snippets: if (false == $res) echo boo!; if (false = $res) echo boo!; // typo with fatal! if ($res == false) echo boo!; if ($res = false) echo boo!; // typo with no error but your logic is silently borked! The issue is whether or not this is a safe test. My initial thought is that it is safe since I'm simply checking for true/false-ness. I either check for '!== false' explicitly or (in the case of the latter example) check that something other than 'false' is returned. It's slightly less readable but it seems more efficient (if nothing more than to save on the number of lines typed). readable is probably a question of preference and what your acustomed to; in situations where you don't need $result you can save on creating a variable by testing directly on the return result of the given function. Thoughts? Chris. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Looking for caveats to the following code
At 3:19 PM -0700 8/17/06, Chris W. Parker wrote: ?php if($result = do_something('hello')) { // do something with $result } else { // do some other stuff } ? The issue is whether or not this is a safe test. My initial thought is that it is safe since I'm simply checking for true/false-ness. I either check for '!== false' explicitly or (in the case of the latter example) check that something other than 'false' is returned. It's slightly less readable but it seems more efficient (if nothing more than to save on the number of lines typed). Thoughts? Chris: You can shorten it even further by: if(do_something('hello')) { // do something with $result } else { // do some other stuff } I don't think it's any different than calling any other function that returns whatever or false. I tried it out at -- http://xn--ovg.com/a14.php -- with both pass and fail. All worked the same in all three ways. hth's tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Looking for caveats to the following code
On Thu, 2006-08-17 at 18:58 -0400, tedd wrote: At 3:19 PM -0700 8/17/06, Chris W. Parker wrote: ?php if($result = do_something('hello')) { // do something with $result } else { // do some other stuff } ? The issue is whether or not this is a safe test. My initial thought is that it is safe since I'm simply checking for true/false-ness. I either check for '!== false' explicitly or (in the case of the latter example) check that something other than 'false' is returned. It's slightly less readable but it seems more efficient (if nothing more than to save on the number of lines typed). Thoughts? Chris: You can shorten it even further by: if(do_something('hello')) { // do something with $result I'm guessing you missed the content of the comment Tedd... he actually needs the $result :D Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Looking for caveats to the following code
Tedd, Actually, he seems to be needing the return value of the function inside the if block. So, he would need to do: if ($result = do_something('hello')) or, as Adam stated: if ( ($result = do_something('hello')) !== false ) Regards. -- Anas Mughal On 8/17/06, tedd [EMAIL PROTECTED] wrote: At 3:19 PM -0700 8/17/06, Chris W. Parker wrote: ?php if($result = do_something('hello')) { // do something with $result } else { // do some other stuff } ? The issue is whether or not this is a safe test. My initial thought is that it is safe since I'm simply checking for true/false-ness. I either check for '!== false' explicitly or (in the case of the latter example) check that something other than 'false' is returned. It's slightly less readable but it seems more efficient (if nothing more than to save on the number of lines typed). Thoughts? Chris: You can shorten it even further by: if(do_something('hello')) { // do something with $result } else { // do some other stuff } I don't think it's any different than calling any other function that returns whatever or false. I tried it out at -- http://xn--ovg.com/a14.php -- with both pass and fail. All worked the same in all three ways. hth's tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Anas Mughal
RE: [PHP] Looking for caveats to the following code
At 4:16 PM -0700 8/17/06, Chris W. Parker wrote: tedd mailto:[EMAIL PROTECTED] on Thursday, August 17, 2006 3:58 PM said: You can shorten it even further by: if(do_something('hello')) { // do something with $result } else { // do some other stuff } This actually won't work because $result is not being assigned anywhere. Ahhh yes , I didn't catch that. Sorry to all. http://xn--ovg.com/a14.php Your page should show the source, otherwise it's impossible to tell what's happening. I didn't think that necessary considering that I posted what I was doing. But, I posted it again in my example -- please review: http://xn--ovg.com/a14.php I see now why I got the same result as you -- $result was never cleared. Yeah, I guess as long as I'm just testing for true or false it'll be ok. The example isn't checking for true/false but rather not-null/false Cheers. tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php