Help us understand the problem. What is going on with this article?

知らなかったけど「isset($a) && isset($b)」は「isset($a, $b)」と書けるらしい。コード量が減るのでいいかも。

More than 1 year has passed since last update.

PHPでisset($a) && isset($b)isset($a, $b)とも書ける。コード量が減るのでいいかも。

$a = 1;
$b = 2;

isset($a, $b); // true
isset($a) && isset($b); // true

isset($a, $hoge); // false
isset($a) && isset($hoge); // false

なお、普通にマニュアルに書いてある: PHP: isset - Manual

ベンチマーク的には

  • 値が定義されている場合(つまりtrueを返す場合)はisset(a,b)を使ったほうが早い
  • 値が定義されていない場合(つまりfalseが返る場合)は&&を使った方が早い
    • でも調べる個数が増えると&&は遅くなる

という結果が出たが、些細な違いな気がする。

<?php

function bench(int $repeat, callable... $snippets)
{
    foreach ($snippets as $no => $snippet) {
        $start = microtime(true);
        for ($i = 0; $i < $repeat; $i++) {
            $snippet();
        }
        printf("snippet #%d: %0.8f秒\n", $no + 1, microtime(true) - $start);
    }
}

$data = ['x' => 1, 'y' => 2];

bench(
    10000000,
    // 0.76908994秒
    function () use ($data) {
        isset($data['x'], $data['y']);
    },
    // 0.83662701秒
    function () use ($data) {
        isset($data['x']) && isset($data['y']); 
    },

    // 0.68813109秒
    function () use ($data) {
        isset($data['none1'], $data['none2']);
    },
    // 0.54158902秒
    function () use ($data) {
        isset($data['none1']) && isset($data['none2']);
    },

    // 0.67208385秒
    function () use ($data) {
        isset($data['x'], $data['none']);
    },
    // 0.66804123秒
    function () use ($data) {
        isset($data['x']) && isset($data['none']);
    },

    // 0.76296902秒
    function () {
        isset(
            $data['a'],
            $data['b'],
            $data['c'],
            $data['d'],
            $data['e'],
            $data['f'],
            $data['g'],
            $data['h'],
            $data['i'],
            $data['j']
        );
    },
    // 0.81688905秒
    function () {
        isset($data['a']) &&
        isset($data['b']) &&
        isset($data['c']) &&
        isset($data['d']) &&
        isset($data['e']) &&
        isset($data['f']) &&
        isset($data['g']) &&
        isset($data['h']) &&
        isset($data['i']) &&
        isset($data['j']);
    }
);

結果

snippet #1: 0.76908994秒
snippet #2: 0.83662701秒
snippet #3: 0.68813109秒
snippet #4: 0.54158902秒
snippet #5: 0.67208385秒
snippet #6: 0.66804123秒
snippet #7: 0.76296902秒
snippet #8: 0.81688905秒
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした