Reference - What does this symbol mean in PHP?
Reference - What does this symbol mean in PHP?
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 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 "&" operator work in PHP function?
- What does & in &2 mean in PHP?
- When should I use Bitwise operator?
- Is there ever a need to use ampersand in front of an object
=&
References
- Reference assignment operator in php =&
- what do "=&" / "&=" operators in php mean?
- What do the '&=' and '=&' operators do?
- What does =& mean in PHP?
- 'AND' vs '&&' as operator
- difference between & and && in PHP
- PHP: Is there a difference between operators AND and && here?
- PHP - and / or keywords
- What does the percent sign mean in PHP?
- What is the PHP operator % and how to use it in real world examples?
- What is the use of @ symbol in php?
- PHP - 'At' symbol before variable name: @$_POST
- PHP functions and @functions
- Should I use @ in my PHP code?
- What is ?: in PHP 5.3?
- What is the PHP ? : operator called and what does it do?
- ?: operator (the 'Elvis operator') in PHP
- Where can I read about conditionals done with ? and :
- Using PHP 5.3 ?: operator
??
Null Coalesce Operator (since PHP 7)
:
Alternative syntax for control structures, Ternary 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
- What is the "->" PHP operator called and how do you say it when reading code out loud?
- where we use object operator "->" in php
- What's the difference between :: (double colon) and -> (arrow) in PHP?
- What does this PHP syntax mean: $var1->$var2
- Absolutely basic PHP question about the "-> " syntax
=>
Arrays
- php == vs === operator
- How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
- PHP != and == operators
- The 3 different equals
- PHP Type-Juggling and (strict) Greater/Lesser Than Comparisons
- What does "===" mean?
- php == vs === operator
- How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
- The 3 different equals
- PHP Type-Juggling and (strict) Greater/Lesser Than Comparisons
- PHP != and == operators
- Is there a difference between !== and != in PHP?
- comparing, !== versus !=
- What is the difference between <> and !=
- php operator <>
- php <> operator
- What is the difference between <> and !=
- PHP Type-Juggling and (strict) Greater/Lesser Than Comparisons
<=>
Comparison Operators (since PHP 7.0)
- What is the difference between the | and || operators?
- What Does Using A Single Pipe '|' In A Function Argument Do?
- What is the difference between the | and || operators?
- PHP - and / or keywords
- What exactly does || mean?
- The behaviour of the or operator in PHP
+
Arithmetic Operators, Array Operators
++
Incrementing/Decrementing Operators
[]
Arrays
- PHP arrays... What is/are the meaning(s) of an empty bracket?
- PHP : 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
...
Argument unpacking (since PHP 5.6)
**
Exponentiation (since PHP 5.6)
#
One-line shell-style comment
Answer by Peter Ajtai for Reference - What does this symbol mean in PHP?
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"; }
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 value of the first variable and only after 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:
Answer by Ankur Saxena for Reference - What does this symbol mean in PHP?
syntax name description x == y Equality True if x and y have the same key/value pairs x != y Inequality True if x is not equal to y x === y Identity True if x and y have the same key/value pairs in the same order and of the same types x !== y Non-identity True if x is not identical to y ++ x Pre-increment Increments x by one, then returns x x ++ Post-increment Returns x, then increments x by one -- x Pre-decrement Decrements x by one, then returns x x -- Post-decrement Returns x, then decrements x by one x and y And True if both x and y are true x=6 y=3 (x < 10 and y > 1) returns true x && y And True if both x and y are true x=6 y=3 (x < 10 && y > 1) returns true a . b Concatenation Concatenate two strings: "Hi" . "Ha"
Answer by Ankur Saxena for Reference - What does this symbol mean in PHP?
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| -------------------------------------------
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?
$a = 00001001 $b = 00001010
You will notice that we have 3 bits set, in the 8, 2, and 1 columns. Add those up: 8+2+1=11.
Answer by HabeebPerwad for Reference - What does this symbol mean in PHP?
The underscore character '_' as in _()
is an alias to gettext()
.
Answer by Sumoanand for Reference - What does this symbol mean in PHP?
Magic constants: Although these are not just symbols but important part of this token family. There are eight magical constants that change depending on where they are used.
__LINE__
: The current line number of the file.
__FILE__
: The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__
always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.
__DIR__
: The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__)
. This directory name does not have a trailing slash unless it is the root directory. (Added in PHP 5.3.0.)
__FUNCTION__
: The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the function name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.
__CLASS__
: The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased. The class name includes the namespace it was declared in (e.g. Foo\Bar
). Note that as of PHP 5.4 __CLASS__
works also in traits. When used in a trait method, __CLASS__
is the name of the class the trait is used in.
__TRAIT__
: The trait name. (Added in PHP 5.4.0) As of PHP 5.4 this constant returns the trait as it was declared (case-sensitive). The trait name includes the namespace it was declared in (e.g. Foo\Bar
).
__METHOD__
: The class method name. (Added in PHP 5.0.0) The method name is returned as it was declared (case-sensitive).
__NAMESPACE__
: The name of the current namespace (case-sensitive). This constant is defined in compile-time (Added in PHP 5.3.0).
Answer by Maulik patel for Reference - What does this symbol mean in PHP?
Type Operators
instanceof
is used to determine whether a PHP variable is an instantiated object of a certain class.
The above example will output:
bool(true) bool(false)
Reason: Above Example $a
is a object of the mclass
so use only a mclass
data not instance of with the sclass
Example with inheritance
The above example will output:
bool(true) bool(true)
Example with Clone
The above example will output:
bool(true) bool(true)
Answer by Sherif for Reference - What does this symbol mean in PHP?
<=>
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; });
Answer by rajangupta for Reference - What does this symbol mean in PHP?
Spaceship Operator <=>
(Added in PHP 7)
Examples for <=>
Spaceship operator (PHP 7, Source: PHP Manual):
Integers, Floats, Strings, Arrays & objects for Three-way comparison of variables.
// Integers echo 10 <=> 10; // 0 echo 10 <=> 20; // -1 echo 20 <=> 10; // 1 // Floats echo 1.5 <=> 1.5; // 0 echo 1.5 <=> 2.5; // -1 echo 2.5 <=> 1.5; // 1 // Strings echo "a" <=> "a"; // 0 echo "a" <=> "b"; // -1 echo "b" <=> "a"; // 1 echo "a" <=> "aa"; // -1 echo "zz" <=> "aa"; // 1 // Arrays echo [] <=> []; // 0 echo [1, 2, 3] <=> [1, 2, 3]; // 0 echo [1, 2, 3] <=> []; // 1 echo [1, 2, 3] <=> [1, 2, 1]; // 1 echo [1, 2, 3] <=> [1, 2, 4]; // -1 // Objects $a = (object) ["a" => "b"]; $b = (object) ["a" => "b"]; echo $a <=> $b; // 0 $a = (object) ["a" => "b"]; $b = (object) ["a" => "c"]; echo $a <=> $b; // -1 $a = (object) ["a" => "c"]; $b = (object) ["a" => "b"]; echo $a <=> $b; // 1 // only values are compared $a = (object) ["a" => "b"]; $b = (object) ["b" => "b"]; echo $a <=> $b; // 1
Answer by mnv for Reference - What does this symbol mean in PHP?
{}
Curly braces
- Blocks - curly braces/no curly braces?
- Curly braces in string in PHP
- PHP curly braces in array notation
And some words about last post
$x[4] = 'd'; // it works $x{4} = 'd'; // it works $echo $x[4]; // it works $echo $x{4}; // it works $x[] = 'e'; // it works $x{} = 'e'; // does not work $x = [1, 2]; // it works $x = {1, 2}; // does not work echo "${x[4]}"; // it works echo "${x{4}}"; // does not work echo "{$x[4]}"; // it works echo "{$x{4}}"; // it works
Answer by devpro for Reference - What does this symbol mean in PHP?
PHP Strings: PHP Strings can be specified in four ways not just two ways:
1) Single Quote Strings:
$string = 'This is my string'; // print This is my string
2) Double Quote Strings:
$str = 'string'; $string = "This is my $str"; // print This is my string
3) Heredoc:
$string = <<
4) Nowdoc (since PHP 5.3.0):
$string = <<<'END_OF_STRING' This is my string END_OF_STRING; // print This is my string
Answer by John Slegers for Reference - What does this symbol mean in PHP?
An overview of operators in PHP :
Logical Operators :
- $a && $b : TRUE if both $a and $b are TRUE.
- $a || $b : TRUE if either $a or $b is TRUE.
- $a xor $b : TRUE if either $a or $b is TRUE, but not both.
- ! $a : TRUE if $a is not TRUE.
- $a and $b : TRUE if both $a and $b are TRUE.
- $a or $b : TRUE if either $a or $b is TRUE.
Comparison operators :
- $a == $b : TRUE if $a is equal to $b after type juggling.
- $a === $b : TRUE if $a is equal to $b, and they are of the same type.
- $a != $b : TRUE if $a is not equal to $b after type juggling.
- $a <> $b : TRUE if $a is not equal to $b after type juggling.
- $a !== $b : TRUE if $a is not equal to $b, or they are not of the same type.
- $a < $b : TRUE if $a is strictly less than $b.
- $a > $b : TRUE if $a is strictly greater than $b.
- $a <= $b : TRUE if $a is less than or equal to $b.
- $a >= $b : TRUE if $a is greater than or equal to $b.
- $a <=> $b : An integer less than, equal to, or greater than zero when $a is respectively less than, equal to, or greater than $b. Available as of PHP 7.
Arithmetic Operators :
- -$a : Opposite of $a.
- $a + $b : Sum of $a and $b.
- $a - $b : Difference of $a and $b.
- $a * $b : Product of $a and $b.
- $a / $b : Quotient of $a and $b.
- $a % $b : Remainder of $a divided by $b.
- $a ** $b : Result of raising $a to the $b'th power (introduced in PHP 5.6)
Incrementing/Decrementing Operators :
- ++$a : Increments $a by one, then returns $a.
- $a++ : Returns $a, then increments $a by one.
- --$a : Decrements $a by one, then returns $a.
- $a-- : Returns $a, then decrements $a by one.
Bitwise Operators :
- $a & $b : Bits that are set in both $a and $b are set.
- $a | $b : Bits that are set in either $a or $b are set.
- $a ^ $b : Bits that are set in $a or $b but not both are set.
- ~ $a : Bits that are set in $a are not set, and vice versa.
- $a << $b : Shift the bits of $a $b steps to the left (each step means "multiply by two")
- $a >> $b : Shift the bits of $a $b steps to the right (each step means "divide by two")
String Operators :
- $a . $b : Concatenation of $a and $b.
Array Operators :
- $a + $b : Union of $a and $b.
- $a == $b : TRUE if $a and $b have the same key/value pairs.
- $a === $b : TRUE if $a and $b have the same key/value pairs in the same order and of the same types.
- $a != $b : TRUE if $a is not equal to $b.
- $a <> $b : TRUE if $a is not equal to $b.
- $a !== $b : TRUE if $a is not identical to $b.
Assignment Operators :
- $a = $b : The value of $b is assigned to $a
- $a += $b : Same as $a = $a + $b
- $a -= $b : Same as $a = $a - $b
- $a *= $b : Same as $a = $a * $b
- $a /= $b : Same as $a = $a / $b
- $a %= $b : Same as $a = $a % $b
- $a **= $b : Same as $a = $a ** $b
- $a .= $b : Same as $a = $a . $b
- $a &= $b : Same as $a = $a & $b
- $a |= $b : Same as $a = $a | $b
- $a ^= $b : Same as $a = $a ^ $b
- $a <<= $b : Same as $a = $a << $b
- $a >>= $b : Same as $a = $a >> $b
- $a .= $b : Same as $a = $a . $b
Note
and
operator and or
operator have lower precedence than assignment operator =
.
This means that $a = true and false;
is equivalent to ($a = true) and false
.
In most cases you will probably want to use &&
and ||
, which behave in way known from languages like C, Java or JavaScript.
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment