some1
some1

Reputation: 1737

Strange array syntax in PHP

I'm modifying a web form, this is the array syntax it currently uses:

$fields = array(); 
$fields{"name"} = "Name"; 
$fields{"title"} = "Title"; 
$fields{"email"} = "Email"; 
$fields{"prefer_phone"} = "Prefer phone"; 
$fields{"prefer_email"} = "Prefer email";
$fields{"message"} = "Message";
$fields{"referral"} = "Referral";

The values on the left are from the webform, on the right is what displays in the email.

I'm not too familiar with php but I've not seen an array setup like this in other languages.. (where are the array[5], etc.?)

I need to add another variable into the array. The new variable is from earlier in the script so it won't have the $fields{} syntax I assume.

I can't find documentation of this type of syntax for PHP arrays -- help? How do I add another value into here.. something like:

{$phone} = "Phone"; 

Upvotes: 1

Views: 133

Answers (3)

random_user_name
random_user_name

Reputation: 26160

The curly braces actually tell PHP to escape the value within them, so while the other answers are right as well, there's more to the story that is worth understanding.

You should also read This Answer

Upvotes: 3

developerwjk
developerwjk

Reputation: 8659

In PHP arrays you can use numbers or strings inside the brackets. http://php.net/manual/en/language.types.array.php

Normally, however, it uses plain brackets [] not curly {}.

Upvotes: 2

Chris Brown
Chris Brown

Reputation: 4635

See this question

Both are valid, but could be changed to the square bracket notation you expected to see.

Upvotes: 3

Related Questions