Adding 2 Variables for Total

I'm trying to show you why it's wrong so that you can avoid it in the future; run the script, see what the difference is and learn why :?
 
Well I appreciate that but am not much of a coder I just learn as I go using the code vs. writing it. Hope you can understand.. if I was a code wizard I would not be the one asking here.
 
And you also missed the point of what I was trying to code. It was NOT about equal it was about if value = A then output B. If value is NOT = to A then output C.
 
= is assignment:
Code:
$a = 6;
# Assign the value 6 to the variable $a
== is to test for equality:
Code:
if ($a == 6)...
# Test $a to see if it evaluates to 6

Code:
if ($a = $b) {
  # assign $a with the value of $b and return true
  # this will always execute
} else {
  # this will never execute
}
Is an assignment which returns true; in your script, the if statements always return true and potentially corrupts the value of the variables you think you are testing.

If you had looked at and run the code I offered, you would have seen that.

You will struggle more by trying not to learn the basics than if you do learn the basics. Copy and paste will only get you so far.
 
Well we learn something new everyday :oops: Following a weird thought I had, I now stand corrected on this:
hyper said:
Code:
if ($a = $b) {
  # assign $a with the value of $b and return true
  # this will always execute
} else {
  # this will never execute
}

If $b evaluates to false, then that will be how the if statement acts, so:
Code:
# assign $a with the value of $b.
if ($a = $b) { # evaluate $b as boolean
  # this will execute if $b evaluates to true
} else {
  # this will execute if $b evaluates to false
}
Still not recommended since it could also corrupt the value of variables used and can be very unclear as to the intention, the following is clearer:
Code:
if ($b){...
 
Back
Top