Reference โ€” What does this symbol mean in PHP? – Dev

The best answers to the question “Reference โ€” What does this symbol mean in PHP?” in the category Dev.

QUESTION:

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 for the help others provided.

The List

If your particular token is not listed below, you might find it in the List of Parser Tokens.


& Bitwise Operators or References

  • What does it mean to start a PHP function with an ampersand?
  • Understanding PHP & (ampersand, bitwise and) operator
  • PHP “&” operator
  • Difference between & and && in PHP
  • What 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? (&$)

=& References

  • Reference assignment operator in PHP, =&
  • What do the “=&” and “&=” operators in PHP mean?
  • What do the ‘&=’ and ‘=&’ operators do?
  • What does =& mean in PHP?

&= Bitwise Operators

  • What do the “=&” and “&=” operators in PHP mean?
  • What do the ‘&=’ and ‘=&’ operators do?

&& Logical Operators

  • ‘AND’ vs ‘&&’ as operator
  • Difference between & and && in PHP
  • Is there any difference between “and” and “&&” operators in PHP?
  • PHP – and / or keywords

% Arithmetic Operators

  • What does the percent sign mean in PHP?
  • What is the PHP operator % and how do I use it in real-world examples?

!! Logical Operators

  • Double not (!!) operator in PHP

@ Error Control Operators

  • What is the use of the @ symbol in PHP?
  • ‘At’ symbol before variable name in PHP: @$_POST
  • PHP functions and @functions
  • Should I use @ in my PHP code?
  • What does @ mean in PHP?

?: Ternary Operator

  • What are the PHP operators “?” and “:” called and what do they do?
  • ?: operator (the ‘Elvis operator’) in PHP
  • Where 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
  • php method argument type hinting with question mark (?type)

: Alternative syntax for control structures, Ternary Operator, Return Type Declaration

  • What is “:” in PHP?
  • What does “:” mean in PHP?
  • Colon after method declaration?

:: Scope Resolution Operator

  • What 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

\ Namespaces

  • Backslash in PHP — what does it mean?
  • What does a \ (backslash) do in PHP (5.3+)?

-> Classes And Objects

  • What is the “->” PHP operator called?
  • 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?

=> Arrays

  • What does “=>” mean in PHP?
  • Use of => in PHP
  • What does $k => $v in foreach($ex as $k=>$v) mean?

^ Bitwise Operators

  • How does the bitwise operator XOR (‘^’) work?
  • What does ^ mean in PHP?

>> Bitwise Operators

  • What does >> mean in PHP?

<< Bitwise Operators

  • Strange print behaviour in PHP?

<<< Heredoc or Nowdoc

  • What does <<<END mean in PHP?
  • PHP expression <<<EOB
  • In PHP, what does “<<<” represent?
  • Using <<<CON in PHP
  • What’s this kind of syntax in PHP?

= Assignment Operators

  • The 3 different equals

== Comparison Operators

  • How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
  • PHP != and == operators
  • The 3 different equals
  • Type-juggling and (strict) greater/lesser-than comparisons in PHP

=== Comparison Operators

  • What does “===” mean?
  • How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
  • The 3 different equals
  • Type-juggling and (strict) greater/lesser-than comparisons in PHP

!== Comparison Operators

  • What does !== comparison operator in PHP mean?
  • Is there a difference between !== and != in PHP?

!= Comparison Operators

  • PHP != and == operators
  • Is there a difference between !== and != in PHP?
  • comparing, !== versus !=
  • What is the difference between <> and !=

<> Comparison Operators

  • PHP operator <>
  • PHP’s <> operator
  • What 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 Operators

  • What is the difference between the | and || operators?
  • What Does Using A Single Pipe ‘|’ In A Function Argument Do?

|| Logical Operators

  • What is the difference between the | and || operators?
  • PHP – and / or keywords
  • What exactly does || mean?
  • The behaviour of the or operator in PHP

~ Bitwise Operators

  • What does this ~ operator mean here?

+ Arithmetic Operators, Array Operators

  • + operator for array in PHP?

+= and -= Assignment Operators

  • What is += used for?
  • What does `$page -= 1` in my PHP document mean?

++ and -- Incrementing/Decrementing Operators

  • Understanding Incrementing
  • Answer below

.= Assignment Operators

  • What is the difference between .= and += in PHP?
  • To understand a line of PHP

. String Operators

  • Difference between period and comma when concatenating with echo versus return?
  • What does a . (dot) do in PHP?

, Function Arguments

  • Difference between period and comma when concatenating with echo versus return?

, Variable Declarations

  • What do commas mean in a variable declaration?

$$ Variable Variables

  • What does $$ (dollar dollar or double dollar) mean in PHP?
  • what is “$$” in PHP
  • $function() and $$variable

