1

I want to know why php global function compact() only return empty array when used inside a function.

Here is the code as an example:

$hello = "world";

function myFunc($str) {
    return compact($str);
}

$arr = myFunc('hello');

print_r($arr);

Any help would be appreciated. Thank you!

3 Answers3

1

variable $hello scope issue try this:

$hello = "world";

$myFunc = function($str) use($hello) {
    return compact($str);
};
print_r($myFunc('hello'));
suresh bambhaniya
  • 1,687
  • 1
  • 10
  • 20
1

compact() function is an inbuilt function in PHP and it is used to create an array using variables. This function is opposite of extract() function. It creates an associative array whose keys are variable names and their corresponding values are array values.

you are passing variable like this compact($str), but in compact we used pass parameter like this (in single quote without $ sign)

compact('str');

know your code look like this

$hello = "world";

function myFunc($str) {
    return compact('str');//compact('str'): means passing $str variable
}

$arr = myFunc('hello');//or myFunc($hello); (myFunc('hello'): means passing hello string and myFunc($hello): means passing $hello variable)
print_r($arr);

check output

If you have multiple variables then used , like this

$f_name='john';
$l_name='smith';
$address='xyz';

$result = compact("f_name", "l_name", "address");

print_r($result);

Parameters: This function accepts a variable number of arguments separated by comma operator (','). These arguments are of string data type and specify the name of variables which we want to use to create the array. We can also pass an array as an argument to this function, in that case, all of the elements in the array passed as a parameter will be added to the output array.

Reference

Bilal Ahmed
  • 4,005
  • 3
  • 22
  • 42
  • Thanks for the answer, buddy. But you probably misunderstood my question or i forgot to mention the output expected as `Array([hello] => world)`. But your suggestion code gave output as `Array([str] => hello)`. Thankfully, suresh and david had solved my problem. – Hendra Prasetya Nov 13 '18 at 08:27
1

It's because myFunc isn't in the global scope, and can't access the $hello variable. Think about this code:

$hello = "world";
function myFunc () {
    echo $hello;
}
myFunc(); // => PHP: Undefined variable: hello

myFunc simply can't access the global variable $hello. However, if you add a global statement in the function, you can access the global scope:

$hello = "world";
function myFunc () {
    global $hello;
    echo $hello;
}
myFunc(); // => world

So, in summary, if you add a global to the top of your function, your function can work as expected:

$hello = "world";

function myFunc ($str) {
    global $hello;
    return compact($str);
}

$arr = myFunc('hello');

print_r($arr); // => Array ( [hello] => world )

Here's some further information on variable scope: Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?

Ethan
  • 4,295
  • 4
  • 25
  • 44