Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using extract() with hyphens

Tags:

php

If for example you had an associative array which looked something like this:

$array = array('first-value' => 'Hello');

And you were then to extract it:

extract($array);

How would you access "first-value" as hyphens cannot be used in variable names? Is the hyphen replaced with another character?

I have tried the following with no luck...

echo ${'first-value'};
echo $first_value;
echo $firstvalue;

Couldn't even find an mention of this in the Php manual...

Thanks in advance!

like image 596
smilly92 Avatar asked Jul 28 '11 06:07

smilly92


People also ask

What is a hyphen and how do you use it?

What Is a Hyphen? A hyphen (-) is a punctuation mark that’s used to join words or parts of words. It’s not interchangeable with other types of dashes. Use a hyphen in a compound modifier when the modifier comes before the word it’s modifying. If you’re not sure whether a compound word has a hyphen or not, check your preferred dictionary.

What is a hyphenated compound word?

Hyphenated Compound Words. Hyphenated compound words are the ones (obviously) with a hyphen between the words. Over time, many hyphenated compounds become closed compounds—teen-ager became teenager for instance. Check a dictionary if you’re not sure whether to use a hyphen or not.

Do you put a hyphen in the middle of numbers?

When you write out a number in words, you should use hyphens to show a link between the number words (e.g. “There are ninety-nine people invited.”) When the end of a line comes in the middle of a word, you can use a hyphen to divide it in a way it normally wouldn’t be.

Do you hyphenate the word technology?

For example, in the phrase “up-to-date technology,” we hyphenate “up-to-date” to signal that these three words are to be read as one concept, or adjective, functioning to modify the word “technology.” One easy way to tell if you should use a hyphen is to pair each describing word on its own with the noun it’s describing.


1 Answers

If you do echo extract($array);, you can see that it outputs 0, which is the number of variables successfully imported into the symbol table. In other words, the variable can not be imported because of the hyphen and thus does not exist.

like image 58
Tatu Ulmanen Avatar answered Oct 13 '22 01:10

Tatu Ulmanen