php method to convert a positive number into a negative number: 1. Define a positive number variable and assign it "$num=positive value;", use the multiplication operator "*" to combine the positive number with "-1" Just multiply, the syntax is "$num=$num* (-1)". 2. Define a positive number variable and assign it "$num=positive value;". Use the multiplication assignment operator "*=" to multiply the positive number and "-1". The syntax is "$num *= -1;" .
The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer
In PHP, you can use multiplication operations. Multiply by -1
to turn it into a negative number.
There are two ways to implement multiplication:
Use the multiplication operator "*"
Use the multiplication assignment operation Character "*="
Method 1. Use the multiplication operator "*"
Just use the multiplication operator "*" to convert Positive numbers
and -1
can be multiplied.
$num*(-1);
Example:
<?php header("Content-type:text/html;charset=utf-8"); $number = 99; echo "原正数:".$number."<br>"; $number = $number*(-1); echo "转换后的负数:".$number; //输出 -99 ?>
##Method 2: Use the multiplication assignment operator "*="
The "*=" operator can perform multiplication and addition operations first, and then assign the result to the variable on the left side of the operator. Syntax for converting positive numbers to negative numbers:$num *= -1;
<?php header("Content-type:text/html;charset=utf-8"); $number = 88; echo "原正数:".$number."<br>"; $number *= -1; echo "转换后的负数:".$number; //输出 -88 ?>
PHP Video Tutorial 》
The above is the detailed content of How to convert positive numbers to negative numbers in php. For more information, please follow other related articles on the PHP Chinese website!