0

I was able to use render successfully in both buy.php and sell.php, but can't seem to get it in quote.php. Here's the relevant snippet:

$stock = lookup($_POST["symbol"]);

$issue[] = [
            "name"=>$stock["name"],
            "price"=>$stock["price"],
            ];
render("quoteoutput_form.php", ["name"=>$issue["name"], "price"=>$issue["price"]]);

I'm getting an undefined index error for both name and price in the render line.

1

Note the use of brackets [] on this line:

$issue[] = [
            "name"=>$stock["name"],
            "price"=>$stock["price"],
            ];

This results in $issue being treated as an array. The two brackets [] means that the value on the right is being appended to the $issue array.

This is probably not what you want since you are only passing a single stock symbol in the POST parameter.

The correct code would be:

$issue = [
          "name"=>$stock["name"],
          "price"=>$stock["price"],
          ];
3
  • Thanks, that fixed it although I'm not sure I understand why. $issue is an associative array so shouldn't it be declared as $issue[]? Here's an analogous situation from the pset specs: $stock = lookup($row["symbol"]);$positions[] = [ "name" => $stock["name"], . . .] – ronga Aug 6 '14 at 14:13
  • PHP is different to C. In C you would need to declare the variable as an array. This is not necessary in PHP, as PHP determines the type of variable depending on the value assigned to it. Here you are assigning an associative array to $value so PHP knows to make it an associative array. The brackets [] as you have used here have a different meaning in PHP (see here php.net/manual/en/function.array-push.php). If this answer helped you, please up-vote, and / or mark it as accepted. – Luke Van In Aug 6 '14 at 14:54
  • Got it, after going over the excerpt from the pset specs, it was indeed a slightly different situation, and $positions was indeed being declared as an array of arrays. Thanks for clearing that up. – ronga Aug 7 '14 at 9:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .