3 ways to force a negative number to be converted into a positive number: 1. Use the inversion operation "-" and the syntax "-$number"; 2. Use the "*" or "*=" operator to convert the original number to a positive number. Multiply the number by "-1", the syntax is "$number*(-1)" or "$number *= -1;"; 3. Use the abs() function to get the absolute value of the original number, the syntax is "abs($number) ".
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
php forces negative numbers to Method of converting to a positive number
Method 1: Use the negation operation
The negation operator is a unary operator, also called unary subtraction operator.
<?php header("Content-type:text/html;charset=utf-8"); $number = -11; echo "原负数:".$number."<br>"; $result = -$number; echo "转为正数后:".$result; ?>
Method 2: Use the "*" or "*=" operator to multiply the original number by "-1"
<?php header("Content-type:text/html;charset=utf-8"); $number = -12; echo "原负数:".$number."<br>"; $result = $number*(-1); echo "转为正数后:".$result."<br>"; $number *= -1; echo "转为正数后:".$number; ?>
Method 3: Use the abs() function to get the absolute value
The abs() function returns the absolute value of a number.
<?php header("Content-type:text/html;charset=utf-8"); $number = -13; echo "原负数:".$number."<br>"; $result = abs($number); echo "转为正数后:".$result."<br>"; ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to force conversion of negative numbers to positive numbers in php. For more information, please follow other related articles on the PHP Chinese website!