Php assignment operator

Source: Internet
Author: User
Tags bitwise operators php tutorial

Php Tutorial assignment operator
The basic assignment operator is "= ". At first, we may think it is "equal", but it is not. It actually means assigning the value of the right expression to the number of operations on the left.

The value of the value assignment expression is the value assigned. That is to say, the value of "$ a = 3" is 3. In this way, you can do some tips:

<? Php

$ A = ($ B = 4) + 5; // $ a is now 9, while $ B is 4.

?>

    

<? Php
$ A = 10;
$ B = 20;
Echo $ a + = $ B; // equivalent to $ a = $ a + $ B 10 + 20 = 30
Echo "<br/> ";

Echo $ a-= $ B; // equivalent to $ a = $ a-$ B 30-20 = 10
Echo "<br/> ";

Echo $ a * = $ B; // equivalent to $ a = $ a * $ B 10*20 = 200
Echo "<br/> ";

Echo $ a/= $ B; // equivalent to $ a = $ a/$ B 200/20 = 10
Echo "<br/> ";

Echo $ a % = $ B; // equivalent to $ a = $ a % $ B 10% 20 = 10
Echo "<br/> ";

// See the String Operators page
Echo $ a. = $ B; // equivalent to $ a = $ a. $ B 10.20 = 1020
Echo "<br/> ";

$ A = true;
$ B = false;
// See the Bitwise Operators page
Echo $ a & = $ B; // equivalent to $ a = $ a & $ B 0 true & false = false
Echo "<br/> ";

Echo $ a | = $ B; // equivalent to $ a = $ a | $ B 0 false | false = false
Echo "<br/> ";

Echo $ a ^ = $ B; // equivalent to $ a = $ a ^ $ B 0 false ^ false = false
Echo "<br/> ";

$ A = 1;
$ B = 2;
Echo $ a <= $ B; // equivalent to $ a = $ a <$ B 1 <2 = 4
Echo "<br/> ";

Echo $ a >=$ B; // equivalent to $ a =a >>>4 B 4 >> 2 = 1
Echo "<br/> ";
?>
   
In addition to the basic value assignment operators, there are also "combined operators" suitable for all binary arithmetic, array sets, and string operators ", in this way, you can use its value in an expression and assign the result of the expression to it. For example:

<? Php

$ A = 3;
$ A + = 5; // sets $ a to 8, as if we had said: $ a = $ a + 5;
$ B = "Hello ";
$ B. = "There! "; // Sets $ B to" Hello There! ", Just like $ B = $ B." There! ";

?>

Note that the value assignment operation copies the value of the original variable to the new variable (pass the value assignment), so changing one does not affect the other. This is also suitable for copying some values such as large arrays in intensive loops. You can also assign values by reference using the $ var = & $ othervar; syntax. Referencing a value assignment means that both variables point to the same data without any data copies. For more information about references, see references. In PHP 5, objects are always assigned by reference unless the new clone keyword is explicitly used.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.