PHP Short Circuit Rating (good / bad?)

This is a general question about childbirth, but I will use a concrete example to explain it.

I have a function that loads a document. If this document does not exist, it will create it; if it exists, it will convert it to a JSON array. I always want this function to return an array of some type, regardless of whether there is a problem with json_decode()or if the file does not exist. I am currently doing it like this ...

function load($file) {
    if( ! file_exists($file)) {
        $handle = fopen($file, 'w');
        fclose($handle);
    }

    $raw = file_get_contents($file);
    $contents = json_decode($raw, TRUE);

    return( ! $contents ? array() : $contents);
    //cant use ternary shorthand "?:" in PHP 5.2, otherwise this would be shorter
}

Now, there is nothing wrong with the code above (at least I don't think it is, and it works fine). However, I am always looking for ways to improve my code and condensate it, while keeping it completely legible. And this expression of return always bothered me because of how ineffective it seems. So today I thought, and something happened to me. I remember watching mysql tutorials that did something with the effect connect() or die();, so I thought, why not json_decode() or array();? Will this work? So I rewrote my function to find out ...

function load($file) {
    if( ! file_exists($file)) {
        $handle = fopen($file, 'w');
        fclose($handle);
    }

    $raw = file_get_contents($file);
    return json_decode($raw, TRUE) or array();
}

, . , . ? , - ? ? , , , , . . .

Wikipedia

, , , , MySQL. , or die(), , . , , or die(). ? , , , . , PHP C, . PHP? , , ?

.

- " ( ) [ . , - C. (, )"

?

. , -, , , , , . , - . , , . .

+3
3

, , .

: C , . PHP , . , --, , .

? , , , C. , PHP C, .

PHP, , . , , , . 2012 , or .

, $a or $b . , if.

if (a() || b())

, b() , a() true.

:

return a() or b();

.

, , , , (, , , ):

function load($file) {
    if (!file_exists($file)) {
        touch($file);
        return array();
    }

    $raw = file_get_contents($file);

    $contents = json_decode($raw, true);

    if (is_array($contents)) {
        return $contents;
    } else {
        return array();
    }

}

, , :

function load($file) {

    $raw = file_get_contents($file);

    if ($raw !== false) {
        $contents = json_decode($raw, true);
        if ($contents !== null) {
            return $contents;
        }
    }

    return array();

}

, . , , , . , , , .

: 1--, :

function load($file) {

    $contents = array();

    $raw = file_get_contents($file);

    if ($raw !== false) {
        $contents = json_decode($raw, true);
        if ($contents === null) {
            $contents = array();
        }
    }

    return $contents;

}
+2

, , , , . - , , .

, , , , ,

+1

, .

:

isset($value) or $value = 0;

if (!isset($value)) {
  $value = 0;
}

, .

, , , :

return $data[$key] or $data[1];

1 , .

:

// Make sure $key is valid.
$data[$key] or $key = 1;

return $data[$key];

, PHP , $ $data.

0

All Articles