0

sorry this seems to be a repeated question but it confused to why I can't get a drop-down box to differentiate between null and 0.

I have a drop-down box with some PHP which select a value from a database and assigns it to $user. The default value is null, but I then want to change it to a 0 or 2 (its for number of tickets, so null is I haven't assigned any, 0 is I don't want to give them any, and 2 is they get 2). Basically the code below doesn't distinguish between null and 0 and based on other posts I read I feel it should but obviously I'm missing something.

<option value="" <?php if( is_null($user)){ echo 'selected'; }?>>Not Set</option>
<option value="0" <?php if($user === 0){ echo 'selected'; }?>>0</option>
<option value="2" <?php if($user === 2){ echo 'selected'; }?>>2</option>

I have also tried

<option value="" <?php if($user === NULL){ echo 'selected'; }?>>Not Set</option>
<option value="0" <?php if($user === 0){ echo 'selected'; }?>>0</option>
<option value="2" <?php if($user === 2){ echo 'selected'; }?>>2</option>

and

<option value="" <?php if($user === ''){ echo 'selected'; }?>>Not Set</option>
<option value="0" <?php if($user === 0){ echo 'selected'; }?>>0</option>
<option value="2" <?php if($user === 2){ echo 'selected'; }?>>2</option>

Any help appreciated.

EDIT

var_dump($user) = string(1) "0"

This is the same where $user = NULL and $user = 0

on my local xampp var_dump($user)=int(0) and var_dump($user)=NULL the same code is acting different on the live version.

snookian
  • 795
  • 6
  • 12
  • 32
  • Can you post a `var_dump($user)` please, just so we know what we are dealing with – RiggsFolly Feb 23 '18 at 09:31
  • As a general rule, `NULL` is used to denote a value that is not known, not available (yet) or not applicable. It is definitely different than `0`; `0` is a known value that denotes the absence of whatever items it counts. Using `0` instead of `NULL` is a logic error. – axiac Feb 23 '18 at 09:32
  • `$user` is a string `"0"` there for comparing with `0` using `===` will not work. `===` compares type as well as value. **They are of different types** – RiggsFolly Feb 23 '18 at 09:35
  • Then check your schema – RiggsFolly Feb 23 '18 at 10:02
  • Im not sure what you mean by schema, shall i open a new question regarding my live server returning as string while local returns and int? – snookian Feb 23 '18 at 10:21

0 Answers0