Introduction:
Explore the world of PHP math functions with our comprehensive lesson. From basic arithmetic operations to advanced math and trigonometric functions, this tutorial will guide you through the essential concepts and practical examples. Boost your PHP skills and empower your web development projects with mathematical prowess.
PHP Math
PHP (Hypertext Preprocessor) is a server-side scripting language commonly used for web development. It includes a variety of built-in functions for performing mathematical operations. Here are some common PHP math functions:
Addition: +
Subtraction: –
Multiplication: *
Division: /
Modulus (remainder): %
Example:
$sum = 5 + 3; // 8 $difference = 7 - 2; // 5 $product = 4 * 6; // 24 $quotient = 10 / 2; // 5 $remainder = 11 % 3; // 2
abs(): Returns the absolute value of a number.
sqrt(): Returns the square root of a number.
pow(): Raises a number to the power of another.
exp(): Returns the value of ‘e’ raised to the power of a number.
log(): Returns the natural logarithm of a number.
Example:
$absolute = abs(-10); // 10 $squareRoot = sqrt(25); // 5 $power = pow(2, 3); // 8 (2 raised to the power of 3) $exponential = exp(2); // 7.389 (e raised to the power of 2) $logarithm = log(10); // 2.302 (natural logarithm of 10)
sin(): Sine function.
cos(): Cosine function.
tan(): Tangent function.
asin(): Arcsine function.
acos(): Arccosine function.
atan(): Arctangent function.
Example:
$angle = deg2rad(45); // Convert degrees to radians $sinValue = sin($angle); // 0.707 (sin of 45 degrees) $cosValue = cos($angle); // 0.707 (cos of 45 degrees) $tanValue = tan($angle); // 1 (tan of 45 degrees)
Basic Arithmetic:complete example in html with explanation
This example will create a web page with a form where s can input two numbers, select an operation (addition, subtraction, multiplication, or division), and then display the result.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Basic Arithmetic Calculator</title> </head> <body> <h1>Basic Arithmetic Calculator</h1> <form method="post"> <label for="num1">Enter Number 1:</label> <input type="text" name="num1" required> <label for="num2">Enter Number 2:</label> <input type="text" name="num2" required> <label for="operation">Select Operation:</label> <select name="operation" required> <option value="addition">Addition</option> <option value="subtraction">Subtraction</option> <option value="multiplication">Multiplication</option> <option value="division">Division</option> </select> <button type="submit">Calculate</button> </form> <?php // Check if the form is submitted if ($_SERVER["REQUEST_METHOD"] == "POST") { // Retrieve values from the form $num1 = $_POST["num1"]; $num2 = $_POST["num2"]; $operation = $_POST["operation"]; // Perform the selected arithmetic operation switch ($operation) { case "addition": $result = $num1 + $num2; break; case "subtraction": $result = $num1 - $num2; break; case "multiplication": $result = $num1 * $num2; break; case "division": // Check if divisor is not zero to avoid division by zero if ($num2 != 0) { $result = $num1 / $num2; } else { $result = "Cannot divide by zero"; } break; default: $result = "Invalid operation"; } // Display the result echo "<p>Result: $result</p>"; } ?> </body> </html>
Explanation:
Math Functions:complete example in html with explanation
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>PHP Math Functions Example</title> </head> <body> <h1>PHP Math Functions Example</h1> <?php // Example values $number1 = 9; $number2 = 4; // abs() - Absolute value echo "<p>Absolute value of $number1: " . abs($number1) . "</p>"; // sqrt() - Square root echo "<p>Square root of $number1: " . sqrt($number1) . "</p>"; // pow() - Power echo "<p>$number1 raised to the power of $number2: " . pow($number1, $number2) . "</p>"; // exp() - Exponential echo "<p>Exponential of $number1: " . exp($number1) . "</p>"; // log() - Natural logarithm echo "<p>Natural logarithm of $number1: " . log($number1) . "</p>"; ?> </body> </html>
Explanation:
HTML Structure: This is a basic HTML document with a title and body.
PHP Code: The PHP code is embedded within the HTML to perform various mathematical operations.
Math Functions Usage:
abs(): Calculates the absolute value of a number.
sqrt(): Calculates the square root of a number.
pow(): Raises a number to the power of another.
exp(): Calculates ‘e’ raised to the power of a number.
log(): Calculates the natural logarithm of a number.
Output: The results of each math function are echoed within <p> tags and displayed on the webpage.
You can run this code on a server that supports PHP. The calculated results for each math function will be displayed on the web page.
Trigonometric Functions:complete example in html with explanation
Explanation:
HTML Structure: This is a basic HTML document with a title and body.
PHP Code: The PHP code is embedded within the HTML to perform various trigonometric operations.
Trigonometric Functions Usage:
sin(): Sine function.
cos(): Cosine function.
tan(): Tangent function.
asin(): Arcsine function.
acos(): Arccosine function.
atan(): Arctangent function.
Conversion between Degrees and Radians: PHP trigonometric functions typically use radians, so the deg2rad() function is used to convert degrees to radians. Conversely, rad2deg() is used to convert radians back to degrees for display.
Output: The results of each trigonometric function are echoed within <p> tags and displayed on the webpage.
An application
This example will be a basic calculator application with support for basic arithmetic operations, math functions, and trigonometric functions.
Here’s a simple structure for a PHP calculator application:
index.php (Main Page):
This file contains the HTML structure and interface.
It includes forms for basic arithmetic operations and math/trigonometric functions.
<!-- index.php --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>PHP Calculator</title> </head> <body> <h1>PHP Calculator</h1> <!-- Basic Arithmetic Calculator Form --> <form method="post" action="calculator.php"> <label for="num1">Enter Number 1:</label> <input type="text" name="num1" required> <label for="num2">Enter Number 2:</label> <input type="text" name="num2" required> <label for="operation">Select Operation:</label> <select name="operation" required> <option value="addition">Addition</option> <option value="subtraction">Subtraction</option> <option value="multiplication">Multiplication</option> <option value="division">Division</option> </select> <button type="submit">Calculate</button> </form> <hr> <!-- Math Functions Calculator Form --> <form method="post" action="math_functions.php"> <label for="number">Enter a Number:</label> <input type="text" name="number" required> <label for="math_function">Select Math Function:</label> <select name="math_function" required> <option value="abs">Absolute Value</option> <option value="sqrt">Square Root</option> <option value="pow">Power</option> <option value="exp">Exponential</option> <option value="log">Natural Logarithm</option> </select> <button type="submit">Calculate</button> </form> <hr> <!-- Trigonometric Functions Calculator Form --> <form method="post" action="trig_functions.php"> <label for="angle">Enter Angle in Degrees:</label> <input type="text" name="angle" required> <label for="trig_function">Select Trigonometric Function:</label> <select name="trig_function" required> <option value="sin">Sine</option> <option value="cos">Cosine</option> <option value="tan">Tangent</option> <option value="asin">Arcsine</option> <option value="acos">Arccosine</option> <option value="atan">Arctangent</option> </select> <button type="submit">Calculate</button> </form> </body> </html>
calculator.php (Basic Arithmetic Calculator):
This file processes the basic arithmetic form submission and performs the calculation.
<!-- calculator.php --> <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { $num1 = $_POST["num1"]; $num2 = $_POST["num2"]; $operation = $_POST["operation"]; switch ($operation) { case "addition": $result = $num1 + $num2; break; case "subtraction": $result = $num1 - $num2; break; case "multiplication": $result = $num1 * $num2; break; case "division": $result = ($num2 != 0) ? $num1 / $num2 : "Cannot divide by zero"; break; default: $result = "Invalid operation"; } echo "<p>Result: $result</p>"; } ?>
math_functions.php (Math Functions Calculator):
This file processes the math functions form submission and performs the calculation.
<!-- math_functions.php --> <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { $number = $_POST["number"]; $mathFunction = $_POST["math_function"]; switch ($mathFunction) { case "abs": $result = abs($number); break; case "sqrt": $result = sqrt($number); break; case "pow": $result = pow($number, 2); break; case "exp": $result = exp($number); break; case "log": $result = log($number); break; default: $result = "Invalid math function"; } echo "<p>Result: $result</p>"; } ?>
trig_functions.php (Trigonometric Functions Calculator):
This file processes the trigonometric functions form submission and performs the calculation.
<!-- trig_functions.php --> <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { $angleDegrees = $_POST["angle"]; $angleRadians = deg2rad($angleDegrees); $trigFunction = $_POST["trig_function"]; switch ($trigFunction) { case "sin": $result = sin($angleRadians); break; case "cos": $result = cos($angleRadians); break; case "tan": $result = tan($angleRadians); break; case "asin": $result = rad2deg(asin(sin($angleRadians))); break; case "acos": $result = rad2deg(acos(cos($angleRadians))); break; case "atan": $result = rad2deg(atan(tan($angleRadians))); break; default: $result = "Invalid trigonometric function"; } echo "<p>Result: $result</p>"; } ?>
This is a simplified example, and in a real-world scenario, you might want to enhance error handling, input validation, and overall application structure.
Here’s a set of 15 quiz questions based on the PHP math concepts covered in the previous examples. Each question is followed by multiple-choice answers.
a) Personal Home Page
b) Pretext Hypertext Processor
c) Hypertext Preprocessor
d) Public Home Page
a) absolute()
b) abs()
c) absolute_value()
d) abs_value()
a) sqrt()
b) squareRoot()
c) root()
d) sqrRoot()
a) Calculates the product of two numbers.
b) Raises a number to the power of another.
c) Finds the remainder of a division.
d) Calculates the square root.
a) exp()
b) power()
c) e_power()
d) calculate_exp()
a) Logarithm to the base 10
b) Natural logarithm
c) Common logarithm
d) Exponential logarithm
a) Converts degrees to radians
b) Converts radians to degrees
c) Calculates the tangent of an angle
d) Finds the inverse sine of a value
a) sin()
b) cos()
c) tan()
d) sine()
a) Calculates the arcsine of a value
b) Calculates the arccosine of a value
c) Calculates the arctangent of a value
d) Calculates the angle whose tangent is a specified number
a) Sine / Cosine
b) Cosine / Sine
c) Sine * Cosine
d) Sine + Cosine
a) /
b) *
c) %
d) –
a) -15
b) 0
c) 15
d) -1
a) 2
b) 3
c) 5
d) 0
a) ln()
b) log()
c) natlog()
d) logarithm()
a) 7
b) 25
c) 10
d) 3