Php string or operator

PHP Operators

Operators are used to perform operations on variables and values.

PHP divides the operators in the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • String operators
  • Array operators
  • Conditional assignment operators

PHP Arithmetic Operators

The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.

Operator Name Example Result Show it
+ Addition $x + $y Sum of $x and $y Try it »
Subtraction $x — $y Difference of $x and $y Try it »
* Multiplication $x * $y Product of $x and $y Try it »
/ Division $x / $y Quotient of $x and $y Try it »
% Modulus $x % $y Remainder of $x divided by $y Try it »
** Exponentiation $x ** $y Result of raising $x to the $y’th power Try it »

PHP Assignment Operators

The PHP assignment operators are used with numeric values to write a value to a variable.

The basic assignment operator in PHP is «=». It means that the left operand gets set to the value of the assignment expression on the right.

Assignment Same as. Description Show it
x = y x = y The left operand gets set to the value of the expression on the right Try it »
x += y x = x + y Addition Try it »
x -= y x = x — y Subtraction Try it »
x *= y x = x * y Multiplication Try it »
x /= y x = x / y Division Try it »
x %= y x = x % y Modulus Try it »
Читайте также:  Думать html is on

PHP Comparison Operators

The PHP comparison operators are used to compare two values (number or string):

Operator Name Example Result Show it
== Equal $x == $y Returns true if $x is equal to $y Try it »
=== Identical $x === $y Returns true if $x is equal to $y, and they are of the same type Try it »
!= Not equal $x != $y Returns true if $x is not equal to $y Try it »
<> Not equal $x <> $y Returns true if $x is not equal to $y Try it »
!== Not identical $x !== $y Returns true if $x is not equal to $y, or they are not of the same type Try it »
> Greater than $x > $y Returns true if $x is greater than $y Try it »
Less than $x < $y Returns true if $x is less than $y Try it »
>= Greater than or equal to $x >= $y Returns true if $x is greater than or equal to $y Try it »
Less than or equal to $x Returns true if $x is less than or equal to $y Try it »
Spaceship $x $y Returns an integer less than, equal to, or greater than zero, depending on if $x is less than, equal to, or greater than $y. Introduced in PHP 7. Try it »

PHP Increment / Decrement Operators

The PHP increment operators are used to increment a variable’s value.

The PHP decrement operators are used to decrement a variable’s value.

Operator Name Description Show it
++$x Pre-increment Increments $x by one, then returns $x Try it »
$x++ Post-increment Returns $x, then increments $x by one Try it »
—$x Pre-decrement Decrements $x by one, then returns $x Try it »
$x— Post-decrement Returns $x, then decrements $x by one Try it »

PHP Logical Operators

The PHP logical operators are used to combine conditional statements.

Operator Name Example Result Show it
and And $x and $y True if both $x and $y are true Try it »
or Or $x or $y True if either $x or $y is true Try it »
xor Xor $x xor $y True if either $x or $y is true, but not both Try it »
&& And $x && $y True if both $x and $y are true Try it »
|| Or $x || $y True if either $x or $y is true Try it »
! Not !$x True if $x is not true Try it »

PHP String Operators

PHP has two operators that are specially designed for strings.

Operator Name Example Result Show it
. Concatenation $txt1 . $txt2 Concatenation of $txt1 and $txt2 Try it »
.= Concatenation assignment $txt1 .= $txt2 Appends $txt2 to $txt1 Try it »

PHP Array Operators

The PHP array operators are used to compare arrays.

Operator Name Example Result Show it
+ Union $x + $y Union of $x and $y Try it »
== Equality $x == $y Returns true if $x and $y have the same key/value pairs Try it »
=== Identity $x === $y Returns true if $x and $y have the same key/value pairs in the same order and of the same types Try it »
!= Inequality $x != $y Returns true if $x is not equal to $y Try it »
<> Inequality $x <> $y Returns true if $x is not equal to $y Try it »
!== Non-identity $x !== $y Returns true if $x is not identical to $y Try it »

PHP Conditional Assignment Operators

The PHP conditional assignment operators are used to set a value depending on conditions:

Operator Name Example Result Show it
?: Ternary $x = expr1 ? expr2 : expr3 Returns the value of $x.
The value of $x is expr2 if expr1 = TRUE.
The value of $x is expr3 if expr1 = FALSE
Try it »
?? Null coalescing $x = expr1 ?? expr2 Returns the value of $x.
The value of $x is expr1 if expr1 exists, and is not NULL.
If expr1 does not exist, or is NULL, the value of $x is expr2.
Introduced in PHP 7
Try it »

Источник

Php string or operator

There are two string operators. The first is the concatenation operator (‘.’), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator (‘ .= ‘), which appends the argument on the right side to the argument on the left side. Please read Assignment Operators for more information.

$a = «Hello » ;
$b = $a . «World!» ; // now $b contains «Hello World!»

$a = «Hello » ;
$a .= «World!» ; // now $a contains «Hello World!»
?>

See Also

User Contributed Notes 6 notes

As for me, curly braces serve good substitution for concatenation, and they are quicker to type and code looks cleaner. Remember to use double quotes (» «) as their content is parced by php, because in single quotes (‘ ‘) you’ll get litaral name of variable provided:

// This works:
echo «qwe < $a >rty» ; // qwe12345rty, using braces
echo «qwe» . $a . «rty» ; // qwe12345rty, concatenation used

