Skip to content

Instantly share code, notes, and snippets.

@anam-hossain
Last active September 10, 2018 00:40
Show Gist options
  • Save anam-hossain/4dc49fc3b1b1d931cedffdbeb3182b0d to your computer and use it in GitHub Desktop.
Save anam-hossain/4dc49fc3b1b1d931cedffdbeb3182b0d to your computer and use it in GitHub Desktop.
Passing by reference from Foreach loop
<?php
class test
{
public function addDescription(&$data)
{
$data['description'] = 'Hello World!';
}
public function printMessages()
{
$messages = [
['code' => 200],
['code' => 200],
['code' => 200],
['code' => 200],
];
foreach($messages as &$message) {
$this->addDescription($message);
}
var_dump($messages);
}
}
(new Test())->printMessages();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment