Set NULL if empty

Hi,

How can set the value to NULL if it was empty because I want to save it as NULL in the database.

I tried:

$user = !empty($user) ? $user : "NULL";

but value is getting set to 0

Kindly help…

Thanks,
Jassim

That is a string containing the 4 characters NULL.

2 Likes

Additionally, always code to truth when you can. Switch your parameters around like so.

$user = empty($user) ? null : $user;

1 Like

Totally agree, far more readable at a glance and intuitive.

I have adopted the following style:

if (true) : 
  // Ok, do nothing
else: 
  // do something
endif; 

Above is good for trying alternative methods to find the best or fastest, etc.

I frequently use the following while debugging:

if(0): 
  // Wrap working script that can be toggled
else:
  // try new script
endif;

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.