Solved

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

Posted on 2008-06-26
4
1,559 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

771 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

10 Experts available now in Live!

Get 1:1 Help Now