Categories > TinyButStrong general >

What is the conditional I can use for if a variable is null, and if it isn't?

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: desbest
Date: 2010-08-14
Time: 20:44

What is the conditional I can use for if a variable is null, and if it isn't?

What is the conditional I can use for if a variable is null, and if it isn't?
I looked on the manual under operators and by searching for null and I couldn't find it.
By: Skrol29
Date: 2010-08-15
Time: 22:38

Re: What is the conditional I can use for if a variable is null, and if it isn't?

Hi Desbest,

At the template side, you can only deals with string values. A null value in the PHP side will be converted into empty string ''.
So a conditional could be "if [val]=''; then ..."
By: desbest
Date: 2011-03-31
Time: 01:33

Re: What is the conditional I can use for if a variable is null, and if it isn't?

I'm replying to this thread because something's not working. I think I've found a bug in TinyButStrong 3.7.0 for php5.
The bug is that when I'm using a conditional to check whether a mysql variable is null, the block fails to show if the condition is true.

[onshow;block=span;when [var.null]='']
$null = "";
The above code works! :)


[onshow;block=span;when [items.description]='']
The above code doesn't work, even though the variable is null. [items.description] is pulled from a mysql database. :(
By: Skrol29
Date: 2011-04-01
Time: 01:20

Re: What is the conditional I can use for if a variable is null, and if it isn't?

Hi Desbest,

The behavior is known, and it is because of PHP: a unset item in an array is the same thing as an item with a null value. It's the same for variables.
See the discussion here:
http://www.tinybutstrong.com/forum.php?thr=2686#m11494
By: desbest
Date: 2011-04-02
Time: 00:30

Re: What is the conditional I can use for if a variable is null, and if it isn't?

It's good for lazy coders who use
if ($variable == ""){
instead of
if isset($variable){
but bad for templating engines.

But I am not trying to pull an unset item. I'm trying to pull a variable from a mysql database. The variable I would believe isn't unset, as it's supposed to have the value of null content.
It works for defined variables, but NOT defined mysql variables.
By: Skrol29
Date: 2011-04-02
Time: 22:50

Re: What is the conditional I can use for if a variable is null, and if it isn't?

Some MySQL connectors have an option for NULL values that may turn them into false or empty string.
But if you know that your field can have a null value, then you just have to add the TBS parameter "noerr".
By: desbest
Date: 2011-04-21
Time: 17:38

Re: What is the conditional I can use for if a variable is null, and if it isn't?

If you have the same problem as me, you can use a workaround.
Get rid of your conditional to show a block whether something is
""
or not, and instead use the
ifempty
parameter on that variable instead.

The manual prevails the winner again. http://www.tinybutstrong.com/manual.php#html_field_prm