0

変数を設定し、ループ後にその値をチェックするこのパターンを記述するより良い方法はありますか?

<?php

$isValid = false;
foreach ($posts as $post) {

    // If post ID matches the request ID
    if($post->id == $id) {

        $isValid = true;
        break;
    }
}

if(!$isValid) {

    // Not valid!
    header('Location: ...');
}

もっといい書き方がありそうです。

4

2 に答える 2

-1

短くするためにこのようなことをすることもできますが、それだけです...

<?php
foreach ($posts as $post) {
    // If post ID matches the request ID
    if($post->id != $id) {
        header("Location: ...");
        exit;
    }
}
于 2013-05-29T19:01:43.593 に答える