// Does not work:
echo ‘qwerty’ ; // qwerty, single quotes are not parsed
echo «qwe $arty » ; // qwe, because $a became $arty, which is undefined

A word of caution — the dot operator has the same precedence as + and -, which can yield unexpected results.

The above will print out «3» instead of «Result: 6», since first the string «Result3» is created and this is then added to 3 yielding 3, non-empty non-numeric strings being converted to 0.

To print «Result: 6», use parantheses to alter precedence:

» < $str1 >< $str2 > < $str3 >» ; // one concat = fast
$str1 . $str2 . $str3 ; // two concats = slow
?>
Use double quotes to concat more than two strings instead of multiple ‘.’ operators. PHP is forced to re-concatenate with every ‘.’ operator.

If you attempt to add numbers with a concatenation operator, your result will be the result of those numbers as strings.

echo «thr» . «ee» ; //prints the string «three»
echo «twe» . «lve» ; //prints the string «twelve»
echo 1 . 2 ; //prints the string «12»
echo 1.2 ; //prints the number 1.2
echo 1 + 2 ; //prints the number 3

Some bitwise operators (the and, or, xor and not operators: & | ^ ~ ) also work with strings too since PHP4, so you don’t have to loop through strings and do chr(ord($s[i])) like things.

See the documentation of the bitwise operators: https://www.php.net/operators.bitwise

Be careful so that you don’t type «.» instead of «;» at the end of a line.

It took me more than 30 minutes to debug a long script because of something like this:

The output is «axbc», because of the dot on the first line.

  • Operators
    • Operator Precedence
    • Arithmetic Operators
    • Assignment Operators
    • Bitwise Operators
    • Comparison Operators
    • Error Control Operators
    • Execution Operators
    • Incrementing/Decrementing Operators
    • Logical Operators
    • String Operators
    • Array Operators
    • Type Operators

    Источник

    PHP OR Operator

    Summary: in this tutorial, you’ll learn about the PHP OR operator (||) and how to use it to build complex logical expressions.

    Introduction to the PHP OR operator

    The logical OR operator accepts two operands and returns true if either operand is true; otherwise, it returns false . In other words, the logical OR operator returns false if both operands are false .

    To represent the logical OR operator, PHP uses either the or keyword or the || as follows:

    expression1 or expression2

    The following table illustrates the result of the or operator:

    expression1 expression2 expression1 || expression2
    true true true
    true false true
    false true true
    false false false

    Note that the or , Or , and OR are the same because PHP keywords are case-insensitive.

    The || and or operators return the same result. The only difference between the || and or operators are their precedences. The or operator has higher precedence than the || operator.

    PHP OR operator examples

    Suppose that you need to clear the cache of the website if the flag $exprired or $purge is set to true . To do that, you can use the logical OR operator as follows:

     $expired = true; $purged = false; $clear_cache = $expired || $purged; var_dump($clear_cache);Code language: HTML, XML (xml)
    bool(true)Code language: JavaScript (javascript)

    Since $expired is true , the result of the OR operator is also true .

    However, if you change the $expired to false , the result will be false as shown in the following example:

     $expired = false; $purged = false; $clear_cache = $expired || $purged; var_dump($clear_cache);Code language: HTML, XML (xml)

    In practice, you often use the logical OR operator in the if, if-else, if-elseif, while, and do-while statements.

    Short-circuiting

    When the first operand is true , the logical OR operator knows that the result must be also true . In this case, it doesn’t evaluate the second operand. This process is called short-circuiting.

    In practice, you often find that the or operator is used in the following pattern:

    function_call() || die(message)Code language: PHP (php)

    If the function_call() returns true , it succeeded. PHP will never execute the second operand which is a call to the die() function. Otherwise, PHP will call the die() function with an error message.

     function connect_to_db() < return false; > connect_to_db() || die('Cannot connect to the database.'); Code language: HTML, XML (xml)
    Cannot connect to the database

    In this example, the connect_to_db() function returns false , PHP calls the die() function that shows the error message.

    The PHP OR gotchas

    See the following example:

     $result = false or true; var_dump($result); Code language: HTML, XML (xml)
    bool(false)Code language: JavaScript (javascript)

    In this example, you would expect that the $result is true because false or true expression returns true. However, it is not the case.

    When evaluating the following statement:

    $result = false or true;Code language: PHP (php)

    PHP evaluates the $result = false first and then the or operator second because the = operator has higher precedence than the or operator.

    Notice that each operator has precedence. And PHP will evaluate the operators with the higher precedence before the ones with the lower precedence.

    Technically, it is equivalent to the following:

    ($result = false) or true;Code language: PHP (php)

    Therefore, $result is assigned the false value.

    To fix this, you need to use parentheses to change the order of evaluation:

     $result = (false or true); var_dump($result);Code language: HTML, XML (xml)
    bool(true)Code language: JavaScript (javascript)

    Or you can use the || operator:

     $result = false || true; var_dump($result); Code language: HTML, XML (xml)
    bool(true)Code language: JavaScript (javascript)

    Therefore, it’s a good practice to always use the || operator instead of the or operator.

    Summary

    • Use the PHP OR operator ( or , || ) to combine two expressions and returns true if either expression is true ; otherwise, it returns false .
    • The logical OR operator is short-circuiting.
    • Do use the || operator instead of the or operator.

    Источник

Оцените статью