Solved

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

Posted on 2008-06-26
4
1,565 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
  • 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 128 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 128 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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.

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now