0

I set a var:

$name = "john";

Then I have a function:

function whatever(){
    $name .= " smith";
}

Outside the function I want to dump the name:

var_dump($name);

But just john is output. I've tried using:

function whatever() use ($name) {
    var_dump($name) //john
    $name .= " smith";
}

But the final var dump outside the function still does not append smith. Where am I going wrong?

panthro
  • 22,779
  • 66
  • 183
  • 324
  • 1
    Is this an *academic quesition*, or will you actually use the answer? I'm asking because what you showed could be considered *bad practice*. But to answer, you simple need a reference to `$name`. E.g.: `function whatever() use (&$name) {` – Yoshi Oct 12 '16 at 10:43
  • I will actually use the answer. The example above is extremely abstracted to what I will actually use it for, but similar principles. – panthro Oct 12 '16 at 10:45
  • Ok, further reading that might be of interest then: https://php.net/manual/functions.arguments.php#functions.arguments.by-reference and http://stackoverflow.com/a/885/697154 But please, never use `global`. – Yoshi Oct 12 '16 at 10:50
  • 1
    This is more or less a duplicate of http://stackoverflow.com/a/16959577/476; only the tiny detail of `use (&$name)` as a solution is different. – deceze Oct 12 '16 at 11:47

3 Answers3

1

simpliest way to do that:

$name = "john";

function merge($name) {
  return "$name smith";
}

$name = merge($name);

echo $name; // shows you: john smith

i think this is exactly what you was looking for

don't try to use outer scope vars like in javascript. thats not testable and bad practice

mtizziani
  • 956
  • 10
  • 23
0

What you have encountered here is the concept of variable scope in PHP.

What ever is defined outside of a function is irrelevant inside a function, due to it having its own scope.

So you can do what you intend in multiple ways.

Method 1: Using Global Keyword

$a = 1; $b = 2;

function Sum(){
    global $a, $b;

    $b = $a + $b;
} 

Sum();
echo $b;

Note: The global keyword makes the variable available to the "global" scope, and it can be defined and accessed anywhere from any class or even from outside during the runtime, due to which, from maintenance and debugging perspective it creates a lots of hassle hence it is very much frowned upon and considered a last resort tool.

Method 2: Function Argument

$name = "john";
$lastname = "smith";
function fullname($name, $lastname){
    $name .= $lastname;
    return $name;
}
echo fullname($name, $lastname);

Method 3: Using Reference

function foo(&$var){
    $var++;
}

$a=5;
foo($a);
// $a is 6 here
Mohd Abdul Mujib
  • 13,071
  • 8
  • 64
  • 88
  • A little bit more of an explanation wouldnt go amiss. – panthro Oct 12 '16 at 10:32
  • What about using the use way? – panthro Oct 12 '16 at 10:33
  • Thanks, with global, how far reaching is this, whats the scope of global, the class or the entire app? – panthro Oct 12 '16 at 10:46
  • I dont think you get the question. – panthro Oct 12 '16 at 10:55
  • 1
    As far as `use` goes, it has an intermediate to expert level of understanding required for someone to use it. So if you are beginning to program, just use the easier methods. For the sake of completeness, `use` has various uses in php. For starters it is used 1. For aliasing the classes, 2. defining the closure 3. Importing the namespaces etc. describing everything will be out of the scope of this question. – Mohd Abdul Mujib Oct 12 '16 at 11:16
0

Its a case of variable scope. What you can do here is getting it via global or passing a parameter:

// Pass by parameter
$name = "john";

function whatever($name){
    $name .= " smith";
    return $name;
}

var_dump(whatever($name));

// Calling it via global
function whatever2(){
    global $name;
    $name .= " smith";
    return $name;
}

var_dump(whatever2());

If you still want to use "use":

$name2 = "john";

$full_name = function () use ($name2){
    return $name2 . " smith";
};

var_dump($full_name()); 
L. Herrera
  • 490
  • 4
  • 13