Home > Backend Development > PHP Tutorial > PHP mathematical function practice one: interesting analysis of max() and min() functions

PHP mathematical function practice one: interesting analysis of max() and min() functions

藏色散人
Release: 2023-04-10 12:04:02
Original
3354 people have browsed it

PHP Math function is the PHP Math function. As the name suggests, it is a series of functions related to mathematics or data calculation. It plays an important role in our PHP interview process or in actual development projects, such as finding maximum and minimum values, absolute values, rounding data, etc... So for newcomers who like to play with data, this series Don’t miss the article. I guarantee you’ll understand it at a glance. Getting started with PHP can be done in minutes! ! !

Now I will explain to you the use of max() and min(), which are commonly used mathematical functions in PHP, through actual code cases. Follow-up The article will continue to introduce commonly used mathematical functions, please pay more attention~

Now if we have such an exercise question, the question is "Please write a PHP script to find the maximum sum from the array Minimum value and output the result in the browser

This question is concise and clear, so let’s go step by step~

Note that you must be local before we operate Install and configure the PHP environment (attached: PHP environment installation video)!

First we create a PHP sample file demo.php (Note: I use phpstorm editor: Genuine phpstorm free activation steps (detailed graphic and text explanation))

is as follows Picture:

PHP mathematical function practice one: interesting analysis of max() and min() functions

The code in the picture is as follows:

<?php
$marks1 = array(360,310,310,330,313,375,456,111,256);
$marks2 = array(350,340,356,330,321);
$marks3 = array(630,340,570,635,434,255,298);
$max_marks = max(max($marks1),max($marks2),max($marks3));
$min_marks = min(min($marks1),min($marks2),min($marks3));
echo "最大值 : ".$max_marks."<br>";
echo "最小值 : ".$min_marks;
Copy after login

In the above code, I created three example arrays $marks1, $marks2, $marks3 , and then use the max function to compare the array data one by one and obtain the maximum value; also use the min function to obtain the minimum value.

Finally we just echo the maximum and minimum values!

The result of running the browser is as follows:

PHP mathematical function practice one: interesting analysis of max() and min() functions

Isn’t it very simple! ! !

Below we attach the official definitions of the max function and the min function:

  • max() returns the maximum value, the syntax "max(x,y)", if only With one parameter and an array, max() returns the largest value in the array. If the first argument is an integer, string, or float, at least two arguments are required and max() returns the largest of these values. An infinite number of values ​​can be compared.

  • min() returns the minimum value, syntax "min(x,y)" If there is only one parameter and it is an array, min() returns the minimum value in the array. If the first argument is an integer, string, or float, at least two arguments are required and min() returns the smallest of these values. An infinite number of values ​​can be compared.

Welcome everyone to learn~

Recommended related articles:

PHP Mathematical Function Practice 2: All-round Application of Round() Function
PHP mathematical function practice three: clever use of random function rand()

PHP video tutorialPlease click ->https://www.php.cn/course/list/29/type/2.html

The above is the detailed content of PHP mathematical function practice one: interesting analysis of max() and min() functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template