matthewhaworth Posted August 16, 2008 Share Posted August 16, 2008 Okay, look at the following code: #1 <?php if ( $link = mysql_connect($host, $user, $pass) ) { echo "The connection was successful"; } #2 <?php $link = mysql_connect($host, $user, $pass); if ( $link = FALSE) { echo "The connection was successful"; } Which one should I use? When assigning variables within if statements does it return TRUE is the assignment was successful or does it return TRUE is the content of the variables is TRUE? Quote Link to comment https://forums.phpfreaks.com/topic/119922-general-question-about-if-statements-thats-too-difficult-to-describe-in-subject/ Share on other sites More sharing options...
redarrow Posted August 16, 2008 Share Posted August 16, 2008 they both work but ill sugest example 2....... Quote Link to comment https://forums.phpfreaks.com/topic/119922-general-question-about-if-statements-thats-too-difficult-to-describe-in-subject/#findComment-617778 Share on other sites More sharing options...
awpti Posted August 16, 2008 Share Posted August 16, 2008 Uh.. <?php $link = mysql_connect($host, $user, $pass); if($link === FALSE): // Connection failed. endif; Your second example is checking if the assignment of FALSE to $link worked Quote Link to comment https://forums.phpfreaks.com/topic/119922-general-question-about-if-statements-thats-too-difficult-to-describe-in-subject/#findComment-617779 Share on other sites More sharing options...
matthewhaworth Posted August 16, 2008 Author Share Posted August 16, 2008 Of course! I'm half asleep, in the second example I did mean to use the comparison operator, not the assignment operator.. which makes a mokery of my whole question really.. but any more answers will be appreciated as I still don't feel satisfied. Thanks so far. Quote Link to comment https://forums.phpfreaks.com/topic/119922-general-question-about-if-statements-thats-too-difficult-to-describe-in-subject/#findComment-617780 Share on other sites More sharing options...
natbob Posted August 16, 2008 Share Posted August 16, 2008 I would use #1 just because it uses less code. I ran a speed test and #2 takes 1.5 times as long (with the === operator of course ) so from a performance perspective, 1 is faster. Quote Link to comment https://forums.phpfreaks.com/topic/119922-general-question-about-if-statements-thats-too-difficult-to-describe-in-subject/#findComment-617782 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.