×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Variable returning "empty" even when not?
2

Variable returning "empty" even when not?

Variable returning "empty" even when not?

(OP)
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. smile

Ok, so I have the following test code set up:

CODE --> 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

RE: Variable returning "empty" even when not?

From http://php.net/manual/en/function.empty.php

Quote:

A variable is considered empty if it does not exist or if its value equals FALSE.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

RE: Variable returning "empty" even when not?

Hi

Quote (Katie)

Does false somehow = empty in PHP?
Yes. As the documentation says :

Quote (PHP Documentation about empty())

Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.

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

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

Quote (Katie)

how do I get it to correctly set to false
Your code already sets it correctly to false. The var_dump() function outputs type information too, so is helpful when in doubt regarding a value's type.

CODE --> php -a

Interactive mode enabled

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

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

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

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

Feherke.
feherke.ga

RE: Variable returning "empty" even when not?

(OP)
Thanks, guys. smile 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:

Quote (php manual)

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

RE: Variable returning "empty" even when not?

Hi

Quote (Katie)

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 --> php -a

php > var_dump((int) true, (int) false, (string) true, (string) false); 
int(1)
int(0)
string(1) "1"
string(0) ""

php > 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

RE: Variable returning "empty" even when not?

(OP)
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). smile So, just a bit of confusion surrounding the verbiage.

[Edit] ...which is what it did. It did return TRUE. So... Oops. big smile 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! smile

Katie

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close