most reliable way to see if a variable contains a value.
Posted: Fri Feb 03, 2006 8:42 am
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". (
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
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'])Many Thanks
Will