There are two ways to assign one variable to another:

  • By value—this creates two variables that hold copies of the same value but remain independent entities.
  • By reference—this creates two variable names (aliases) which point to the same space in memory. They cannot be modified separately!

This remains true when dealing with array variables:

$favorites = ["food"=>"pizza", "person"=>"myself", "dog"=>"Tadpole"]; $copy = $favorites; $alias =& $favorites; $favorites["food"] = "NEW!"; echo $favorites["food"]; // Prints: NEW! echo $copy["food"]; // Prints: pizza echo $alias["food"]; // Prints: NEW!

When passing arrays into functions, both built-in functions and those we write ourselves, we’ll want to be conscious of whether the arrays are being passed by value or by reference.

function changeColor ($arr) { $arr["color"] = "red"; } $object = ["shape"=>"square", "size"=>"small", "color"=>"green"]; changeColor ($object); echo $object["color"]; // Prints: green

Our function above doesn’t accept its array argument by reference. Therefore, $arr is merely assigned a copy of the argument’s value. This copy array is changed when the function is invoked, but that doesn’t affect the original argument array ($object). To do that, we’d need to pass it by reference:

function reallyChangeColor (&$arr) { $arr["color"] = "red"; } $object = ["shape"=>"square", "size"=>"small", "color"=>"green"]; reallyChangeColor ($object); echo $object["color"]; // Prints: red

Cool! Let’s write some array functions!



Create a function, createMeme() that takes in a $meme array and returns a new custom meme about PHP.

The $meme array will have the following keys: "top_text", "bottom_text", "img", and "description".

The function should change the value of "top_text" to "Much PHP" and the "bottom_text" to "Very programming. Wow."

The function should not permanently alter its argument.


Create a variable $php_doge and assign as its value the result of invoking your createMeme() with $doge_meme.

Note: we recommend using the print_r() function to see the arrays you’re creating and using, but it’s not required.


Write a function fixMeme() that takes in a $meme array and permanently changes the "top_text" and "bottom_text" to strings of your choosing.


Invoke your fixMeme() function with the $bad_meme array we provided.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?