abs() : absolute value

echo abs(-6.2); // Output 6.2
echo "<br>";
echo abs(-4); // Output 4
echo "<br>";
echo abs(3.2); // Output 3.2
echo "<br>";
echo abs(7); // Output 7

Changing all negative numbers of an array

We can take an array with both negative and positive numbers. We will use array_map() built in PHP function to change all negative elements to positive by using abs function.
<?Php
$a= array(1,-2,4,-7.3,105); 
$b=array_map("abs",$a); 
while (list ($key, $val) = each ($b)) { 
echo "$key -> $val <br>"; 
}
?>
Output is here.
0 -> 1 
1 -> 2 
2 -> 4 
3 -> 7.3 
4 -> 105

Example: Handling Negative Numbers

$num = -15;
echo abs($num);  // Output: 15

Example: Calculating Difference Between Two Numbers

$num1 = 25;
$num2 = 50;
echo abs($num1 - $num2);  // Output: 25

Questions


Math Functions pow() to get exponential value
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer