Examples of PHP array operators, string operators, and logical operators

Source: Internet
Author: User
PHP Array Operators

$a + $b Union of Joint $a and $b.
$a = = $b equal if the $a and $b have the same key/value pairs true.
$a = = = $b congruent if $a and $b have the same key/value pairs and the order and type are the same, TRUE.
$a! = $b is TRUE if the $a does not equal $b.
$a <> $b If the $a is not equal to $b true.
$a!== $b Not equal to TRUE if the $a is not all equals $b.

The only one array operator in PHP is the + operator. It appends the right array to the left array, but the duplicate key values are not overwritten.

$a = Array ("A" and "Apple", "B" and "banana"); $b = Array ("A" = "pear", "b" = "Strawberry", "c" = "cherry"); $c = $a + $b; Var_dump ($c);

Once executed, this script will display:

Array (3) {  ["a"]=>  string (5) "Apple"  ["B"]=>  string (6) "Banana"  ["C"]=>  string (6 ) "Cherry"}

The cells in the array are compared when they have the same key name and value.

<?php$a = Array ("Apple", "banana"), $b = Array (1 = "banana", "0" = "apple"), var_dump ($a = = $b); BOOL (true) var_dump ($a = = = $b); BOOL (FALSE)?>

String operators

There are two string operators. The first one is the join operator (".") ), which returns the string after its left and right arguments are concatenated. The second is the Join assignment operator (". ="), which attaches the right argument to the left argument

<?phpecho "THR". " EE ";           Prints the string "three" echo "TWE". "Lve";        Prints the string "Twelve" Echo 1. 2;                Prints the string "a" echo 1.2;                  Prints the number 1.2echo 1+2;                  Prints the number 3?>
<?php $a = ' 12345 ';//This Works:echo "qwe{$a}rty"; Qwe12345rty, using braces echo "Qwe". $a. "Rty"; Qwe12345rty, concatenation used//Does not Work:echo ' qwe{$a}rty '; qwe{$a}rty, single quotes is not parsed echo "Qwe$arty"; Qwe, because $a became $arty, which is undefined?>

logical operators

$a and $b and (logical AND) true if both $a and $b are true.
$a or $b or (logical OR) true if either $a or $b is true.
$a XOR $b xor (Logical XOR) True if $a or $b any one is true, but not both.
! $a not (logical not) True if $a is not true.
$a && $b and (logical AND) true if $a and $b are true.
$a | | $b or (logical OR) true if either $a or $b is true.

There are two different form operators for "and" and "or" because their operations have different precedence.

<?php ($a = $_get[' var ') | | ($a = ' a default ');? >
<?php//foo () below is not called because they are "shorted" by the operator. $a = (false && foo ()), $b = (true  | | foo ()), $c = (False and foo ()), $d = (true  or  foo ());//"| |" Priority ratio " or "high $e = False | | True The $e is assigned a value of (false | | true) and the result is True$f = False or true; $f is assigned false [Altair NOTE: "=" has a higher priority than "or"]var_dump ($e, $f);//"&&" has a higher priority than "and" $g = True && false; The $G is assigned a value of (true && false), and the result is False$h = True and false; $h is assigned true [Altair NOTE: "=" has a higher priority than "and"]var_dump ($g, $h);? >

The output of the above routines is similar to the following:

BOOL (TRUE) bool (FALSE) bool (FALSE) bool (true)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.