Home >Backend Development >PHP Problem >How to convert positive numbers to negative numbers in php

How to convert positive numbers to negative numbers in php

青灯夜游
青灯夜游Original
2023-01-07 15:01:444672browse

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

How to convert positive numbers to negative numbers in php

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
?>

How to convert positive numbers to negative numbers in php

##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;

Example:

<?php
header("Content-type:text/html;charset=utf-8");
$number = 88;
echo "原正数:".$number."<br>";
$number *= -1;
echo "转换后的负数:".$number;   //输出 -88
?>

How to convert positive numbers to negative numbers in php

Recommended learning: "

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!

Statement:
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