Jump to content

EXCEPTION help!


snowman15

Recommended Posts

This script isn't working at all.

There is an include file that holds the functions, and then the actual php file that runs this really easy script and its not working! :(

 

 

 

include file: (do not change, it is supposed to be right)

 

<?php

  function formatException(Exception $e)
  {
    return "Error {$e->getCode()}: {$e->getMessage()} " .
          "(line: {$e->getline()} of {$e->getfile()})";
  }
  
  function average($numArray) {
    if (! is_array($numArray))
      throw new Exception("Input parameter not an array", 10001);
        
    $total = 0;
    $itemCount = count($numArray);

    if ($itemCount == 0)
      throw new Exception("Number of items to average = 0", 10002);

    foreach ($numArray as $i) {
      if (is_int($i) || is_float($i))
        $total += $i;
      else
        throw new Exception("Array element not numeric", 10003);
    }
    return $total / $itemCount;
  }
?>

 

 

What is wrong below?

 

<?php
include('exam1p4func.inc.php');
  
  $myArray = array(10, 15, 17, 23, 9, 11, 13);
  $myString = "I am not an array";
  $emptyArray = array();
  $badArray = array(1, 2.2, 3.25, "four", 5.0, 66);
  
  try {
   $answer=average($myArray); 
   echo $answer;
} 
catch (Exception $em) 
{
    echo $em;
}

}
?>

 

 

something is wrong in the php file that runs the function average()

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/75597-exception-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.