标签云

微信群

扫码加入我们

WeChat QR Code

What is this?This is a collection of questions that come up every now and then about syntax in PHP. This is also a Community Wiki, so everyone is invited to participate in maintaining this list. Why is this?It used to be hard to find questions about operators and other syntax tokens.¹The main idea is to have links to existing questions on Stack Overflow, so it's easier for us to reference them, not to copy over content from the PHP Manual.Note: Since January 2013, Stack Overflow does support special characters. Just surround the search terms by quotes, e.g. [php] "==" vs "==="What should I do here?If you have been pointed here by someone because you have asked such a question, please find the particular syntax below. The linked pages to the PHP manual along with the linked questions will likely answer your question then. If so, you are encouraged to upvote the answer. This list is not meant as a substitute to the help others provided.The ListIf your particular token is not listed below, you might find it in the List of Parser Tokens.& Bitwise Operators or ReferencesWhat does it mean to start a PHP function with an ampersand?Understanding PHP & (ampersand, bitwise and) operatorPHP "&" operatorDifference between & and && in PHPWhat does "&" mean here in PHP?What does "&" mean in this case?What does the "&" sign mean in PHP?What does this signature mean (&) in PHP?How does the "&" operator work in a PHP function?What does & in &2 mean in PHP?When should I use a bitwise operator?Is there ever a need to use ampersand in front of an object? (&$)=& ReferencesReference assignment operator in PHP, =&What do the "=&" and "&=" operators in PHP mean?What do the '&=' and '=&' operators do?What does =& mean in PHP?&= Bitwise OperatorsWhat do the "=&" and "&=" operators in PHP mean?What do the '&=' and '=&' operators do?&& Logical Operators'AND' vs '&&' as operatorDifference between & and && in PHPIs there any difference between "and" and "&&" operators in PHP?PHP - and / or keywords% Arithmetic OperatorsWhat does the percent sign mean in PHP?What is the PHP operator % and how do I use it in real-world examples?!! Logical OperatorsDouble not (!!) operator in [email protected] Error Control OperatorsWhat is the use of the @ symbol in PHP?'At' symbol before variable name in PHP: @$_POSTPHP functions and @functionsShould I use @ in my PHP code??: Ternary OperatorWhat are the PHP operators "?" and ":" called and what do they do??: operator (the 'Elvis operator') in PHPWhere can I read about conditionals done with "?" and ":" (colon)?Using PHP 5.3 ?: operator?? Null Coalesce Operator (since PHP 7)C#'s null coalescing operator (??) in PHP?string?int?array?bool?float Nullable return type declaration (since PHP 7.1)Nullable return type declaration: Alternative syntax for control structures, Ternary OperatorWhat is ":" in PHP?What does ":" mean in PHP?:: Scope Resolution OperatorWhat do two colons mean in PHP?What's the meaning of the PHP token name T_PAAMAYIM_NEKUDOTAYIM?What's the difference between :: (double colon) and -> (arrow) in PHP?What exactly are late static bindings in PHP?static::staticFunctionName()Unexpected T_PAAMAYIM_NEKUDOTAYIM, expecting T_NS_Separator\ NamespacesBackslash in PHP -- what does it mean?What does a \ (backslash) do in PHP (5.3+)?-> Classes And ObjectsWhat is the "->" PHP operator called and how do you say it when reading code out loud?Where do we use the object operator "->" in PHP?What's the difference between :: (double colon) and -> (arrow) in PHP?What does the PHP syntax $var1->$var2 mean?What does "->" mean/refer to in PHP?=> ArraysWhat does "=>" mean in PHP?Use of => in PHPWhat does $k => $v in foreach($ex as $k=>$v) mean?^ Bitwise OperatorsHow does the bitwise operator XOR ('^') work?What does ^ mean in PHP?>> Bitwise OperatorsWhat does >> mean in PHP?<< Bitwise OperatorsStrange print behaviour in PHP?<<< Heredoc or NowdocWhat does <<<END mean in PHP?PHP expression <<<EOBIn PHP, what does "<<<" represent?Using <<<CON in PHPWhat's this kind of syntax in PHP?= Assignment OperatorsThe 3 different equals== Comparison OperatorsHow do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?PHP != and == operatorsThe 3 different equalsType-juggling and (strict) greater/lesser-than comparisons in PHP=== Comparison OperatorsWhat does "===" mean?How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?The 3 different equalsType-juggling and (strict) greater/lesser-than comparisons in PHP!== Comparison OperatorsWhat does !== comparison operator in PHP mean?Is there a difference between !== and != in PHP?!= Comparison OperatorsPHP != and == operatorsIs there a difference between !== and != in PHP?comparing, !== versus !=What is the difference between <> and !=<> Comparison OperatorsPHP operator <>PHP's <> operatorWhat is the difference between <> and !=Type-juggling and (strict) greater/lesser-than comparisons in PHP<=> Comparison Operators (since PHP 7.0)Spaceship (three way comparison) operator| Bitwise OperatorsWhat is the difference between the | and || operators?What Does Using A Single Pipe '|' In A Function Argument Do?|| Logical OperatorsWhat is the difference between the | and || operators?PHP - and / or keywordsWhat exactly does || mean?The behaviour of the or operator in PHP~ Bitwise OperatorsWhat does this ~ operator mean here?+ Arithmetic Operators, Array Operators+ operator for array in PHP?+= and -= Assignment OperatorsWhat is += used for?What does `$page -= 1` in my PHP document mean?++ and -- Incrementing/Decrementing OperatorsUnderstanding IncrementingAnswer below.= Assignment OperatorsWhat is the difference between .= and += in PHP?To understand a line of PHP. String OperatorsDifference between period and comma when concatenating with echo versus return?What does a . (dot) do in PHP?, Function ArgumentsDifference between period and comma when concatenating with echo versus return?, Variable DeclarationsWhat do commas mean in a variable declaration?$$ Variable VariablesWhat does $$ (dollar dollar or double dollar) mean in PHP?what is "$$" in PHP$function() and $$variable` Execution OperatorWhat are the backticks `` called?<?= Short Open TagsWhat does this symbol mean in PHP <?=What does '<?=' mean in PHP?What does <?= mean?[] Arrays (short syntax since PHP 5.4)PHP arrays... What is/are the meaning(s) of an empty bracket?What is the meaning of []Php array_push() vs myArray[]What does [] mean when reading from a PHP array?Shorthand for arrays: literal $var = [] empty array<? Opening and Closing tagsAre PHP short tags acceptable to use?... Argument unpacking (since PHP 5.6)** Exponentiation (since PHP 5.6)# One-line shell-style commentCan I use hashes for comments in PHP?:? Nullable return typesphp method argument type hinting with question mark (?type)


