Home>

When you expand an argument, it doesn't always have that value, right?

At that time, I found the ternary operator useful and adopted it, but I would like to know what to do when there is no value.

If i have code conventions, you should follow them, but there is no such thing for beginners in personal development, so I would like to know how to do it.

I think that there are many points that beginners cannot reach, but thanks for your cooperation.

Below, we receive args with a function called hoge.

In case of undefined or empty string, I used the ternary operator below to expand these values, and set `` when there is no value.

However, since offset can be 0, it cannot be judged by empty like name and order_by, and only offset has become complicated as follows.

Is it strange to write like this? I would appreciate any advice on what to do.

Corresponding source code
<? php
$args = [
    'name' =>'smith',
    'order_by' =>'created_at',
    'offset' =>1,
];;
function hoges ($args) {
    // Deployment
    $name =! empty ($args ['name'])? $args ['name']:'';
    $order_by =! empty ($args ['order_by'])? $args ['order_by']:'';
    $offset = (! empty ($args ['offset']) || (isset ($args ['offset'])&&$args ['offset'] == 0))? (int) $args ['offset' ']:'';
    // Use the expanded value to perform various processes from the following
}
What I tried

I thought it would be refreshing if I made a function dedicated to judgment that can use any of the above three, but I could not adopt it because an error of "Undefined variable: v" would occur when it was undefined.

// Returns true if undefined, null, empty string
function my_is_empty ($v) {
    $is = false;
    if (! isset ($v)) {
        $is = true;
    }
    if (is_null ($v)) {
        $is = true;
    }
    if ($v ==='') {
        $is = true;
    }
    return $is;
}
// When executed, an "Undefined variable: v" error occurs when it is undefined.
$v ='';// ok
$v = null;// ok
$v;// does not work
var_dump (my_is_empty ($v));
Supplementary information (FW/tool version, etc.)

I am using PHP var7.3.1 due to the circumstances of the rental server, but I am thinking of changing the server if the update makes it easier.

php
  • Answer # 1

    However, offset can be 0, so it cannot be judged by empty like name and order_by.

    issetOrarray_key_existsLet's use.

  • Answer # 2

    This depends on the requirements and the design.
    What to do when false and how to handle the information at that time
    It will be decided by design and requirements.
    Sometimes it's better not to be a ternary operator.

    I'm using PHP var7.3.1, but if the update makes it easier

    I don't think there is anything in particular so far.

  • Answer # 3

    It seems that the cause is that the function below goes to if ($v ==='') even if it is undefined.
    It looks like you can just return at that point without assigning it to $is.

    In this case
    Why not try using the Null coalescing operator?

    $offset = $args ['offset'] ?? "";


    If it is empty, an empty string will be assigned, and if it is 0, 0 will be assigned.

  • Answer # 4

    As for the direct answer, I think that it can be done with the existing one, so from a slightly different point of view,

    Generally, when inserting data, required items and data are formatted in the form of validation. At that time, for example, it is necessary to think about how to manage non-essential items. Whether it is NULL or something is specified as the default.

    By doing so, the processing during deployment can be reduced.

  • Answer # 5

    Added in PHP 7.0??This is probably the function that the Null coalescing operator is looking for.
    https://www.php.net/manual/ja/migration70.new-features.php

    $name = $args ['name'] ??'';


    If you want to force it, it seems better to use individual arguments instead of array.

    function hoges ($name, $order_by ='created_at', $offset = 1)