Cameron Ball
Cameron Ball

Reputation: 4128

Non lazy evaluation in while loop

Here's a snippit of code I am working with:

while(($response = $responses->fetchObject()) || ($mapping = $mappings->fetchObject())) {
    $mapping = $mapping ? array('data' => array('#type'=>'link','#title' =>$mapping->title, '#href' => 'node/' . $mapping->nid)) : '';        
    $response = $response ? array('data' => array('#type'=>'link','#title' =>$response->title, '#href' => 'node/' . $response->nid)) : '';  
}

Because PHP does short circuit evaluation, $mapping doesn't get assigned anything if $response does.

How can I write this so that both $response and $mapping are assigned something in the while loop?

Upvotes: 0

Views: 84

Answers (1)

Cameron Ball
Cameron Ball

Reputation: 4128

This is my solution right now:

while(true) {
    $response = $responses->fetchObject();
    $mapping = $mappings->fetchObject();

    if(!$response && !$mapping) {
        break;
    }
}

But I feel there must be a nicer way?

Upvotes: 2

Related Questions