3

I have a question about syntax in php. What is the mean of + in following line? Thanks!

+$array['key1']['key2'] = "value"
vascowhite
  • 18,120
  • 9
  • 61
  • 77
Urmelinho
  • 1,927
  • 2
  • 14
  • 22
  • 12
    Is this from a diff? – Ed Heal Aug 10 '12 at 11:34
  • Ed Heal might be right. Never saw this syntax before. – Alp Aug 10 '12 at 11:35
  • might seen Objective C syntax :) – Shehzad Bilal Aug 10 '12 at 11:36
  • Can you post more lines from the same file to see its context (to verify if it's a diff or not, or a typo). – newfurniturey Aug 10 '12 at 11:37
  • possible duplicate of [Reference - What does this symbol mean in PHP?](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – vascowhite Aug 10 '12 at 11:37
  • 1
    It means that someone pressed `enter` in previous line – tereško Aug 10 '12 at 11:40
  • 1
    If this appears literally in the code, it is superfluous and has no effect but [it is not invalid](http://codepad.viper-7.com/RqvWkm). I'm a little surprised it is still valid since it has no effect, but there you go. On the other hand, leading the line with a `-` does [have the effect](http://codepad.viper-7.com/NJgoNl) of inverting the sign on a number. – DaveRandom Aug 10 '12 at 11:46
  • @Alp - Your link to your web site on your profile does not work. – Ed Heal Aug 10 '12 at 11:55
  • I work with github, and a colleague maybe coped some code directly from github, but curiously is that apache and php doesn't notify any problem with these lines. I think that a line with this kind of declaration have a meaning but i don't understand what is the meaning. Maybe "+" is sum operator on a empty variable and does change nothing and then the allocation is made. so correct code. Make sense? – Urmelinho Aug 10 '12 at 12:18
  • @Ed Heal - thanks, the page is not completed yet. I exchanged it with a link to [my open source project](https://github.com/alp82/abmash) – Alp Aug 10 '12 at 12:29

3 Answers3

8

It has no meaning, it is superfluous. You can write the exact same statement without the plus:

$array['key1']['key2'] = "value"

If you have that from a unified diff file, it means that this line was added. So that plus is not PHP code, it is a marker for that line in the diff/patch. The other marker is minus - for removing a line.

hakre
  • 193,403
  • 52
  • 435
  • 836
0

It is used for showing the line diff. in different versions of a same file.

Deleted line can be shown as,

- $array['key1']['key2'] = "value";

Added line can be shown as,

+ $array['key1']['key2'] = "value";
Sumesh TG
  • 440
  • 1
  • 4
  • 15
-3

Edit: Apparently I misunderstood the question, so this answer is invalid.

It look like a "shorthand" technique.

+$array['key1']['key2'] = "value"

should be the same as:

$array['key1']['key2'] = $array['key1']['key2'] + "value"

I have never seen it used like this, so I could be wrong. I know it as:

$x++;

is the same as: $x += 1; or $x = $x + 1; and I know that ++$x; exist also as a pre-increment

Gunnar
  • 2,585
  • 4
  • 21
  • 22
  • 2
    No, no, no shorthand technique: http://codepad.org/SzADy6Bi not same as `+=` no, no *sing* ;) – hakre Aug 10 '12 at 11:42