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

Viewed 1280

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]

3 Answers

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.

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>
Related