PHP Using PHP with MySQL Limiting Records in SQL Queries Reversing the Result Set

array_reverse() Function quiz

In this quiz is asks what would be the output of the array after using

What would you expect the var_dump from the following code to be? $array = array("Hello", "World"); array_reverse($array); var_dump($array);

A array(2) { [0]=> string(5) "World" [1]=> string(5) "Hello" } B array(2) { [0]=> string(5) "Hello" [1]=> string(5) "World" }

It says the answer is (B). Is it an error in the quiz, or something else?

Never mind, I see now that they never apply the value of the revers to $array.

4 Answers

Stone Preston
Stone Preston
42,015 Points

thats because array reverse returns a new array that is the reverse of the array passed in as an argument. It doesnt modify the array argument, just returns a new one. So in this case your $array variable is still the same as it was at first, even after you used the reverse function

I see! Thank you! That was crafty ;)

Stone Preston
Stone Preston
42,015 Points

and you were right that if you did something like

$array = array_reverse($array); 

that b would then be the answer, since you assigned your array variable the return value of the array_reverse function.

Okay, now I have one more question. I have tried to use the markdown for code and other things of the sort, but it never gets applied in my forum posts. Do you have any idea why? example php $array = array_reverse($array);

I used three back ticks before and after that line of code. The php came after the first set as it suggests specifying the code in the cheat sheet.

Stone Preston
Stone Preston
42,015 Points

ok that is single line markdown. to markdown a block of code (multiple lines) , skip a line so that there is an empty line between the text/top of the comment box. then add your 3 back ticks ( ` ) then go to a new line and add your code block, then go to a new line and add 3 more back ticks to close the block

here is a code block
that is multiple lines long

here is a single line snippet

Thanks! I have wondered about that!

Okay, let me test that.

test block of code