Examples explain the operational symbols used in PHP in detail

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise operators logical operators

Operational symbols

An operator symbol can be used to handle numbers, strings, and other conditions that require comparison operations. PHP's operational symbols and C language operation symbol and very similar, for experienced program designers, should be able to very smoothly grasp the operation of PHP symbols.

Different operational symbols, in fact, there is a priority, like a child learning mathematics, teachers will teach: multiplication and division, and then add and subtract. In the PHP operation Precedence can refer to the table below, in a mixed case, the more down the higher priority.

Left to right or left to right XOR left to right and left to right. = &= |=/=%= ^= = = = = *= left to right? : Left to right | |   Left to right && left to right | left to right ^ left to right & left to right = =!= Limited < <= >= > No << >> left to right +-. Left to right */% left to right! ~ + +--@ right to left [] right to left operation symbol binding rule

As the sage said : Things have always, the end of the beginning, know the priority, then a shortcut, in the operation as long as the operation of the order of precedence written procedures, should not occur results and expectations of different situations. In writing more attention to detail, you can reduce the pain of debugging!

Logical operations

Logical operations (logical operators) are often used to test for true and false values. The most common logical operation is cyclic processing, which is used to determine whether to leave the loop or to continue executing the instructions within the loop.

< less than > greater than <= less than or equal to >= greater than or equal to = = equals!= not equal to && and (and) and and (and) | | either (or) or or (or) XOR exclusive OR (XOR)! No (not)

?
$a = 5;
if ($a!= 5) {
echo "$a not 5";
} else {
echo "$a is 5";
}
?>

PHP bit Operations

PHP's Bit operators (bitwise operators) have a total of six, providing numbers for fast and low order operations. To learn more about bit operations, you can refer to the books on discrete mathematics.

& (And) | or (OR) ^ xor (XOR) << left shift >> right Shift ~ Take 1 's complement symbol meaning

Assignment operations

Assignment Operations (assignment operator) can sometimes make people confused, but it makes the program more streamlined and more efficient.

= Connect the right value to the left = = Add the right value to the Left = Add the right value to the left *= multiply the left value by the right/= the left to the right%= the left to the right. = Add the right string to the left

<?php
$a = 5;
$a + 2; That is $a = $a + 2;
echo $a. " <br>\n ";
$b = "Wow";
$b. = "Ha"; $b = "Wow ha";
$b. = "Ha"; $b = "wow haha";
echo "$b <br>\n";
?>

String operators

The operation symbol for string operation (string operator) is only one, which is the period in English. It can concatenate strings into a new merged string.

The following are examples of string operations

<?php
$a = "PHP 4";
$b = "strong function";
echo $a. ":". $b;
?>

Arithmetic operations

Arithmetic operations (arithmetic operators) symbols, which are used to process arithmetic symbols, are the simplest and most commonly used symbols, especially the processing of numbers, which almost always use the arithmetic notation.

+ addition operation-subtraction operation * Multiplication Operations/Division operations% remainder + + cumulative--diminishing symbolic meaning

The following are examples of simple arithmetic operations

<?php
$a = 8;
$b = 2;
$c = 3;
echo $a + $b. " <br>\n ";
Echo $a-$b. " <br>\n ";
Echo $a * $b. " <br>\n ";
echo $a/$b. " <br>\n ";
echo $a% $c. " <br>\n ";
$a + +;
echo $a. " <br>\n ";
$c--;
Echo $c;
?>

Other operational symbols

In addition to the above operational symbols, there are some operational symbols difficult to classify.

$ variable

The address of the & variable (added in front of the variable)

@ Do not display error messages (added to function)-

Methods or properties of the > class

The element value of the => array

? : Ternary operator

Which is more special is ternary operator? : The following example explains

(EXPR1)? (EXPR2): (EXPR3);

Executes the EXPR2 if the result of the expr1 operation is true; Expr3. In fact, it has a sort of if...else loop, but it can make the program more streamlined and efficient.



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.