Value= $varable else null

Hello,
I have a site that I am working on, and it has a field that when loaded up from fresh returns a 0, how can I make this field be completely blank or displays its variable (from a previous value). it currently has
value="<?php print esc_html($var);?>">

So I’m trying to get it to…

  1. display var, if var is empty display nothing (null). Should be easy ?

Regards

Well, are you asking to display all NON-NULL values? OR, non-empty strings or non-zero strings?
Here is how it is done:

if (!IS_NULL($var)) { echo $var; } // If NOT IS_NULL
if (!empty($var)) { echo $var; } // If NOT EMPTY
if (trim($var)!="") { echo $var; } // If string is empty ( Not the same as NOT EMPTY )

**** Note, NULL is not the same as EMPTY and also, if a string contains one space, it is neither. ****
Also, you can combine them depending on where your strings are coming from…
If (!IS_NULL($var) AND trim($var)!="") { echo $var; }

Thank you ErnieAlex, I will try to apply this, this evening and report back, you reply is very appreciated.

Thanks Ernie, I have been ill so have not had the chance to apply your fix until today, works like a treat.

You are very welcome! Glad you solved it. Always nice to solve a programming puzzle!

Sponsor our Newsletter | Privacy Policy | Terms of Service