Solved

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

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exporting image to excel from php document 10 28
php extract($_REQUEST) 5 54
mysql date time 14 31
Which userID to use for shopping cart application 13 19
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

809 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