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>
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']; }
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);
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['submit'])): ?> <div>输入数组:<?= implode(', ', $numbers) ?></div> <div>求和结果:<?= $sum ?></div> <?php endif; ?> </body> </html>
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!