An explanation of assignment operators in PHP

Source: Internet
Author: User
PHP assignment operator The PHP assignment operator is used to write values to variables. The base assignment operator in PHP is "=". This means that the right-hand assignment expression sets the value for the left-hand operand.

The value of the value operation expression is the assigned value. In other words, the value of "$a = 3" is 3. This allows you to do some tips:

<?php$a  = ($b  =  4) +  5;  $a is now 9, and $b is 4.?>

For array arrays, assigning a key with a name is the "= =" operator. This operator has the same precedence as other assignment operators.

In addition to the basic assignment operator, there are "combined operators" that are appropriate for all two-tuple arithmetic, array collections, and string operators, so that 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 assignment operation copies the value of the original variable into a new variable (value assignment), so changing one does not affect the other. This is also suitable for copying some values such as large arrays in dense loops.

An exception to the normal value assignment behavior in PHP is when you encounter object objects, which are assigned by reference in PHP 5 unless you explicitly use the Clone keyword to copy it.

Reference assignment

PHP supports reference assignment, using "$var = & $othervar;" Grammar. A reference assignment means that two variables point to the same data and nothing is copied.

Example #1 Reference Assignment

<?php$a  =  3; $b  = & $a;  $b is $a reference to print  "$a \ n";  Output 3print  "$b \ n";  Output 3$a  =  4;  Modify $aprint  "$a \ n";  Output 4print  "$b \ n";  Also output 4, because $b is a reference to $a and therefore is also changed?>

Since PHP 5, the new operator automatically returns a reference, so a reference assignment to the new result will issue a e_deprecated error message in PHP 5.3 and later, with a e_strict error message issued in the previous version.

For example, the following code generates a warning:

<?phpclass  c  {} $o  = &new  c;? >

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.