Jump to content
Larry Ullman's Book Forums

Recommended Posts

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 post
Share on other sites

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 post
Share on other sites

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 post
Share on other sites

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 post
Share on other sites

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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...