Average Grade program does not show up the result

I wrote a simple program to calculate the average grades.
But the result does not show up.
Could someone help me check my code?
Thank you!

grade Please enter the grades, use space between two grades: <?php $grade=@$_REQUEST['grade']; $grade2=explode(" ",grade); $sum=0; foreach($grade2 as $key=>$V) { $sum+=$V; } ?> <?php echo 'The average grade is '.$sum/count($grade2); ?>

Turn on error reporting, you have missed a $ (at grades), also you shouldn’t use @, it is at best suppressing errors, which we don’t want to do :slight_smile:

I did some changes to the code:

[php]<?php

$gradeField = !empty($_POST[‘grade’]) ? $_POST[‘grade’] : null;

$grades = explode(’ ', $gradeField);
$sum = array_sum($grades);

?>

grade Please enter the grades, use space between two grades: <?php echo 'The average grade is ' . $sum / count($grades); ?> [/php]

to add to Jim’s input, don’t do this: [php]$_REQUEST[‘blahblahblah’][/php]
especially as you already know the data is coming via post.
[php][/php]

use $_POST instead like Jim has.
(or $_GET if you are using method="$_GET")

and as for this: @ <<-- :’( :’( :’( :frowning: :frowning: :’( >:( >:( :frowning: :o :o ??? :’( :’(

Red :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service