PHP operator (i) An example of "arithmetic operator" explained

Source: Internet
Author: User
What is the PHP arithmetic operator?

Before we introduce arithmetic operators , let's look at what operators are, which are symbols used to perform program code operations, often used to evaluate variables, constants, or data, which is a specified operation on a value or a set of values. PHP operators: Arithmetic operators, string operators, assignment operators, bitwise operators, logical operators, comparison operators, increment or decrement operators, error control operators. In the back we will explain to you separately, let us first explain the arithmetic operators in the PHP operator.

Introduction to the various symbols of arithmetic operators:

Arithmetic operators are symbols that handle arithmetic operations. The most commonly used arithmetic operators in the processing of numbers are the following table

Operator Description Example
+ Addition operation $a + $b
- Subtraction operations $a-$b
* Multiplication operations $a * $b
/ Division operation $a/$b
% Take the remainder operation $a% $b
++ Increment operation $a ++,++ $a
-- Decrement operation $a--,--$a

PS: Use the% remainder in arithmetic operators, and if the divisor ($a) is negative, the result is a negative value.

This is to say the last two increment/decrement operators, which are mainly operated on a single variable.

There are two ways to use the increment/decrement operator:

The first is to increase or decrease the variable by 1 before assigning the value to the original variable, which is called the forward increment/decrement operator.

The second type is to put the operator after the variable, return the current value of the variable, and then increase or decrease the current value of the variable by one, which we call the post increment/decrement operator

Arithmetic operator Usage instance

The following examples are calculated using several operators in the table above, with the following code:

<?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 "; + + $a; echo $a." <br>\n "; $c--;echo $c;? >

Running results such as

The above example is our simple application of arithmetic operators, and more on the PHP operator topic . In the next section we will describe in detail the concept and usage of string operators.

Related Video Tutorials recommended:

php.cn (4)-php video tutorial: operator (i): arithmetic operator, string operator, assignment operator

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.