most reliable way to see if a variable contains a value.

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
waskelton4
Forum Contributor
Posts: 132
Joined: Mon Sep 09, 2002 6:42 pm

most reliable way to see if a variable contains a value.

Post by waskelton4 »

Hey group..

I'm re-working a large part of code in one of my applications and by doing so I'm seeing many different stages of my coding and they bring up a few questions.. one is this..

What is the best and most reliable way to check a variable to see if it exsits and has a value or not.

I've used many different ways and most of them seem to work fine but I wanted to get some opinions here..

here they are in my order of "bad" to "good". (

Code: Select all

if($_POST['fieldname'] == "") or if($_POST['fieldname'] == NULL)

if(!$_POST['fieldname']) or if($_POST['fieldname']) 

if(isset($_POST['fieldname']) or if(!isset($_POST['fieldname'])

if(!empty($_POST['fieldname']) or if(empty($_POST['fieldname'])
any reasons on why these are bad or good (i know a few but I'd like to get some more insight)

Many Thanks

Will
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

What do you mean by 'has a value or not'? Every set variable/array element ( isset($variable) === true ) has some value. Whether it meaningful to your application is another question...
waskelton4
Forum Contributor
Posts: 132
Joined: Mon Sep 09, 2002 6:42 pm

Post by waskelton4 »

for instance..

there is a form with a form element that is required in order to process the form.

i submit that form and on my page where i'm processing the form i check to see if anything was entered.
if the element was a text box.. the $_POST value will be empty.. but if the element were a checkbox and it were not checked then there wouldn't be a corresponding $_POST variable on the processing page.

I don't mean to restrict this to only $_POST. Would it apply any differently to any other variable like an unregistered session variable?
i guess what i'm really looking for is some discussion of the differences behind each of the methods as they might apply differently to various situations.

what each one actually does..

does that make more sense?

ws
User avatar
dbevfat
Forum Contributor
Posts: 126
Joined: Tue Jun 28, 2005 2:47 pm
Location: Ljubljana, Slovenia

Post by dbevfat »

depends on what exactly you're trying to check. I've heard empty is Bad(tm), since it evaluates to true even for empty arrays.

Personally, I use array_key_exists if I want to check that a variable actually is in the array, no matter the value. Then I use strlen() > 0 to determine if it has any meaningful value. But mostly, the strlen() > 0 check is enough. It tells you that the value in $_POST contains something else than

I would avoid direct bool casting, like this:

Code: Select all

if ($_POST['var']) ...
since this evaluates to false in all of these cases:
- the value is not in array,
- the value is null,
- the value is an empty string '',
- the value holds an integer zero 0,
- the value holds a string zero '0',

which may not be exactly what you want. Run this code, it will give you an idea and then you will easily decide. Although I do think strlen() is the best possible option ;).

Code: Select all

$keys = array('none', 'null', 'empty', 'zero', 'zerostr', 'emptyarray');
	$array = array('null' => null, 'empty' => '', 'zero' => 0, 'zerostr' => '0', 'emptyarray' => array());
	
	echo "<pre>";
	print_r($array);
	
	echo "\n* array_key_exists:<br />";
	foreach ($keys as $key)
		echo $key ." ... " . array_key_exists($key, $array) . "\n";
	
	echo "\n* isset:<br />";
	foreach ($keys as $key)
		echo $key ." ... " . isset($array[$key]) . "\n";
	
	echo "\n* bool cast:<br />";
	foreach ($keys as $key)
		echo $key ." ... " . (bool)($array[$key]) . "\n";
	
	echo "\n* strlen() > 0:<br />";
	foreach ($keys as $key)
		echo $key ." ... " . (strlen($array[$key]) > 0) . "\n";
	
	echo "\n* !empty():<br />";
	foreach ($keys as $key)
		echo $key ." ... " . (!empty($array[$key])) . "\n";
	echo "</pre>";
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: most reliable way to see if a variable contains a value.

Post by Christopher »

waskelton4 wrote:What is the best and most reliable way to check a variable to see if it exsits and has a value or not.
You answered you own question in the asking. None of your examples will do what you want because they are all only one kind of check. You need to do and isset() first (for vars that you have not initialized) and then, passing that check, see if if the value meets some criteria.

It is a common mistake to think that isset() and functions like empty() are equivalent -- they are not. isset() is only function that checks if a variable has been allocated. It is unique. All other checks are on the value in an allocated variable.
(#10850)
Post Reply