Saturday February 11, 2012 @ 10:02:13 GMT+10    ( Weather:  n/a )
Home » Weblog Archives

Powered byD's Bloggie
Weblog » Archive » 2006 » June » Day 09 » PHP isset()
Browse by specific entry ...
 PHP, MySQL, UnicodeUnicode Testing 

PHP isset() - 7:40 pm
Bug , Coding , PHP  -  poster 

In PHP, sometimes you'll need to do alot of variable assignment like
[ Hide ]
[ Highlight ] [ Text ]
<?php
// simple
$bar = isset($foo) ? $foo : '';
// complicated
$bar = isset($foo[$test['index']]) ? $foo[$test['index']] : '';
?>


So I came up with this function to ease my life...
[ Hide ]
[ Highlight ] [ Text ]
<?php
// Use this to standardize things..
function isset_empty(&$input)
{
	return isset($input) ? $input : '';
}
?>


Things works fine at first. But later then I discovered that it's not working like what I expected when I pass a value inside an array to that function. See below:
[ Hide ]
[ Highlight ] [ Text ]
<?php
$bar = isset_empty($foo);  // $foo is undefined at this point
echo $bar;
$foo = array(1 => 'World', 2 => 'Cup');
print_r($foo);
$bar = isset_empty($foo[10]);
print_r($foo);
 
// The outpust result
//        <-- emptry string, expected
// Array ( [1] => World [2] => Cup )
// Array ( [1] => World [2] => Cup [10] => )
 
?>


It seems that index 10 with empty string is inserted to the $foo array after calling that function. It doesn't harm in some situation but it's a real disaster if you're using array to populate items or doing a count($foo) to check the array size. The results are affected !

So I have to replace all the statements that uses the function and go back to the old way

$bar = isset($foo[$test['index']]) ?$foo[$test['index']] : '';

Doh! glare
 PHP, MySQL, UnicodeUnicode Testing 

Reader comments ( Add your comments )

- No comments (yet) -

Leave a public comment

Info Note: Neither BBCode nor HTML tags are supported currently.
Why ? Just to make my life easier and keep things simple.
Others: Comments that are longer than 1000 characters will be truncated.

    ) Info

Info

Info


Before you submit... verification

Please enter verification codes as seen in the image above to post your comment.
If you can't read the word, click here
(Your comment will be manually approved by admin before it's displayed to public)

$ view_blog.php 2009.09.17 18:16:41 $
Lost? | XML/HTML sitemap | Contact
38.107.179.243 , 22 queries , 0.3066s
Gzip enabled , CSS compressed , JS compressed
Copyright © 2005-2011 Darren's Outpost