Home > Backend Development > PHP Problem > Detailed explanation of how to input PHP array sum from keyboard

Detailed explanation of how to input PHP array sum from keyboard

PHPz
Release: 2023-04-19 09:33:22
Original
699 people have browsed it

In the process of developing web applications, PHP is a commonly used programming language. Array operations in PHP are very common and important, so in this article, we will introduce the method of summing PHP arrays from the keyboard.

In PHP, array operations are one of the operations we often use. We often need to perform various processing on arrays to complete our application requirements. Summing is also a common operation. If we want to input a set of arrays from the keyboard and perform a sum operation on them, we can take the following steps:

1. Input the array from the keyboard

To input a PHP array from the keyboard, we can Use PHP's predefined variables $_POST, $_GET, $_REQUEST ($_COOKIE, $_SESSION can also be used), of which we use $_POST in this example.

In the HTML file, we can use the form form to get the array value we want to enter:

<form method="post" action="">
    <input type="text" name="number[]" />
    <input type="text" name="number[]" />
    <input type="text" name="number[]" />
    <!--此处可以根据需要增加/减少input-->
    <input type="submit" name="submit" value="计算和" />
</form>
Copy after login

In the PHP file, we can use the following code to get the array input from the keyboard:

if(isset($_POST['submit'])) {
    $numbers = $_POST['number'];
}
Copy after login

At this point, the $numbers variable will contain the array entered from the keyboard.

2. Sum the array

After we obtain the input array, we need to perform a sum operation on it. We can do this using PHP's array_sum() function, which sums all elements in an array.

So, for the input array, we can use the following code to sum it:

$sum = array_sum($numbers);
Copy after login

3. Display the result

Finally, we need to display the result in the output page. To do this, we can put the value of the $sum variable into an HTML div. The following is the complete PHP code:

if(isset($_POST['submit'])) {
    $numbers = $_POST['number'];
    $sum = array_sum($numbers);
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>PHP数组求和</title>
</head>

<body>
    <h3>请输入需要求和的数组:</h3>
    <form method="post" action="">
        <input type="text" name="number[]" />
        <input type="text" name="number[]" />
        <input type="text" name="number[]" />
        <!--此处可以根据需要增加/减少input-->
        <input type="submit" name="submit" value="计算和" />
    </form>
    <br />
    <?php if(isset($_POST[&#39;submit&#39;])): ?>
        <div>输入数组:<?= implode(&#39;, &#39;, $numbers) ?></div>
        <div>求和结果:<?= $sum ?></div>
    <?php endif; ?>
</body>

</html>
Copy after login

In the above code, we use PHP's implode() function to connect the elements in the array together to form a string for easy output and display.

Summary:

PHP is a rich and powerful programming language, of which array operations are an important part. By learning the methods provided in this article, we can easily input PHP arrays from the keyboard and perform sum operations on them. This provides us with great convenience in web development applications. I hope it will be helpful to you.

The above is the detailed content of Detailed explanation of how to input PHP array sum from keyboard. For more information, please follow other related articles on the PHP Chinese website!

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