Null

This filter will change the given input to be NULL if it meets specific criteria. This is often necessary when you work with databases and want to have a NULL value instead of a boolean or any other type.

Supported options for Zend\Filter\Null

The following options are supported for Zend\Filter\Null:

  • type: The variable type which should be supported.

Default behaviour for Zend\Filter\Null

Per default this filter works like PHP‘s empty() method; in other words, if empty() returns a boolean TRUE, then a NULL value will be returned.

1
2
3
4
$filter = new Zend\Filter\Null();
$value  = '';
$result = $filter->filter($value);
// returns null instead of the empty string

This means that without providing any configuration, Zend\Filter\Null will accept all input types and return NULL in the same cases as empty().

Any other value will be returned as is, without any changes.

Changing behaviour for Zend\Filter\Null

Sometimes it’s not enough to filter based on empty(). Therefor Zend\Filter\Null allows you to configure which type will be converted and which not.

The following types can be handled:

  • boolean: Converts a boolean FALSE value to NULL.
  • integer: Converts an integer 0 value to NULL.
  • empty_array: Converts an empty array to NULL.
  • float: Converts an float 0.0 value to NULL.
  • string: Converts an empty string ‘’ to NULL.
  • zero: Converts a string containing the single character zero (‘0’) to NULL.
  • all: Converts all above types to NULL. (This is the default behavior.)

There are several ways to select which of the above types are filtered. You can give one or multiple types and add them, you can give an array, you can use constants, or you can give a textual string. See the following examples:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
// converts false to null
$filter = new Zend\Filter\Null(Zend\Filter\Null::BOOLEAN);

// converts false and 0 to null
$filter = new Zend\Filter\Null(
    Zend\Filter\Null::BOOLEAN + Zend\Filter\Null::INTEGER
);

// converts false and 0 to null
$filter = new Zend\Filter\Null( array(
    Zend\Filter\Null::BOOLEAN,
    Zend\Filter\Null::INTEGER
));

// converts false and 0 to null
$filter = new Zend\Filter\Null(array(
    'boolean',
    'integer',
));

You can also give a Traversable or an array to set the wished types. To set types afterwards use setType().

Project Versions

Table Of Contents

This Page

Note: You need to stay logged into your GitHub account to contribute to the documentation.

Edit this document

Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Login with your GitHub account.
  2. Go to Null on GitHub.
  3. Edit file contents using GitHub's text editor in your web browser
  4. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  5. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  6. Your changes are now queued for review under project's Pull requests tab on GitHub.