Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.