0

how to fix this code so that it works correctly?

the programmed code makes it add all the integers from 1, as long as the sum doesn't exceed 1500. Show By display how many numbers were added and the result obtained. The problem is that when I want to view it through xampp it doesn't work. <!DOCTYPE html> <html> <head> <title>Number Entry</title> </head> <body> <h1>Number Entry</h1> <?php // Initialize the sum, counter, and array to store the numbers entered $sum = 0; $count = 0; $numbersentered = array(); // Loop to allow input of numbers do { // Ask the user to enter a number $number = readline("Enter a number: "); // Check if the number is valid (numeric and greater than or equal to 1) if (is_numeric($number) && floatval($number) >= 1) { $number = floatval($number); // Convert the entered number to a float value $sum += $number; // Add the entered number to the total sum $counter++; // Increment the counter of entered numbers $numbersEntered[] = $number; // Add the entered number to the array of numbers // Ask the user if they want to enter more numbers $continue = strtolower(readline("Do you want to enter another number? (yes/no): ")); } else { echo "Please enter a valid number greater than or equal to 1.\n"; $continue = "yes"; // Force the loop to continue on invalid input } } while ($continue === 'if'); // Check if the sum is greater than or equal to 1500 if ($sum >= 1500) { echo "<p>The sum of the numbers has exceeded 1500 and the result cannot be displayed.</p>"; } else { // Display the result of the sum and the number of numbers added echo "<p>$count valid numbers greater than or equal to 1 were entered.</p>"; echo "<p>The total sum is: $sum</p>"; echo "<p>The numbers entered were: " . implode(', ', $numbersentered) . "</p>"; } ?> </body> </html>

23rd Jul 2023, 4:47 AM
Victoria Abi Rached
1 Odpowiedź
+ 1
Short answer, try to run this in a terminal using: php followed by file script name Example: php example1.php , because web interface does not support this way of interactivity between user and script using php To provide interactivity in web page, you must use front end languages like javascript Or set your php script in the backend then send requests to it using ajax to tell him to process something and return you the result that you can then show up in your web app Php cannot be used to interact with user directly through web interface, but it can just process something before the webpage is loaded then you can use the result of this process within your web page
23rd Jul 2023, 10:04 AM
Radi
Radi - avatar