Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2016-02-07 08:57:04

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,599
GitHub Twitter

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

#2 2016-02-07 18:08:26

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,595
Website

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

#3 2016-02-07 20:15:36

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,270
Website GitHub

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

#4 2016-02-08 08:12:09

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,599
GitHub Twitter

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

Board footer

Powered by FluxBB