` Execution Operator

  • What are the backticks “ called?

<?= Short Open Tags

  • What 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 tags

  • Are PHP short tags acceptable to use?

... Argument unpacking (since PHP 5.6)


** Exponentiation (since PHP 5.6)


# One-line shell-style comment

  • Can I use hashes for comments in PHP?

?-> NullSafe Operator Calls (since PHP 8.0)

  • What does this symbol mean “?->” in php, within an object or null value

ANSWER:

Bitwise Operator

What is a bit? A bit is a representation of 1 or 0. Basically OFF(0) and ON(1)

What is a byte? A byte is made up of 8 bits and the highest value of a byte is 255, which would mean every bit is set. We will look at why a byte’s maximum value is 255.

-------------------------------------------
|      1 Byte ( 8 bits )                  |
-------------------------------------------
|Place Value | 128| 64| 32| 16| 8| 4| 2| 1|     
-------------------------------------------

This representation of 1 Byte

1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 255 (1 Byte)

A few examples for better understanding

The “AND” operator: &

$a =  9;
$b = 10;
echo $a & $b;

This would output the number 8. Why? Well let’s see using our table example.

-------------------------------------------
|      1 Byte ( 8 bits )                  |
-------------------------------------------
|Place Value | 128| 64| 32| 16| 8| 4| 2| 1|     
-------------------------------------------
|      $a    |   0|  0|  0|  0| 1| 0| 0| 1|    
-------------------------------------------
|      $b    |   0|  0|  0|  0| 1| 0| 1| 0|
------------------------------------------- 
|      &     |   0|  0|  0|  0| 1| 0| 0| 0|
------------------------------------------- 

So you can see from the table the only bit they share together is the 8 bit.

Second example

$a =  36;
$b = 103;
echo $a & $b; // This would output the number 36.
$a = 00100100
$b = 01100111

The two shared bits are 32 and 4, which when added together return 36.

The “Or” operator: |

$a =  9;
$b = 10;
echo $a | $b;

This would output the number 11. Why?

-------------------------------------------
|      1 Byte ( 8 bits )                  |
-------------------------------------------
|Place Value | 128| 64| 32| 16| 8| 4| 2| 1|     
-------------------------------------------
|      $a    |   0|  0|  0|  0| 1| 0| 0| 1|    
-------------------------------------------
|      $b    |   0|  0|  0|  0| 1| 0| 1| 0|
------------------------------------------- 
|      |     |   0|  0|  0|  0| 1| 0| 1| 1|
-------------------------------------------

You will notice that we have 3 bits set, in the 8, 2, and 1 columns. Add those up: 8+2+1=11.

ANSWER:

Incrementing / Decrementing Operators

++ increment operator

-- decrement operator

Example    Name              Effect
---------------------------------------------------------------------
++$a       Pre-increment     Increments $a by one, then returns $a.
$a++       Post-increment    Returns $a, then increments $a by one.
--$a       Pre-decrement     Decrements $a by one, then returns $a.
$a--       Post-decrement    Returns $a, then decrements $a by one.

These can go before or after the variable.

If put before the variable, the increment/decrement operation is done to the variable first then the result is returned. If put after the variable, the variable is first returned, then the increment/decrement operation is done.

For example:

$apples = 10;
for ($i = 0; $i < 10; ++$i) {
    echo 'I have ' . $apples-- . " apples. I just ate one.\n";
}

Live example

In the case above ++$i is used, since it is faster. $i++ would have the same results.

Pre-increment is a little bit faster because it really increments the variable and after that ‘returns’ the result. Post-increment creates a special variable, copies there the value of the first variable and only after the first variable is used, replaces its value with second’s.

However, you must use $apples--, since first, you want to display the current number of apples, and then you want to subtract one from it.

You can also increment letters in PHP:

$i = "a";
while ($i < "c") {
    echo $i++;
}

Once z is reached aa is next, and so on.

Note that character variables can be incremented but not decremented and even so only plain ASCII characters (a-z and A-Z) are supported.


Stack Overflow Posts:

  • Understanding Incrementing

ANSWER:

_ Alias for gettext()

The underscore character ‘_’ as in _() is an alias to the gettext() function.

ANSWER:

<=> Spaceship Operator

Added in PHP 7

The spaceship operator <=> is the latest comparison operator added in PHP 7. It is a non-associative binary operator with the same precedence as equality operators (==, !=, ===, !==). This operator allows for simpler three-way comparison between left-hand and right-hand operands.

The operator results in an integer expression of:

  • 0 when both operands are equal
  • Less than 0 when the left-hand operand is less than the right-hand operand
  • Greater than 0 when the left-hand operand is greater than the right-hand operand

e.g.

1 <=> 1; // 0
1 <=> 2; // -1
2 <=> 1; // 1

A good practical application of using this operator would be in comparison type callbacks that are expected to return a zero, negative, or positive integer based on a three-way comparison between two values. The comparison function passed to usort is one such example.

Before PHP 7 you would write…

$arr = [4,2,1,3];

usort($arr, function ($a, $b) {
    if ($a < $b) {
        return -1;
    } elseif ($a > $b) {
        return 1;
    } else {
        return 0;
    }
});

Since PHP 7 you can write…

$arr = [4,2,1,3];

usort($arr, function ($a, $b) {
    return $a <=> $b;
    // return $b <=> $a; // for reversing order
});