[Original] |, & operator similarities and differences in Js and PHP

Source: Internet
Author: User
[Original] javascript and PHP |, & operator similarities and differences in learning javascript, found logic and &, logic or | these two operators are very special in variable assignment, different from the familiar PHP!

1. condition statements

Condition statements, such as if judgment, are the same as and OR and have no objection:

(1) | when one of the conditions is true, the statement is executed.

(2) & if all conditions must be true, the statement is executed.

2. variable assignment

PHP:

$ Person = array (); $ param = $ arr ['name'] | 'Bom '; var_dump ($ param); // true, in PHP, the logic or condition determines the nature. the variable is a Boolean value var_dumo ($ param); // false. in PHP, the logic and condition determines the nature, A variable is a Boolean value.

The logic and or understanding of JS are as follows:

(1) | indicates that if the left value is true, the left value is returned. Otherwise, the right value is returned.

(2) & indicates that if true is returned on the left side, the right side is returned. Otherwise, the left side is returned.

What is the purpose? It can prevent the typeError exception from being thrown when the variable is assigned, or it can be used as the default value of the variable, for example:

Var person = {}; // empty object var undefinedRes = person. name; // undefined, attempts to retrieve a nonexistent member element and returns undefinedvar typeErrorRes = person. name. age; // typeError. if you try to retrieve an undefined value, the typeError exception var status = person. name | "Bom"; // Bom, with | use the default Bom. Person. name returns false, so take the logical or right value var name = person. name & "Bom"; // undefined, person. name returns false, so the logical and left values are used.

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.