Php array operator (+), string operator (.), logical operator (& AND | or xor)

Source: Internet
Author: User
Tags bool logical operators

Php array operators


$ A + $ B join $ a and $ B.
$ A = $ B is equal. If $ a and $ B have the same key/value pair, the value is TRUE.
$ A ===$ B. TRUE if $ a and $ B have the same key/value pairs and the order and type are the same.
$! = $ B: TRUE if $ a is not equal to $ B.
$ A <> $ B: TRUE if $ a is not equal to $ B.
$! = $ B incomplete. If $ a is not all equal to $ B, TRUE is returned.

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

The code is as follows: Copy code

$ A = array ("a" => "apple", "B" => "banana ");
$ B = array ("a" => "pear", "B" => "strawberry", "c" => "cherry ");

$ C = $ a + $ B;

Var_dump ($ c );

After the script is executed, the script displays:

The code is as follows: Copy code

Array (3 ){
["A"] =>
String (5) "apple"
["B"] =>
String (6) "banana"
["C"] =>
String (6) "cherry"
}

If the elements in the array have the same key name and value, the comparison is equal.


Example #1 compare arrays

The code is as follows: Copy code

<? 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 is the concatenation operator ("."), which returns the string after the left and right parameters are connected. The second is to connect the value assignment operator (". ="), which attaches the right parameter to the parameter on the left

The code is as follows: Copy code

<? Php

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

?>

Example

The code is as follows: Copy code

<? Php

$ A = '000000 ';

// 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 are not parsed
Echo "qwe $ arty"; // qwe, because $ a became $ arty, which is undefined

?>


Logical operators

Example of logical operators in the following table

$ A and $ B And (logical and) TRUE, if both $ a And $ B are TRUE.
$ A or $ B Or (logical or) TRUE, if $ a Or $ B is TRUE.
$ A xor $ B Xor (logical exclusive or) TRUE. If either $ a or $ B is TRUE, but not both.
! $ A Not (logical Not) TRUE, if $ a is Not TRUE.
$ A & $ B And (logical And) TRUE, if both $ a And $ B are TRUE.
$ A | $ B Or (logical Or) TRUE. If $ a Or $ B is set to TRUE.

"And" and "or" have two different operators because they have different operation priorities.

Example

The code is as follows: Copy code

<? Php

($ A = $ _ GET ['var']) | ($ a = 'a default ');

?>

Example

The code is as follows: Copy code

<? Php

// The following foo () will not be called because they are short-circuited by the operator.
$ A = (false & foo ());
$ B = (true | foo ());
$ C = (false and foo ());
$ D = (true or foo ());

// "|" Has a higher priority than "or"
$ E = false | true; // $ e is assigned (false | true) and returns true.
$ F = false or true; // $ f is assigned to false. [Altair note: "=" has a higher priority than "or"]
Var_dump ($ e, $ f );

// "&" Has a higher priority than "and"
$ G = true & false; // $ g is assigned to (true & false) and the result is false.
$ H = true and false; // $ h is assigned true [Altair note: "=" priority is higher than "and"]
Var_dump ($ g, $ h );
?>

The output of the preceding routine is similar:

Bool (true)
Bool (false)
Bool (false)
Bool (true)

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.