How to Convert a Positive Number to Negative in PHP Using Multiplication
This article explains how to convert a positive integer to a negative one in PHP by using either the multiplication operator (*) or the multiplication‑assignment operator (*=), and provides clear code examples for both methods.
In PHP, you can turn a positive number into a negative one by multiplying it by -1.
There are two ways to perform this multiplication: using the * operator or the multiplication‑assignment operator *=.
Method 1 – using the * operator : multiply the variable by -1 directly.
<?php
header("Content-type:text/html;charset=utf-8");
$number = 99;
echo "原正数:" . $number . "<br>";
$number = $number * (-1);
echo "转换后的负数:" . $number; // 输出 -99
?>Method 2 – using the *= operator : the operator performs the multiplication and assigns the result back to the variable.
<?php
header("Content-type:text/html;charset=utf-8");
$number = 88;
echo "原正数:" . $number . "<br>";
$number *= -1;
echo "转换后的负数:" . $number; // 输出 -88
?>Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
