PHP - Maths Functions
Hello, aspiring programmers! Today, we're going to dive into the exciting world of PHP mathematical functions. Don't worry if you've never written a line of code before - we'll start from the basics and work our way up. By the end of this tutorial, you'll be manipulating numbers like a pro!
PHP abs() Function
Let's start with something simple. Have you ever needed to find the absolute value of a number? That's where the abs()
function comes in handy.
<?php
$number = -15;
echo abs($number); // Output: 15
?>
In this example, we have a negative number (-15), but abs()
gives us its positive equivalent. It's like turning frowns upside down in the number world!
PHP ceil() Function
Next up is the ceil()
function. Think of it as a pessimistic elevator - it always goes up to the next floor.
<?php
$price = 4.3;
echo ceil($price); // Output: 5
?>
Here, even though our price is 4.3, ceil()
rounds it up to 5. Useful when you're calculating prices and don't want to shortchange yourself!
PHP exp() Function
Now, let's get a bit more advanced with the exp()
function. This calculates the exponential of e (Euler's number).
<?php
$x = 2;
echo exp($x); // Output: approximately 7.3890560989307
?>
Don't worry if this seems abstract - it's mostly used in scientific and financial calculations. Just remember, it's like e is doing push-ups: e^x!
PHP floor() Function
The floor()
function is like the opposite of ceil()
. It's an optimistic elevator that always goes down to the lower floor.
<?php
$score = 85.9;
echo floor($score); // Output: 85
?>
In this case, even though the student almost got an 86, floor()
keeps it at 85. Tough luck, kiddo!
PHP intdiv() Function
intdiv()
performs integer division and returns the quotient. It's like the strict teacher of division - no decimal points allowed!
<?php
$total = 17;
$people = 5;
echo intdiv($total, $people); // Output: 3
?>
If you're splitting 17 candies among 5 people, each person gets 3 candies. The remaining 2? Well, that's for another function to handle!
PHP log10() Function
The log10()
function calculates the base-10 logarithm of a number. It's like asking, "10 to what power gives me this number?"
<?php
$number = 100;
echo log10($number); // Output: 2
?>
Because 10^2 = 100. See? Math can be logical!
PHP max() Function
max()
finds the highest value in a list of numbers. It's like picking the tallest person in a group photo.
<?php
echo max(5, 8, 3, 12, 9); // Output: 12
?>
In this lineup, 12 stands tallest!
PHP min() Function
Conversely, min()
finds the lowest value. It's like finding the shortest person in that same group photo.
<?php
echo min(5, 8, 3, 12, 9); // Output: 3
?>
Poor 3, always picked last for basketball...
PHP pow() Function
pow()
raises a number to a specified power. It's like a number doing push-ups!
<?php
echo pow(2, 3); // Output: 8
?>
Here, 2 is doing 3 push-ups, so we get 2 2 2 = 8.
PHP round() Function
round()
is the fair judge of numbers. It rounds a number to the nearest integer.
<?php
echo round(3.4); // Output: 3
echo round(3.5); // Output: 4
?>
3.4 gets rounded down, but 3.5 makes the cut and gets rounded up. It's all about that .5 threshold!
PHP sqrt() Function
sqrt()
calculates the square root of a number. It's like asking, "What number, when multiplied by itself, gives me this number?"
<?php
echo sqrt(9); // Output: 3
?>
Because 3 * 3 = 9. Elementary, my dear Watson!
Predefined Mathematical Constants
PHP also provides some predefined mathematical constants. Let's look at a few:
<?php
echo M_PI; // Outputs: 3.1415926535898
echo M_E; // Outputs: 2.7182818284590
echo M_SQRT2; // Outputs: 1.4142135623731
?>
These constants save you from having to remember or calculate these values yourself. It's like having a mini-mathematician in your code!
Now, let's summarize all these functions in a handy table:
Function | Description | Example |
---|---|---|
abs() | Returns absolute value | abs(-15) = 15 |
ceil() | Rounds up to nearest integer | ceil(4.3) = 5 |
exp() | Calculates exponential of e | exp(2) ≈ 7.389 |
floor() | Rounds down to nearest integer | floor(85.9) = 85 |
intdiv() | Integer division | intdiv(17, 5) = 3 |
log10() | Calculates base-10 logarithm | log10(100) = 2 |
max() | Returns highest value | max(5,8,3,12,9) = 12 |
min() | Returns lowest value | min(5,8,3,12,9) = 3 |
pow() | Raises to specified power | pow(2, 3) = 8 |
round() | Rounds to nearest integer | round(3.5) = 4 |
sqrt() | Calculates square root | sqrt(9) = 3 |
And there you have it! You've just taken your first steps into the world of PHP mathematical functions. Remember, practice makes perfect, so don't be afraid to experiment with these functions in your own code. Before you know it, you'll be calculating and manipulating numbers with ease. Happy coding!
Credits: Image by storyset