# PHP Conditional Statement

**URL:** <https://discourse.softpress.com/t/php-conditional-statement/21894>\
**Category:** Dynamo\
**Created:** [March 11, 2015, 3:48am UTC](https://discourse.softpress.com/t/php-conditional-statement/21894 "2015-03-11T03:48:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Todd1](https://discourse.softpress.com/letter_avatar_proxy/v4/letter/t/838e76/32.png) [@Todd1](https://discourse.softpress.com/u/Todd1)\
**Post date:** [March 11, 2015, 3:48am UTC](https://discourse.softpress.com/t/php-conditional-statement/21894/1 "2015-03-11T03:48:55Z")

</div>

I’m using several placeholders [[+placeholder\_name]] which are stored as a JSON object. Each one is optional so it may have a value (string) or not (empty). If it does it’s rendered on the front-end as a link.

I’m using a conditional statement to check the placeholder for a value and if empty the front-end field should be left blank, i.e. no link text.

```auto
[[!+twitter:isempty =``:else=`<a href="[[+twitter]]” title="[[+twitter]]">Twitter</a>`]]

```

The problem is that even when the value is empty the link text (e.g. ‘Twitter’) is still rendered which, as far as I can tell, should not happen with the above example. Clearly I’m wrong, though.

Todd  
[https://xiiro.com](https://xiiro.com)

* * *

dynamo mailing list  
email@hidden  
Update your subscriptions at:  
[http://freewaytalk.net/person/options](http://freewaytalk.net/person/options)

---

<div class="post-metadata">

**Author:** ![The\_Big\_Erns](https://discourse.softpress.com/letter_avatar_proxy/v4/letter/t/3ec8ea/32.png) [@The\_Big\_Erns](https://discourse.softpress.com/u/The_Big_Erns)\
**Post date:** [March 11, 2015, 9:22am UTC](https://discourse.softpress.com/t/php-conditional-statement/21894/2 "2015-03-11T09:22:57Z")

</div>

I do not see an IF, so how is this conditional?

* * *

dynamo mailing list  
email@hidden  
Update your subscriptions at:  
[http://freewaytalk.net/person/options](http://freewaytalk.net/person/options)

---

<div class="post-metadata">

**Author:** ![waltd](https://discourse.softpress.com/user_avatar/discourse.softpress.com/waltd/32/2720_2.png) [@waltd](https://discourse.softpress.com/u/waltd)\
**Post date:** [March 11, 2015, 1:41pm UTC](https://discourse.softpress.com/t/php-conditional-statement/21894/3 "2015-03-11T13:41:35Z")

</div>

Are you allowed to use straight PHP in these snippets, or must you encapsulate your code in these pidgin `:isempty` type statements?

Perhaps the simplest way to do this is to create a function that you can call yourself, so you can use more straightforward code there, and not in your view, which should be “dumb”. Think of this like a Rails helper method.

```
<?php
function twitter_link($twitter){
  if(empty($twitter)) return '';
  return '<a href="' . $twitter . '" title="' . $twitter . '">Twitter</a>';
}
?>
[[+twitter_link([[+twitter]])]]

```

Does that make sense?

Walter

* * *

dynamo mailing list  
email@hidden  
Update your subscriptions at:  
[http://freewaytalk.net/person/options](http://freewaytalk.net/person/options)

---

<div class="post-metadata">

**Author:** ![Todd1](https://discourse.softpress.com/letter_avatar_proxy/v4/letter/t/838e76/32.png) [@Todd1](https://discourse.softpress.com/u/Todd1)\
**Post date:** [March 11, 2015, 4:25pm UTC](https://discourse.softpress.com/t/php-conditional-statement/21894/4 "2015-03-11T16:25:02Z")

</div>

Of course I got it sorted 5 minutes after posting last night.

Walter, thank you for the suggestion. The code I posted was by the author’s admission a guess, and not ideal. Straight PHP can and does work just fine except yours didn’t quite do so, though I’m sure it would.

In any case, I’m using a very handy “if” snippet written in what appears(?) to be straight PHP [If - If | MODX Documentation](http://rtfm.modx.com/extras/revo/if), that allows me to write conditionals using a more familiar (albeit regional) syntax.

Here’s the result:

```auto
[[!If?
   &subject=`[[!+twitter]]`
   &operator=`empty`
   &then=``
   &else=`<a href="[[!+twitter]]" title="[[!+twitter]]">Twitter</a>`
]]

```

Alternatively, this one does the same as above but also passes the value (if any) to a filter that returns a list of resources (a tag filter):

```auto
[[!If?
	&subject=`[[!+specialties]]`
	&operator=`empty`
	&then=``
	&else=`Specialties:
		<span>
			[[!toLinks?
				&items=`[[+specialties]]`
				&target=`22`
				&tpl=`tagResult`
				&tagKey=`tag`
			]]</span>`
]]

```

@Ernie - I believe the “isempty" part is essentially the “if” you’re looking for.

Todd  
[https://xiiro.com](https://xiiro.com)

> On Mar 11, 2015, at 8:41 AM, waltd [email@hidden](mailto:email@hidden) wrote:
> 
> Are you allowed to use straight PHP in these snippets, or must you encapsulate your code in these pidgin `:isempty` type statements?
> 
> Perhaps the simplest way to do this is to create a function that you can call yourself, so you can use more straightforward code there, and not in your view, which should be “dumb”. Think of this like a Rails helper method.
> 
> \<?php function twitter\_link($twitter){ if(empty($twitter)) return ''; return 'Twitter'; } ?\>
> 
> [[+twitter\_link([[+twitter]])]]
> 
> Does that make sense?
> 
> Walter
> 
> * * *
> 
> dynamo mailing list  
> email@hidden  
> Update your subscriptions at:  
> [http://freewaytalk.net/person/options](http://freewaytalk.net/person/options)

* * *

dynamo mailing list  
email@hidden  
Update your subscriptions at:  
[http://freewaytalk.net/person/options](http://freewaytalk.net/person/options)
