What is this operator "=>"? [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does “=>” mean in PHP?
Reference - What does this symbol mean in PHP?
I see this symbol in a lot of PHP code.
I can't figure out what it means or what it does..
Is it really an operator ?

Do you mean =>? If so, it's for initializing array keys (or indexes if you prefer). Like this:
$values = array(
'foo' => 'bar'
);
This will initiazlie an array with a key named foo with a value of bar.
Read more on about arrays at php.net.

http://php.net/manual/en/language.operators.php

Related

What does foo[] mean in $this->foo[] in php [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 6 years ago.
Is foo[] an array?
In the following code is an array element being assigned to foo[]? And is there an array key that is automatically associated with it?
$this->foo[] = 'hello world';
This is adding an element to the end of the foo array.
It's the same as using array_push().

Using an array without initialization in PHP [duplicate]

This question already has answers here:
Should an array be declared before using it? [closed]
(7 answers)
Closed 7 years ago.
In most languages, I have to initialize an associative array before I can use it:
data = {}
data["foo"] = "bar"
But in PHP I can just do
data["foo"] = "bar"
Are there any repercussions to doing this? Is this "the right way" to write PHP?
Is the same, but is not a good idea, the next is a copy-paste from php documentation.
If $arr doesn't exist yet, it will be created, so this is also an alternative way to create an array. This practice is however discouraged because if $arr already contains some value (e.g. string from request variable) then this value will stay in the place and [] may actually stand for string access operator. It is always better to initialize variable by a direct assignment.
Basically it's the same, and no you won't find any problem or repercussion.
But if you like you can do this:
$a = array();
You can read more in the PHP page

Understanding array output [duplicate]

This question already has answers here:
Characters allowed in php array keys?
(11 answers)
Closed 8 years ago.
I am studying PHP and came across a question like this:
What is the output of the following array?
Code:
$a = array(0.001 => 'b', .1 => 'c');
print_r($a);
The answer is 0 => 'c' - now I know array keys can't be numbers but wouldn't that throw an error? Why is the first element overwritten?
From the documentation on arrays:
Floats are also cast to integers, which means that the fractional part will be truncated. E.g. the key 8.7 will actually be stored under 8.
and, as Alex points out below:
If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten.

php variable from variable [duplicate]

This question already has answers here:
Mixing a PHP variable with a string literal
(5 answers)
Closed 9 years ago.
I have a variable and I want to create a variable with that. I get the variable from database and put it together with some text and then I want another variable.
For exampel
$a = $ . "txt" . $d;
Try with this. It will create a variable from another one.
$a = ${'txt'.$d}
P.s. This is a question asked a couple of times. You might have found the answer simply by searching the issue on google.

What does => mean in php? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What does “=>” mean in PHP?
I'm learning about arrays and am not certain what => means or does.
Assignment of a value to a named key.
http://www.php.net/manual/en/language.operators.assignment.php
$example = array('color' => 'blue');
echo $example['color']; //prints blue
I believe it is just syntax for writing array literals that behave like maps / dictionaries.
$mydict = array('hello' => 'world');
The above is an array with one element. But instead if indexing that element with an integer index, you use the key 'hello':
echo $mydict['hello']

Categories