I know this isn't strictly PHP, but what about including a link to phpdoc.org for phpDocumentor comment syntax, which is commonly used and it's also impossible to search for /**?

2019年05月24日45分57秒

Can I suggest square bracket and curly bracket ?

2019年05月24日45分57秒

I ran into this problem a lot too (not being able to search for special characters), which is why I made SymbolHound, a search engine that doesn't ignore special characters. I also posted it on StackApps.

2019年05月24日45分57秒

Well, from the heading Why is this?, I'd guess it's because "The main idea is to have links to existing questions on Stack Overflow, so it's easier for us to reference them".

2019年05月24日45分57秒

A question was asked today (Nov.20/15) stackoverflow.com/questions/33833259/what-is-rscat-in-php asking "What is $rsCat in php" (should that question still be made visible and not deleted).Strangely enough, there isn't a reference about $ variable, but about $$ Variable Variables only. I believe that should be amended somewhere.

2019年05月24日45分57秒

+1 for the note that decrementers don't work on characters, only on numbers

2019年05月24日45分57秒

For everyone's sake, please remove the bolded information about pre-incrementing being infinitesimally faster. This is the absolute worst example of premature optimization and this kind of information should not be in people's heads if they are just starting to learn PHP.

2019年05月24日45分57秒

Lotus - I consider it a fun fact. If you're a beginner to PHP, or C++, etc, it seems pretty wacky that ++i and i++ are different enough to work at different speeds. I found it fascinating.

2019年05月24日45分57秒

Peter Ajtai Yes, it's interesting, but from the way you've structured your post you make it seem like one of the prime facts of PHP that is absolutely vital to using the language.

2019年05月24日45分57秒

Not sure if it is just me or not, but the apple example seems a little misleading. 'I have 10 apples. I just ate one' makes me believe you had 11 apples, but now you have 10 because you just ate one rather than you eating one after saying you have 10, thus making you have 9 -- which is what the code is implying.

2019年05月24日45分57秒

What if $a takes a value greater than 255?

2019年05月24日45分57秒

AycanYaşıt Most of the operating system is using 32 bit and 64 bit system, that means the limit is much more than 255 (8 bits).

2019年05月24日45分57秒

AycanYaşıt Actually, the representation here with one byte length isn't even correct, as even the smallest integer is still 64 bit (8 byte) in memory on a modern 64bit platform.

2019年05月24日45分57秒

Why and & is0 0 0 0 1 0 0 0and or | is0 0 0 0 1 0 1 1?

2019年05月24日45分57秒

Not sure how $a <=> $b is different to $a - $b

2019年05月24日45分57秒

AcidShout $a - $b works for numbers, but not strings, objects, or arrays.

2019年05月24日45分57秒

mcrumley No, it's worse than that. In general $a - $b doesn't even work for numbers; it works only for integers. It doesn't work for non-integer numbers, because usort casts your comparator function's return values to int, which means 0.5 gets cast to 0, which means that two numbers with a difference of less than 1, such as 4 and 4.6, may (depending upon which one gets passed as the first argument to your comparator function) incorrectly compare as equal.

2019年05月24日45分57秒

MarkAmery the migration guide isn't the documented behavior of the operator. For that you want to look at the language operators section of the manual for that php.net/language.operators.comparison the actual behavior behind this relies on various comparison functions of the API, like when you're doingstrcmp for strings, where you can not guarantee the actual return value in every single case. Sure, it's almost always 1, 0, or -1, but for the cases where you can't guarantee it like in wrapping libc's strcmp, you offer up the same defined behavior as the underlying spec to be safe

2019年05月24日45分57秒

MarkAmery The point here is not to allow people to rely on undefined behavior. For the one case where someones gets a value that is not exactly 1, 0, or -1, you get someone filing a bug report thinking there's something wrong in the language. Which is why we document that all we can guarantee is that value will be less than, greater than, or equal to 0, and not necessarily 1, 0, and -1.

2019年05月24日45分57秒

The above works with 'interfaces' as well. This is useful for checking that a particular interface is available.

2019年05月24日45分57秒

This is a copy of this answer.

2019年05月24日45分57秒

Check php.net/manual/en/language.operators.comparison.php for the reference

2019年05月24日45分57秒

{''} notation for property names. echo $movies->movie->{'great-lines'}->line; can be used to access data in SimpleXMLElement. Maybe it can be used in other cases too. php.net/manual/en/simplexml.examples-basic.php

1970年01月01日00分03秒

This already exists: stackoverflow.com/q/3446216/3933332

2019年05月24日45分57秒

Rizier123 : What does mean by "Heredocs can not be used for initializing class properties. Since PHP 5.3, this limitation is valid only for heredocs containing variables."?

2019年05月24日45分57秒

This looks like a repeat of this existing answer.

2019年05月24日45分57秒