Home > Backend Development > PHP Problem > How to force conversion of negative numbers to positive numbers in php

How to force conversion of negative numbers to positive numbers in php

青灯夜游
Release: 2023-03-16 12:56:02
Original
1967 people have browsed it

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) ".

How to force conversion of negative numbers to positive numbers in php

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;
?>
Copy after login

How to force conversion of negative numbers to positive numbers in php

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;
?>
Copy after login

How to force conversion of negative numbers to positive numbers in php

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>";
?>
Copy after login

How to force conversion of negative numbers to positive numbers in php

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template