php - comparing a string, and, a string with only numbers -


i new php , have been trying no success comparison working:

i have variable 'productcode' might hold

  • case 1: value of 'choose'.
  • case 2: later variable can take string (with numbers in it) 102,103

i need know in variable productcode? still 'choose' or '102' (like string numbers)

what comparison operator should use?

is having value 'choose'? should

  • true if productcode set 'choose' ,
  • false if other string (with numbers in 102,103..)

i tried

if(strcmp($productcode,'choose')){  }//tried double quotes  if(($productcode=='choose')){  }//tried double quotes  if(($productcode==='choose')){  }//tried double quotes 

i stumbled @ this.. '102' compared 'choose' passing if statement...

can please me correct method of checking mixed case..

edit 1: related code (after incorporating === 0 suggesion rizier123 :

 $productcode11=$_post['productcode11'];  $productcode12=$_post['productcode12'];  $productcode13=$_post['productcode13'];  if(strcmp($productcode11,'choose') === 0){     //testing values if hold expecting     echo '<script type="text/javascript">alert(" '.$productcode11.' ");</script>';     echo '<script type="text/javascript">alert(" '.$productcode12.' ");</script>';         //testing if 'if' check passes     echo '<script type="text/javascript">alert("same");</script>';         $productcode11error="fill this";         $incomplete=true;     }  if(strcmp($productcode12,'choose') === 0)){         $productcode12error="fill this";         $incomplete=true;     }  if(strcmp($productcode13,'choose') === 0){         $productcode13error="fill this";         $incomplete=true;     } 

edit 2 did tests , these strange results got:

$test = strcmp($productcode11, 'choose'); $test2 = strcmp($productcode11, $productcode12);  if both 'choose' 1 , incremented 1 every submission (2,3,4,5...) if both numbers , same, 0 if lhs number , rhs 'choose', -1 if lhs , rhs 'choose' 1,3,4.. if lhs 'choose' , rhs number 1 if both numbers, , lhs > rhs 1 if both numbers , rhs > lhs -3 

your first attempt works should. have know, strcmp() return 0, when strings equal, add comparison, e.g.

if(strcmp($productcode, 'choose') === 0) {                                 //^^^^^ see here  } 

so evaluate true if $productcode equal "choose".

sidenote:


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -