0

I am using this to get a string from jquery to a php array via json.

    $navMenuLayout = array();
parse_str($layout['navMenu'], $navMenuLayout);
print_r($navMenuLayout);

But I noticed if I take out the first line then I still get the same output, does the first line matter?

parse_str($layout['navMenu'], $navMenuLayout);
print_r($navMenuLayout);

3 Answers 3

1

Of course, you don't have to predefine the variable for this function because PHP sets them automatically.

See the example from PHP.net for what happens when the second argument is defined/undefined:

<?php
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo $first;  // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz

parse_str($str, $output);
echo $output['first'];  // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz

?>

So, if you don't define the second argument, each key=>value pair in the string is made into an actual variable, whereas if you define a variable to be set, they are put into an array as that variable. PHP will automatically create everything for the function, you don't have to pre-define anything.

Sign up to request clarification or add additional context in comments.

Comments

0

parse_str is defined in the php manual as:

void parse_str ( string $str [, array &$arr ] )

The square brackets around the array argument mark it as optional. The & marks it as a reference argument, implying that an array will be created if a variable is passed in from the calling code.

So either is correct, but the first tells any maintainer of the code more about what you are expecting to send/get back from the function call without having to look it up in the manual.

Comments

0

The $navMenuLayout = array(); declares the variable. You can get away without declaring it first but if you have notices enabled in your php.ini error configuration, you'll get a notice informing you that an undeclared variable is being used (see http://www.php.net/manual/en/errorfunc.configuration.php#ini.error-reporting for setting your error logging levels).

$navMenuLayout is passed to the function with the intention replacing it's value with a result from the function.

If you look at http://www.php.net/parse_str , you'll see in the declaration that there is an & before the variable name: parse_str ( string $str [, array &$arr ] ). This means that the variable being passed will be over-written.

2 Comments

It will not produce a notice error as the function defines the variable automatically. In fact, notice errors are not even mentioned anywhere on the manual page, so I don't know where you got that from.
You're quite right. Looks like PHP actually does handle that automatically. I'm so used to seeing PHP Notice: Undefined variable: when dealing with code-bases that I automatically assumed this would throw one too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.