0

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?

0

1 Answer 1

2

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?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.