Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1949
  • Last Modified:

using php isset and having a 0 (zero) value??

To prevent PHP "Notice" errors i generally use ISSET to check my variables for values... I have a function that creates a select box that has values from -10 to 10. The problem is that in the function, i pass the "selected" value for the select box and if its set to 0 the ISSET seems to result in false and not properly select the 0 value item in my select box. Is there a way around this? The php documentation for isset doesnt state that ISSET will return false if the value is set to 0....

// build item array
$temp=array();
$temp[10]="10 (first)";
for ( $counter = 9; $counter >= -9; $counter -= 1) {
$temp[$counter]=$counter;
}
$temp[-10]="-10 (last)";

// display our select box
display_formfield(array(
  "name"=>"content_weight",
  "items"=>$temp,
  "label"=>"Weight",
  "span"=>"display order of our pages",
  ),"select");

// select box function...
function display_formfield($fieldarray,$type='text'){
 
  $output='';
  $outputlabel='';
  global $_POST;
 
  $outputlabel.="<label>";
  if(isset($fieldarray['label'])){$outputlabel.=$fieldarray['label'];};
  $outputlabel.=":<br /><span class=\"small\">";
  if(isset($fieldarray['span'])){$outputlabel.=$fieldarray['span'];};
  $outputlabel.="</span></label>";
 
  // get post data for our value if there is no value passed in our array
  if(!isset($fieldarray['value']) && isset($_POST[$fieldarray['name']]) ){
    $fieldarray['value']=stripslashes($_POST[$fieldarray['name']]);
  }
  // store defaults
  if(!isset($fieldarray['value']) ){ $fieldarray['value']=''; }
 
  switch ($type)
  {
   case 'select':
    $output.="<tr><td align=\"right\">".$outputlabel."</td><td valign=\"top\" >";
    $output.="<select name=\"".$fieldarray['name']."\">";
    foreach($fieldarray['items'] as $value => $text){
      $output.="<option ";
      if(isset($fieldarray['value']) && $value==$fieldarray['value'] && $value!=''){$output.="selected=\"selected\"";};
      $output.= " value=\"$value\">$text</option>";
    }
    $output.="</select>";
    $output.="</td></tr>";
    break;
  }  
  print  "<div class=\"forminput\">".$output."</div>";
  return;
}
0
cimmer
Asked:
cimmer
  • 2
2 Solutions
 
RurneCommented:
That's correct.  The two checking language constructs are empty() and isset().  empty() returns true if a variable is 0, null, the blank string, or FALSE; isset also returns true if a variable is 0, because it's not null.

That doesn't appear to be your problem.  The problem appears more in the logic behind your switch statement.

Based on the example you've given, there appears to be a problem with:

      if(isset($fieldarray['value']) && $value==$fieldarray['value'] && $value!=''){$output.="selected=\"selected\"";};

From what I see, $fieldarray['value'] is always the empty string, unless $_POST['content_weight'] is already set, so $value!='' is going to break this every time.

If you are setting $_POST['content_weight'], then confirm that the following lines are actually setting up $fieldarray['value'] = 0;

  if(!isset($fieldarray['value']) && isset($_POST[$fieldarray['name']]) ){
    $fieldarray['value']=stripslashes($_POST[$fieldarray['name']]);
  }
0
 
Beverley PortlockCommented:
Notice errors aren't really all that important. If they are showing on your browser then you need to change your php.ini for error_reporting to something like E_ALL ^ E_NOTICE

You should also be aware that isset returns FALSE which in PHP is zero. Zero and false often get confused which is why PHP has the === and !== operators in addition to == and != so that zero and FALSE are distinguisable

I suspect your problem is the line

if(isset($fieldarray['value']) && $value==$fieldarray['value'] && $value!=''){$output.="selected=\"selected\"";};
      $output.= " value=\"$value\">$text</option>";


Change the $value != '' to $value !== '' since, for PHP zero IS blank
0
 
RurneCommented:
0 is considered empty, and when typecast to boolean, it returns false.
However, a variable containing 0 returns true to isset(), because 0 !== NULL.

0 == '' because of how the implicit typecasting works in type comparison.  A complete breakdown can be found at http://us.php.net/manual/en/types.comparisons.php .  In essence, the semantics of empty() and the semantics of typecasting to boolean are the same.

That said, changing to a strict comparison to the $value !== '' check is an additional step to be made, since these both resolve to FALSE.  However, I would still recommend checking that your $_POST values are instantiated properly.
0
 
cimmerAuthor Commented:
about your comment....

...."From what I see, $fieldarray['value'] is always the empty string, unless $_POST['content_weight'] is already set, so $value!='' is going to break this every time."....


nice. that was it!!!...
 i changed it...
if(isset($fieldarray['value']) && $value==$fieldarray['value'] && $value!=''){$output.="selected=\"selected\"";};

to...
 if($value==$fieldarray['value']){$output.="selected=\"selected\"";};

works perfect now... it was picking up all the numbers before just not 0 after making that change it works fine now.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now