Understanding Parameters and Arguments

Although i have passed the php functions parameters & arguments, i still need more conceptual understanding about the same.

check below code…

function createMeme ($valueMeme)
   {
       $valueMeme = ["Top_text"=>"PHP Top","Bottom_text"=>"PHP Bottom","img"=>"PHP img","des"=>"PHP des"];
       $valueMeme["Top_text"] = "Changed PHP Top";
       $valueMeme["Bottom_text"] = "Changed PHP Bottom";
       return $valueMeme;
   }
  $newMeme = createMeme ($assignMeme);
   print_r($newMeme);

it does print array with new key’s value. but at the same it also says undefined variable $assignMeme

I am really getting confused with $assignMeme…

  1. Whats the use of $assignMeme IN THIS code?

  2. can you make this code work without any error or warning?

  1. $assignMeme is just the parameter you’re passing; in createMeme(), it is referenced as $valueMeme.
  2. You never define $assignMeme. You’ll need to set it before calling it in your function.
1 Like

Here is another example…

function understanding ($_i_got_value) {
      $_i_got_value = $_i_got_value * 2;
      return $_i_got_value;
   }

   $_i_am_new_variable = understanding (2);
   echo "Return value is $_i_am_new_variable\n";

Almost same as previous one. But this prints 2 and the previous one throws error. Here argument is an integer (2) and in earlier example it is a variable.

How do i make previous code work?

You need to define the variable. In your second example you pass the value 2 to the function, but in your first, you pass a variable $assignMeme that does not yet exist. Either define the variable before you call the function, or pass a non-variable value to the function (as you did in example 2):

$assignMeme = NULL;
$newMeme = createMeme($assignMeme);
print_r($newMeme);

OR…

$newMeme = createMeme(NULL);
print_r($newMeme);

Functionally speaking, though, the $valueMeme value you pass to createMeme() doesn’t do anything; it is immediately reassigned in the first line of the function.

@825orion understood :+1:

hopefully i will complete php by tomorrow morning.