Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
Can't check a variable value. How to?
Hi folks ;)
I’m trying to check my variable value if it is false
but I fail:
if ( null == variable(array('name' => 'full_display')) ) header('Location: '.hu.'404');
Could you correct me?
Thanks by advance.
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: Can't check a variable value. How to?
Is that a textpattern variable? If so you can access it with
<txp:php>
global $variable;
// output variable
echo $variable['full_display'];
</txp:php>
Then, instead of the echo statement, use whatever testing method you need for your situation depending on whether the variable exists, is empty, or contains “0” or “false” as a word, e.g. if (empty(…)
, if (isset(…)
or testing for false, 0 or string.
TXP Builders – finely-crafted code, design and txp
Offline
Re: Can't check a variable value. How to?
Pat64 wrote #297781:
if ( null == variable(array('name' => 'full_display')) )...
Don’t think you can compare it to null. From memory, if it’s used at all on your page, the best you can hope for is to test for false:
isset()
only checks if it’s never been used at all. Not much use.empty()
will check if it’s false or 0.- Any other value compares to that value. Use
===
for strict comparison if there’s a chance it may return 0 or false.
jakob’s method is probably better, or you could use !if_variable(array(name => 'full_display') { ... }
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: Can't check a variable value. How to?
Hi Stef & Jakob ;)
Thanks lot for your help.
I know it wasn’t a good chance to compare variable
value against null
but that’s what Textpattern told me when I tried isset
:
Fatal error: Cannot use isset() on the result of a function call (you can use "null !== func()" instead) in /home/xxx/d/website/www/dev/textpattern/publish/taghandlers.php(3836) : eval()'d code on line 14
It seems obvious that variable
tag didn’t store boolean values but string one.
So this works fine for me:
if ( 'false' === variable(array('name' => 'full_display')) ) header('Location: '.hu.'404');
Kind regards,
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Pages: 1