Jump to content

Recommended Posts

In this code...

 

if ($preview==999){
	echo '<div id="boxPreview">
		<h3>Preview</h3>
		<p>' . (isset($comments) ? nl2br(str2htmlentities($comments)) : "") . '</p>
		</div>';
}

 

 

If $preview has a value of "1", then why would my If-Then evaluate to TRUE and my echo code fire???  :o

 

 

Debbie

 

 

Link to comment
https://forums.phpfreaks.com/topic/264349-why-is-this-evalutaing-to-true/
Share on other sites

Because it obviously doesn't equal 1, it equals 999. That's the only reason.

 

Huh??????

 

This statement is FALSE...

 

1==999

 

And so in this code, my echo should never fire...

 

if ($preview==999){
	echo '<div id="boxPreview">
		<h3>Preview</h3>
		<p>' . (isset($comments) ? nl2br(str2htmlentities($comments)) : "") . '</p>
		</div>';
}

 

 

Debbie

 

Again, the only reason that condition would evaluate to TRUE is if $preview is 999. There is no other reason.

 

As I said in my OP, $preview=1...

 

(Specifically, I set $preview=TRUE, and so it evaluates to "1" as verified by me and NetBeans...)

 

If I have literals in there it fails...

if (1==999){

 

 

But this code...

if ($preview==999){

 

...takes the THEN branch meaning the interpreter sees it as TRUE...

 

 

 

Debbie

 

Because it obviously doesn't equal 1, it equals 999. That's the only reason.

 

Again, the only reason that condition would evaluate to TRUE is if $preview is 999. There is no other reason.

Because it obviously doesn't equal 1, it equals 999. That's the only reason.

 

How does 1==999 equal 999 ??????????????????

 

 

Again, the only reason that condition would evaluate to TRUE is if $preview is 999. There is no other reason.

 

Jerking my chain?? 

 

 

Debbie

 

 

Jerking your chain? No. I'm telling you, based on the code you posted yourself, that the only way that condition will evaluate TRUE is if $preview is 999, NOT 1, as you so adamantly claim it to be.

If your comparison is actually if(TRUE==999), rather than the previously stated if (1==999), then yes, TRUE is == to 999.

 

How is TRUE == 999 ???????

 

 

This code results in FALSE...

 

if (1==999){
	echo 'TRUE';
}else{
	echo 'FALSE';
}

 

 

In *my* code, I have...

 

}elseif (isset($_POST['preview'])){
	$preview = TRUE;

}//End of PROCESS FORM

 

 

And I type in $preview==999 last night - and forgot about it - to force a FALSE condition which was what happened last night in this code...

 

<!-- PREVIEW COMMENT -->
<?php
	if ($preview==999){
		echo '<div id="boxPreview">
			<h3>Preview</h3>
			<p>' . (isset($comments) ? nl2br(str2htmlentities($comments)) : "") . '</p>
		</div>';
	}
?>

 

 

Today when I step through my code in NetBeans, if I click the "Preview" button, $preview=TRUE; and I would expect my echo to not fire because "1" - which is what NetBeans shows in the $preview variable - does NOT equal "999"...

 

 

Debbie

 

 

Jerking your chain? No. I'm telling you, based on the code you posted yourself, that the only way that condition will evaluate TRUE is if $preview is 999, NOT 1, as you so adamantly claim it to be.

 

I am seeing it happen with my own two eyes in NetBeans as I step through my code...

 

NetBeans says $preview is "1" in the variables tab, and that would mean I have 1 == 999 for $preview == 999 and that SHOULD BE FALSE, but my If-Then is taking the Then path meaning that 1 == 999 is evaluating to TRUE?!

 

(Like I said above, that was testing code that should have been removed last night, but I still would like to know what is going own for my own sanity!!)

 

 

Debbie

 

 

NetBeans is simply showing you the integer value of true, which is 1.  It's not actually the number 1 in this case, since you set $preview to logical true.  It's just the numerical representation of true.  Why is true represented as a 1?  Think binary.  1 == on/true, 0 == off/false.  In most modern programming languages, any non-zero value that can be converted to an integer will be true.  So, True == 999 will always result in true.

 

FWIW, this is boolean logic 101.  One of those things you should learn if you're going to use if/else statements.

Not sure if this applies to this.

From http://php.net/manual/en/language.types.boolean.php

Warning

 

-1 is considered TRUE, like any other non-zero (whether negative or positive) number!

When converting to boolean, the following values are considered FALSE:

 

    the boolean FALSE itself

    the integer 0 (zero)

    the float 0.0 (zero)

    the empty string, and the string "0"

    an array with zero elements

    an object with zero member variables (PHP 4 only)

    the special type NULL (including unset variables)

    SimpleXML objects created from empty tags

 

Every other value is considered TRUE (including any resource).

NetBeans is simply showing you the integer value of true, which is 1.  It's not actually the number 1 in this case, since you set $preview to logical true.  It's just the numerical representation of true.  Why is true represented as a 1?  Think binary.  1 == on/true, 0 == off/false.

 

I get that.

 

 

In most modern programming languages, any non-zero value that can be converted to an integer will be true.

 

I get that.

 

For example, this would evaluate to TRUE...

 

if (999){
	echo 'This line will be shown';
}else{
	echo 'This line will not be shown';
}

 

 

So, True == 999 will always result in true.

 

Above you said "1" and "TRUE" were the same.

 

And I get that most If-Then's treat any Positive Number as TRUE.

 

But I still do not see how "TRUE" can be logically equal to "999" ?!  (This I can see...  TRUE==1)

 

 

FWIW, this is boolean logic 101.  One of those things you should learn if you're going to use if/else statements.

 

I've never encountered this situation before, so not so "101"...

 

 

Debbie

 

I really don't give a red rat's ass what NetBeans says. Why don't you show us where you assign the value to $preview?

 

Get you Old-Man Glasses out and maybe you'll see that I posted my code in Reply #10...    ;)

 

 

Debbie

 

 

So then if you knew you assigned the boolean value of TRUE to the $preview variable, why did you continue to insist that you had given it the integer value of 1? Alzheimer's disease or just plain ignorance?

In most modern programming languages, any non-zero value that can be converted to an integer will be true.

 

But I still do not see how "TRUE" can be logically equal to "999" ?!  (This I can see...  TRUE==1)

 

True is logically equal to all non-zero values. 

 

23.987.  999.  "Hello". 

 

All of those will be equal to true.

I really don't give a red rat's ass what NetBeans says. Why don't you show us where you assign the value to $preview?

 

Get you Old-Man Glasses out and maybe you'll see that I posted my code in Reply #10...    ;)

 

 

Debbie

 

 

So then if you knew you assigned the boolean value of TRUE to the $preview variable, why did you continue to insist that you had given it the integer value of 1?

 

I said NetBeans showed "1" and you guys are saying "TRUE" is the same as "1"

 

 

Alzheimer's disease or just plain ignorance?

 

Nah, I just like to give you stuff to bitch about...

 

Freebie from me for the curmudgeon...

 

 

Debbie

 

 

In most modern programming languages, any non-zero value that can be converted to an integer will be true.

 

But I still do not see how "TRUE" can be logically equal to "999" ?!  (This I can see...  TRUE==1)

 

True is logically equal to all non-zero values. 

 

23.987.  999.  "Hello". 

 

All of those will be equal to true.

 

I knew that was so in an IF condition, but I did not know that if you put any non-zero value in a logical comparison that it would evaluate to TRUE.

 

Interesting.

 

Thanks for the info (and being civil) KevinM1...  8)

 

 

