AlexBeau Posted July 22, 2011 Share Posted July 22, 2011 Still diligently working my way through this book and I have come to a roadblock at the end of chapter 6. The question I am having trouble with states: Without knowing anything about $var will the following conditional be TRUE or FALSE? Why? if ($var = 'donut') { I am apt to say that it will be false because we don't know if $var has been assigned the value donut yet within the program but I am not sure. If someone could answer this for me and give me an explanation so I can fully grasp this concept I would be thankful. Thank you, Alex Link to comment Share on other sites More sharing options...
Larry Posted July 22, 2011 Share Posted July 22, 2011 Thanks for the question. The answer is that conditional will be TRUE, because the conditional uses the assignment operator, not the comparison operator. So in the conditional, $var is being assigned a new value. I posed this question as it's a common mistake and bug. Link to comment Share on other sites More sharing options...
AlexBeau Posted July 22, 2011 Author Share Posted July 22, 2011 Ok thank you. I guess I was getting caught up on the fact that you can assign a value to a variable within the if statement. Also after speaking to a few other people I learned that the variable assignment occurs before the expression is evaluated. Now that I know that I would say that because the variable $var is first assigned the value donut and then is evaluated it is TRUE. This is because it is not an empty string, and empty value or a value of 0. Am I correct in my thinking? I just want to make sure I fully understand everything before I get in to deep to not know what I am doing. Thank you for your help and your book. It has been a great introduction so far. Alex Link to comment Share on other sites More sharing options...
Antonio Conte Posted July 25, 2011 Share Posted July 25, 2011 When comparing a variable to info, you should always use double equals. ( == ) // I'm a string $something = "Compare me"; if ($something == "Compare me") { echo "Yes, we're alike"; } else { echo "I'm different"; } // I'm Integer $oceans = 11; if ($oceans == 11) { echo "Occeans Eleven rocked!"; } elseif ($oceans == 12) { echo "I was not as good"; } else { echo "Crap..."; } // BOOLEAN $sure == TRUE; // if ($sure) is really enough. Just for the example if ($sure == TRUE) { echo "Of course I'm sure!"; } else { echo "Ok. I might've been wrong"; } Link to comment Share on other sites More sharing options...
Larry Posted July 25, 2011 Share Posted July 25, 2011 Ok thank you. I guess I was getting caught up on the fact that you can assign a value to a variable within the if statement. Also after speaking to a few other people I learned that the variable assignment occurs before the expression is evaluated. Now that I know that I would say that because the variable $var is first assigned the value donut and then is evaluated it is TRUE. This is because it is not an empty string, and empty value or a value of 0. Am I correct in my thinking? I just want to make sure I fully understand everything before I get in to deep to not know what I am doing. Thank you for your help and your book. It has been a great introduction so far. Alex Thanks for the nice words on the book. Much appreciated. Yes, the variable assignment will occur first and you're correct in thinking that if $var is assigned an empty string or 0, the conditional will be FALSE. More importantly, it will still be a bug, which is the point I'm trying to make by that particular prompt. Link to comment Share on other sites More sharing options...
Recommended Posts