Hi There

I have come across some code in someone's API that I wish to use myself and I wonder if anyone could advise me on how to use properly..

I am able to call function
<?php
playerProfile($array);
?>
without any return value.

Subsequently, in my php I can retrieve with -
<?php
print $array["variablename"]; //this has a value inside of it
?>

It seems like calling that function creates an $array with local scope, yet the initial call to playerProfile() did not have a return value..

Does anyone know what I must put in the function definition to mimic this behaviour? Do I need a particular construct/keyword..

Thanks in advance and Season's greetings

Jon
Edit/Delete Message

As Shawn said, what you are looking for is a technique used in many programming languages called passing by reference. Instead of giving the function the value the variable, the place where the variable is stored in memory is given so that function is free to edit the variable (or array). If you are familiar with C, they are called pointers (and they are very similar in PHP, with the exception that in C arrays are automatically passed by reference since they are really several variable in sequence).

To replicate what you are trying to do, try this:

<?php
function playerProfile(&$arr) //Here's the magic, add an & before the argument name
{
  $arr["variablename"] = "has a value"; //Changes made within the function will affect the array outside of the function
}

$arr = array(); //Don't give the variable a name reserved by PHP
playerProfile($arr); //There nothing you need to do here to pass by reference 
print_r($arr); //Show the change by printing the array
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.