php custom function to check if values are blank

x7yiwoj4  于 2022-10-22  发布在  PHP
关注(0)|答案(5)|浏览(112)

I would like to simplify this PHP process that checks if a value is blank.
If the value is blank, the variable gets set to null. If not, the variable is set to the value.
In this case, I have successfully posted a jquery object (called criteria) to PHP for processing. I then set the variables to the values in the object. I use IF/ELSE statements to check if the object value is blank.

<?php
  if(isset($_POST['criteria']))
  {
    $value = $_POST['criteria'];

    if($value['recentsq'] == ""){$recentsq = null;}else{$recentsq = $value['recentsq'];}  
    if($value['custname'] == ""){$custname = null;}else{$custname = $value['custname'];} 
    if($value['address'] == ""){$address = null;}else{$address = $value['address'];} 
  }
?>

I have quite a few of these values I need to check and set.
My question is how can I simplify this process by using a custom function?

xlpyo6sf

xlpyo6sf1#

You can simply create a method like:

function customfunction($valueString){
    return ($valueString != '' ? $valueString : null);
}

in above method, i am using ternary operator.
In your example, you can just call this method like:

$recentsq = customfunction($value['recentsq']);
$custname = customfunction($value['custname']);
$address = customfunction($value['address']);

Additional Point for future visitors:

According to PHP manual, If you are using PHP 7:
The null coalescing operator (??) has been added as syntactic sugar for the common case of needing to use a ternary in conjunction with isset(). It returns its first operand if it exists and is not NULL; otherwise it returns its second operand.

($valueString ?? null); // will use in method body

equal to:

($valueString != '' ? $valueString : null);
nxowjjhe

nxowjjhe2#

Or you can use directly the Null coalescing operator from PHP 7

if(isset($_POST['criteria']))
  {
    $value = $_POST['criteria'];

    $recentsq = $value['recentsq'] ?? null;
    $custname = $value['custname'] ?? null;
    $address = $value['address'] ?? null;
  }
x6h2sr28

x6h2sr283#

Alternatively, you can use empty function:

<?php

    $recentsq = empty($_POST['criteria']['recentsq']) ? null : $_POST['criteria']['recentsq'];
    $custname= empty($_POST['criteria']['custname']) ? null : $_POST['criteria']['custname'];
    $address= empty($_POST['criteria']['address']) ? null : $_POST['criteria']['address'];

?>

Then you don't even need to have separate isset checks.

k2fxgqgv

k2fxgqgv4#

Can use the following function get value from array and set a default initial value, by default null

function arrayValue($array, $key, $default = null)
{
   return (array_key_exists($key, $array) && $array[$key] !== '') ? $array[$key] :     $default;
}

Usage:

$values = [
        'name' => 'David',
        'age' => 18,
        'sex' => '',
    ];
    $name = arrayValue($values, 'name');
    $age = arrayValue($values, 'age');
    $sex = arrayValue($values, 'sex', 'unknown');
    $country = arrayValue($values, 'country', 'unknown');

    print_r(
        [
            $name,
            $age,
            $sex,
            $country,
        ]
    );

Output:

Array
(
    [0] => David
    [1] => 18
    [2] => unknown
    [3] => unknown
)

Like @christophe say since php7 can do something like

$name = $values['name'] ?? null;
$country = $values['country'] ?? 'unknown';
sg3maiej

sg3maiej5#

php function to check if input is blank. its collected from laravel blank function

function is_blank($value) {
    if (is_null($value)) {
        return true;
    }

    if (is_string($value)) {
        return trim($value) === '';
    }

    if (is_numeric($value) || is_bool($value)) {
        return false;
    }

    if ($value instanceof Countable) {
        return count($value) === 0;
    }

    return empty($value);
}

// tests
$tests = [
    '', // true
    null, // true
    0, // false
    '0', // false
    '  ', // true
    [], // true
    1, // false
    'test' // false
];

foreach ($tests as $value) {
    echo json_encode($value) . ' is blank: ' . json_encode(is_blank($value)) . '<br>';
}

相关问题