Forum Moderators: coopster

Message Too Old, No Replies

Wierd true/false situation

         

Readie

1:02 pm on Mar 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, I have some code:

$current_email = 0;
$email_quota = 5000;
if($email_percent = round(($current_email/$email_quota)*100,0)) {
$email_percent = "Unlimited";
}
echo $email_percent;

Which *SHOULD* always return true, as I'm setting a variable as the if statement condition.

However, for some reason, it's always returning false, and only returns true if I do

if(!$email_percent = ...

Could someone explain why this is happening. I have the actual finish working nice, but this is a bit wierd to me :/

jatar_k

1:44 pm on Mar 5, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



how about I ask a question in return

why on earth would you do that? You can't predict the results, it won't really return true or false unless the answer is 1 or 0 but even then that condition would not actually be a true or false.

So why do an assignment in an if that can't be predicted?

Matthew1980

1:57 pm on Mar 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Readie,


$current_email = "0";
$email_quota = "5000";


Im no expert at the way as numbers are represented, but I am pretty sure that you have to have decimal numbers between quotes, else they default so something else, I read something on it somewhere on here a while ago, and from what I can recall it's the way that the parser interprets the non quoted number.

Again, I may well be wrong, but I'm just recalling a previous post from somewhere.

Cheers,
MRb

jatar_k

2:00 pm on Mar 5, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



no, if you put them between quotes then they are strings and then you are relying on php to correctly type switch before doing math, which isn't a good situation though most of the time it works out

Matthew1980

2:13 pm on Mar 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there jatar_k,

Ok, as I said I was probably wrong. Thanks for clearing that up :)

MRb

eelixduppy

2:24 pm on Mar 5, 2010 (gmt 0)



To do assignments in the if condition like that you must wrap it all with parenthesis like so:

if(($email_percent = round(($current_email/$email_quota)*100,0))) {

StoutFiles

2:28 pm on Mar 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if(!$email_percent = ...


A single = will always return true in a if statement.

== Is it equal to?
= It is now equal.

jatar_k

2:31 pm on Mar 5, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



but really if it will always be true then do this

$current_email = 0;
$email_quota = 5000;
$email_percent = "Unlimited";
echo $email_percent;

since you are overwriting the var anyway the math is useless

eelixduppy

2:52 pm on Mar 5, 2010 (gmt 0)



>> A single = will always return true in a if statement.

Wrapping it in the quotes like I have done does not check what that assignment's value is, but rather the value of $email_percent after the assignment has taken place.

Readie

3:47 pm on Mar 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the interest guys.

>> jatar_k
I must admit that it isn't actually my code, rather, someone came to me with a problem, and my solution works, but seems backwards to me :)

>> eelixduppy
I have tried phrasing it in that manner, it still returns false

>> Stoutfiles
I concur, a single = should always return true, that was my basis for posting here :) Because this one is not.

eelixduppy

3:57 pm on Mar 5, 2010 (gmt 0)



If you have tried it the way I told you to do it then the only way it will return false is if $email_percent is ZERO. Consider the following example and you will see the behavior of this syntax:
$test = 0; if(($var = $test)) echo "true on 0\n"; # $test = 1; if(($var = $test)) echo "true on 1\n"; # $test = -1; if(($var = $test)) echo "true on -1\n";

This outputs:

true on 1
true on -1


Notice how it is not true for the 0 case.

Readie

4:19 pm on Mar 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I copied and pasted yours just to make sure I hadn't messed up.

<?php

$current_email = 0;
$email_quota = 5000;
if(($email_percent = round(($current_email/$email_quota)*100,0))) {
$email_percent = "Unlimited";
}
echo $email_percent;

// 0 divided by -.- I should look at things in more detail. :/

?>

Outputs "0"

I think, based on your previous post though, that the final output of
round(($current_email/$email_quota)*100,0)
is equal to zero - which is subsequently being interpreted as false.

Thanks again :)

StoutFiles

4:40 pm on Mar 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Notice how it is not true for the 0 case.


Right, you're setting $var to the value 0, which also means false. if($var = 0) == if(false)

if(($email_percent = round(($current_email/$email_quota)*100,0)))
{$email_percent = "Unlimited";}
echo $email_percent;


if(($email_percent = round(($current_email/$email_quota)*100,0))) //sets $email_percent to 0, which also means false

{$email_percent = "Unlimited";} //doesn't run, because the if statement was false

echo $email_percent; //returns 0