1

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?

1
  • now you have few solutions , check them Commented Jun 14, 2019 at 15:52

5 Answers 5

4

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);
Sign up to request clarification or add additional context in comments.

13 Comments

@JohnBeasley: sure :)
I am getting a 500 error which I was able to isolate to the function call: $recentsq = customfunction($value['recentsq']); Any thoughts?
@JohnBeasley: check php error reporting, or dont know are u using framework
@Dharman: this is some additional info, if OP using php 7 then he can use, its body of function customfunction($valueString){
I am using 5.6.21
|
2

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;
  }

Comments

1

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.

Comments

1

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';

Comments

0

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>';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.