Sandeepan Nath
Sandeepan Nath

Reputation: 10294

How to check if variable is empty in phing build xml?

I referred the documentation and tried this -

<php function="empty" returnProperty="productionSameAsExpectedBranch">
    <param value="${productionDeviationFromExpectedBranch}"/>
</php>

But it gives error -

[php] Calling PHP function: empty() [PHP Error] call_user_func_array() expects parameter 1 to be a valid callback, function 'empty' not found or invalid function name [line 125 of /usr/share/pear/phing/tasks/system/PhpEvalTask.php]

Upvotes: 3

Views: 1423

Answers (3)

Siad Ardroumli
Siad Ardroumli

Reputation: 596

Another solution would be to use the matches condition with an empty line pattern like:

<target name="empty-string">

    <property name="productionSameAsExpectedBranch" value=""/>

    <if>
        <matches string="${productionSameAsExpectedBranch}" pattern="^$"/>
        <then>
            <echo>Property is string empty, do something!</echo>
        </then>
    </if>

</target>

Upvotes: 0

jawira
jawira

Reputation: 4598

I would use isfalse condition to check if a string is empty.

<target name="empty-string">

    <property name="productionSameAsExpectedBranch" value=""/>

    <if>
        <isfalse value="${productionSameAsExpectedBranch}"/>
        <then>
            <echo>Property is string empty, do something!</echo>
        </then>
    </if>

</target>

Note I set productionSameAsExpectedBranch to empty string first, I do this to avoid non-existing property. The property will not be overwritten if already exists.

This works because internally isfalse uses PHP's casting.

Upvotes: 1

Daniel W.
Daniel W.

Reputation: 32350

I'm using a construct like this:

<if>
  <or>
    <not><isset property="productionSameAsExpectedBranch" /></not>
    <and>
      <isset property="productionSameAsExpectedBranch" />
      <equals arg1="${productionSameAsExpectedBranch}" arg2="" />
    </and>
  </or>
  <then>
    <!-- it is not set or empty -->
    <!--property name="productionSameAsExpectedBranch" value="something" override="true" /-->
  </then>
<else />
</if>

Upvotes: 2

Related Questions