Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable returning "empty" even when not? 2

Status
Not open for further replies.

Katerine

Programmer
Mar 9, 2001
234
0
0
US
I'm pretty new to PHP (specifically), so I apologize if I'm missing something obvious. At least this should be a pretty quick question. :)

Ok, so I have the following test code set up:
PHP:
	$secure = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443;
	$secure = (empty($secure)) ? false : $secure;
	echo ($secure ? 'True' : 'False'), empty($secure) ? 'Empty' : '';

I'm running it on my development server, which is a Windows machine running IIS 7 with PHP 5.6, on localhost, obviously not over HTTPS. Under those circumstances, the value of $secure should be false. But for some reason, the echo code above consistently returns, "FalseEmpty" - even though I specifically checked for empty and reset it to false. Does false somehow = empty in PHP? If not (and that would be very strange), how do I get it to correctly set to false based on the server settings?

Many thanks!

Katie
 
Hi

Katie said:
Does false somehow = empty in PHP?
Yes. As the documentation says :
PHP Documentation about empty() said:
Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.

The following things are considered to be empty:
[ul]
[li]"" (an empty string)[/li]
[li]0 (0 as an integer)[/li]
[li]0.0 (0 as a float)[/li]
[li]"0" (0 as a string)[/li]
[li]NULL[/li]
[li]FALSE[/li]
[li]array() (an empty array)[/li]
[li]$var; (a variable declared, but without a value)[/li]
[/ul]
( PHP Manual | Function Reference | Variable and Type Related Extensions | Variable handling | Variable handling Functions | empty | Return Values )

Maybe you are expecting too much from [tt]empty()[/tt]. Of course, this is matter of personal coding style, but I find it abit overused in your code. For example in the 1[sup]st[/sup] line I would prefer [tt]isset()[/tt] instead of [tt]! empty()[/tt]. And the 2[sup]nd[/sup] line is completely pointless, as it only transforms true or false to the same true or false.

Katie said:
how do I get it to correctly set to false
Your code already sets it correctly to false. The [tt]var_dump()[/tt] function outputs type information too, so is helpful when in doubt regarding a value's type.
Code:
Interactive mode enabled

[blue]php >[/blue] $_SERVER = [ 'SERVER_PORT' => 80 ];
[blue]php >[/blue] var_dump($secure = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443);
bool(false)

[blue]php >[/blue] $_SERVER = [ 'SERVER_PORT' => 443 ];
[blue]php >[/blue] var_dump($secure = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443);
bool(true)

[blue]php >[/blue] $_SERVER = [ 'HTTPS' => 'on', 'SERVER_PORT' => 80 ];
[blue]php >[/blue] var_dump($secure = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443);
bool(true)

[blue]php >[/blue] $_SERVER = [ 'HTTPS' => 'off', 'SERVER_PORT' => 80 ];
[blue]php >[/blue] var_dump($secure = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443);
bool(false)


Feherke.
feherke.ga
 
Thanks, guys. :) The isset() tip is particularly useful!

Ok, quick follow-up question:

I had actually looked at the manual before posting, but focused more on the stuff later in the article, Particularly when it said:
php manual said:
Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.

That was the part I read. I'm coming at this from an SQL and VB background... but I'm used to False = 0. Hence the confusion. Does false not have a numeric equivalent in PHP?

Katie
 
Hi

Katie said:
Does false not have a numeric equivalent in PHP?
Not sure to what exactly are you thinking here, but 0 and 0.0 ( even "0" also ) evaluates to false in boolean context :
Code:
[blue]php >[/blue] var_dump((int) true, (int) false, (string) true, (string) false); 
int(1)
int(0)
string(1) "1"
string(0) ""

[blue]php >[/blue] var_dump((bool) 1, (bool) 0, (bool) '1', (bool) '');
bool(true)
bool(false)
bool(true)
bool(false)

You can see more on the documentation's PHP type comparison tables page.


Feherke.
feherke.ga
 
Ok, thanks. It may just be a problem with the verbiage in that one sentence in the manual - if it truly "Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE," - then a boolean set to FALSE (i.e. 0) should return TRUE, because the var exists, and it's not non-empty, and it's not non-zero (sorry about the multiple negatives, but that's how they wrote it). :) So, just a bit of confusion surrounding the verbiage.

[Edit] ...which is what it did. It did return TRUE. So... Oops. :-D Sorry to bug you guys! I'm going with the excuse that I worked for 10 hours yesterday and am just starting to learn PHP! :)

Katie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top