Debbie

 

I really don't give a red rat's ass what NetBeans says. Why don't you show us where you assign the value to $preview?

 

Get you Old-Man Glasses out and maybe you'll see that I posted my code in Reply #10...    ;)

 

 

Debbie

 

 

So then if you knew you assigned the boolean value of TRUE to the $preview variable, why did you continue to insist that you had given it the integer value of 1?

 

I said NetBeans showed "1" and you guys are saying "TRUE" is the same as "1"

 

 

Alzheimer's disease or just plain ignorance?

 

Nah, I just like to give you stuff to bitch about...

 

Freebie from me for the curmudgeon...

 

 

Debbie

 

 

 

So ignorance then. Got it.

I knew that was so in an IF condition, but I did not know that if you put any non-zero value in a logical comparison that it would evaluate to TRUE.

 

It has to do with how PHP decides to interpret the types for the comparison.  Since your testing a boolean==integer, php decides to cast the integer to a boolean and evaluate the condition as boolean==boolean.

 

so:

$preview == 999 becomes TRUE == (boolean)999 which becomes TRUE == TRUE.

 

You can force php to interpret something a specific way by doing your own casting, such as:

if ((int)$preview == 999):

 

which would force php to cast $preview to an int (1) then evaluate 1 == 999, which would be false.

 

 

Think about it like this, behind the scenes php sees that you're using a boolean value and comparing it to an integer, so it casts the integer value to a boolean value for you, if you used the strict (typed) comparison operator, it would evaluate to false:

 

 

var_dump( true === 999 ); //are true and 999 equal in both type and value? (false)


var_dump( true == 999 ); //are true and 999 equal in value (true) //why?


var_dump((bool)999); // true


var_dump(true == true); // true

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.