0

This problem is simpler to explain with the code. Note that functions are located in a different file and are being included on the page.

$announcements = $dbc->query($q)->results(); //Returns array of objects
$announcements = prepareMessage($announcements); //Adds html tags to values
$latest = $announcements[0]; //Assign first index to variable
$announcements = truncate($announcements); //Truncate announcement text

function prepareMessage($message) {
    foreach($message as $values => $key) {
        $key->title = '<h2>'.$key->title.'</h2>';
        $key->name = '<p>'.$key->name.'</p>';
        $key->date = '<small>'.humanDate($key->date).'</small>';
    }

    return $message;
}

function truncate($message) {
    foreach($message as $values => $key) {
        $key->announcement = substr($key->announcement, 0, 50) . '...</p>';
    }

    return $message;
}

The value of the $latest variable changes after I run this line of code

$announcements = truncate($announcements);

$latest value before that line is ran

[announcement] =>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua.

$latest value after that line is ran

[announcement] =>
Lorem ipsum dolor sit amet, consectetur adipisc...

So my question is, why is the value of $latest changing?

1
  • Nothing in this code seems to be messing with $latest... perhaps something in the code that outputs $latest to the screen? Commented Apr 17, 2015 at 18:55

3 Answers 3

4

Because $latest is a reference to $announcements[0], not the actual value of $announcements[0]. Instead, use a clone. In PHP5, object assignments are always by reference.

$latest = clone $announcements[0]; //Clone first object in array
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I was not aware that it was only a reference. I will take note of that for future reference.
2

Maybe this example will help you understand why $latest is changing:

As stated in your code $announcements is an array of objects, simplified it's like this:

$test = array(new StdClass(array('a'=>1)));
$latest = $test[0];
$test[0]->a = 3;
var_dump($latest);

The var_dump will show you $latest also changed. This is because actually $test[0] is only a reference to the instance of the StdClass, and that instance is copied to $latest. When the instance changes, both references to the instance will show you the same values, from the same instance.

Comments

0

you are cutting the value of $latest to 50 characters with the truncate function in your code.

To get the full value of $latest use the code below:

$announcements = $dbc->query($q)->results(); //Returns array of objects
$announcements = prepareMessage($announcements); //Adds html tags to values
$latest = $announcements[0]; //Assign first index to variable


function prepareMessage($message) {
    foreach($message as $values => $key) {
        $key->title = '<h2>'.$key->title.'</h2>';
        $key->name = '<p>'.$key->name.'</p>';
        $key->date = '<small>'.humanDate($key->date).'</small>';
    }

    return $message;
}

Or you can change the amount of characters to display on this line (where "50" are the amount of characters to show and "0" is where to start from)

$key->announcement = substr($key->announcement, 0, 50) . '...</p>';

4 Comments

He is applying truncate() to $announcements... now unless $latest is a reference this won't answer his question. :)
I don't think your answer is really explaining why changing the value on $announcement[0] also changes the value in $latest.
M. Adam Kendall's answer explains exactly why and how... op didn't mention which php version was using.
but its not explaining why the value is cut off (changing) as stated in the question?

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.