I want to insert NULL in a table with the null coalescing operator ( > php 7) I always get and empty string from my POST variables and I did this :
$phone = !empty($this->$_POST['phone']) ?? "NULL";
But when I dump the result of $phone, it's always false...
Thank you
CodePudding user response:
Just use $phone = $this->$_POST['phone'] ?? "NULL";
You are getting false
because of empty()
returns boolean.
CodePudding user response:
Keep in mind that there is a difference between "NULL" and NULL in SQL (and almost everywhere else).
I don't think the null coalescing operator does what you want it to do. It only works if you're starting with a null value, not if you want to turn a falsy value into null.
You could do $phone = !empty($this->$_POST['phone']) ? $this->$_POST['phone'] : null;
, or probably just $phone = $this->$_POST['phone']) ?: null;