Welcome, Guest Login

Support Center

XPHP logic

Last Updated: Apr 17, 2017 07:29AM PDT

XPHP is an ever-growing library of scripting statements that you can use for powerful template control, widget behaviors, and more in LiveWhale.

In the below statements, you can use any of the XPHP variables from the XPHP Dictionary, or item-specific variables in widget results and details pages (i.e., "profiles_45" might be one of your custom profile fields).


Logic Operations

If/Then Example

The following examples demonstrate first, a simple check (is variable "X" empty?) and a more complicated one, using some of the tricks from the conditional functions below.

<xphp content="true">
   <if var="X" value=""/>
        //do something here
<xphp content="true">
   <if var="server_path" value="{group_directory}index.php"/>
        <!-- show this if it's the group homepage -->

Whatever is in-between <content></content> will be shown if the if statement is true. It could be simple HTML, a widget, or more XPHP code.

If/Else Example

Below is an example using If/Else in xphp to display a default image in your profile details page when the person does not have a photo:

<xphp content="true">
 <if var="profiles_image"/>
  <xphp var="profiles_image"/>
  <else content="true">
 <img src="/path/to/default_image.png"/>

In this example, we see we can use <xphp var="foo"/> to display an XPHP variable directly.

AND & OR Operators

By default, XPHP if statements are treated as ANDs when two or more variables must meet the conditions set, but you can switch to an OR relationship with the addition of only one attribute on the surrounding <xphp> tag.

<!-- the default AND state -->
<xphp content="true">
  <if var="a"/>
  <if var="b"/>
    <p>A and B are both non-empty.</p>
<!-- with the addition of the ifmode="or"
attribute, the comparison becomes an OR -->
<xphp content="true" ifmode="or">
  <if var="a"/>
  <if var="b"/>
 <p>Either A or B is non-empty.</p>


Conditional Functions

You can alter the <if> statements above, mixing and matching from the following library, to execute all sorts of logical tests:

Description Code
Does variable foo exist and have a non-empty value? <if var="foo"/>
Does variable foo not exist or have an empty value? <if var="foo value=""/>
Does variable foo equal "bar"? <if var="foo" value="bar"/>
Or: <if var="foo" equals="bar"/>
Does variable foo not equal "bar"? <if var="foo" not_equals="bar"/>
Does variable foo equal variable bar? <if var="foo" value="{bar}"/>
Or: <if var="foo" equals="{bar}"/>
Does variable foo (a comma-separated list) contain value "bar"? <if var="foo" contains="bar"/>
Does variable foo contain the text "bar" anywhere in it? <if var="foo" matches="bar"/>
Does variable foo not contain the text "bar" anywhere in it?     <if var="foo" not_matches="bar"/>
Does the tag foo exist on this page/item? <if var="has_tag_foo"/>
Is variable foo (a number) less than 100?
For dates, can use "today", "yesterday", etc as values.
<if var="foo" less_than="100"/>
Or: <if var="foo" before="yesterday"/>
Is variable foo (a number) greater than 100?
For dates, can use "today", "yesterday", etc as values.
<if var="foo" greater_than="100"/>
Or: <if var="foo" after="today"/>
Is variable foo (a number) in between 5 and 7 (inclusive)? <if var="foo" range="5 to 7"/>
Does variable foo match the regular expression
<if var="foo" regexp="^This.+?test"/>
Does variable foo not match the regular expression 
<if var="foo" not_regexp="^This.+?test"/>


Using other variables (POST, GET) in your if statements

By default, the above tests will check for your variables in the XPHP Dictionary (also known as $_GLOBALS in PHP). You can use the type attribute to check variables from server, get, or post.

Variable Code
From URL string: ?foo=bar <if var="foo" type="get" ...
From form (POST) to page <input name="foo"> <if var="foo" type="post" ...
From the PHP server variables <if var="foo" type="server" ...


Checking for elements on the page

You can also use XPHP to check for certain elements in the source code of the current page, using the has_element function. Your check can match any valid single CSS selector in the following forms:

<if has_element="span"/>
<if has_element="#aaa"/>
<if has_element=".bbb"/>
<if has_element="span#aaa"/>
<if has_element="span.bbb"/>
<if has_element="span#aaa.bbb"/>
<if has_element="#aaa.bbb"/>
<if has_element="#aaa.bbb.ccc"/>

This is especially useful if, for example, you want a section of your page to display only if <div id="sidebar"> exists (<if has_element="div#sidebar"/>).

Note: The element must exist in the page before the <if> statement in question is reached. This may mean, if you have other page changes being output from another XPHP statement, it could not be caught by this logical test.
seconds ago
a minute ago
minutes ago
an hour ago
hours ago
a day ago
days ago
Invalid characters found