Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2008-06-26
4
Medium Priority
?
1,709 Views
Last Modified: 2013-12-12
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
Comment
Question by:cimmer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 9

Expert Comment

by:Rurne
ID: 21877998
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
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 512 total points
ID: 21878129
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
 
LVL 9

Assisted Solution

by:Rurne
Rurne earned 512 total points
ID: 21878513
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
 

Author Comment

by:cimmer
ID: 21878696
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question