Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7601481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:10:26+00:00 2026-05-30T23:10:26+00:00

I hope the title isn’t too confusing, I’ll try to explain better below. Suppose

  • 0

I hope the title isn’t too confusing, I’ll try to explain better below.

Suppose I have a function in a separate file, functions.php:

function divide($num1, $num2) {
    if ($num1 == 0 || $num2 == 0) {
        trigger_error("Cannot divide by 0", E_USER_ERROR);
    } else {
        return ($num1 / $num2);
    }
}

And another file that calls it:

include "functions.php";

echo divide(10, 0);

My error is

Fatal error: Cannot divide by 0 in
C:\Users\Derek\Desktop\projects\functions.php on line 5

My question is, how do I make that error instead point to the location of the error in the main code, so I instead get:

Fatal error: Cannot divide by 0 in
C:\Users\Derek\Desktop\projects\main.php on line 3

The particular reason I want this is because I have a function called load_class that simply finds a PHP file and instantiates the object inside, but if given an incorrect file name, it reports an error from inside load_class, which is technically true, but it’s not particularly helpful if I don’t remember where I called load_class in the first place. I would like the error to point to the file that called load_class incorrectly.

Also, I would like to write a function error() (something like below) that when given a message as a parameter would throw more “meaningful” error messages, but when done that way, the error always says it comes from error(), not from where the error actually came from!

For example, in an error.php:

/**
 * error()
 * 
 * Throws an error of a certain type
 * 
 * @param  string $type    The type of error. "Fatal", "warning," or "notice"
 * @param  string $message A description of the error
 * @return void
 */
function error($type, $message) {
    switch (strtolower($type)) {
        case 'fatal':
            trigger_error($message, E_USER_ERROR);
            break;
        case 'notice':
            trigger_error($message, E_USER_NOTICE);
        default:
            trigger_error($message, E_USER_WARNING);
            break;
    }
}

And in an index.php

error("fatal", "A sample warning!");

My error given is:

Fatal error: A sample warning! in
C:\Users\Derek\Desktop\projects\synthesis\sys\Error.php on line 45

But the error didn’t occur in error.php, it happened in index.php! How can I make it show where it really came from?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T23:10:27+00:00Added an answer on May 30, 2026 at 11:10 pm

    The debug_backtrace function allows you to obtain the stacktrace as an array. You can pick the original location from there.

    Next to that you need to slip into the error message to make this look-alike. Example:

    function divide($num1, $num2) {
        if ($num1 == 0 || $num2 == 0) {
            trigger_error_original("Cannot divide by 0", E_USER_ERROR);
        } else {
            return ($num1 / $num2);
        }
    }
    
    function trigger_error_original($message, $type) {
        $trace = debug_backtrace(FALSE);
        list($location) = array_slice($trace, 1, 1) + array('file' => 'unknown', 'line' => 'unknown');
        $message .= sprintf(" in %s on line %d\nTriggered", $location['file'], $location['line']);
        trigger_error($message, $type);
    }
    
    divide(1, 0);
    

    The error message than shows something like:

    > php test-pad.php
    
    Fatal error: Cannot divide by 0 in test-pad.php on line 18
    Triggered in test-pad.php on line 15
    

    The downside of this is, that you need to change your code to have this “feature”. If you need this for debugging your own code, it’s much better that you enable backtraces in your logs. The Xdebug extension does this for you, or you can write your own error handler that takes care of that.

    See as well the related question Caller function in PHP 5?. I used array_slice so that you could create an additional parameter to define the number of steps you want to go “up” in the backtrace.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hope that title isn't too cryptic. I have an array with a DATETIME object
I hope the title is not too confusing. I am trying to make folders
The title is propably misleading so I try to explain my situation. I have
Hope my title is not too confusing. Please let me know if there is
Hope the title isn't to confusing wasn't sure how I should put it. I
I hope this isn't too basic a question. The title kind of asks it
I hope the title makes sense. I have a set of items that I
(I hope my title is comprehensible, it was hard to sum up.) I have
I hope the title doesn't sound too subjective; I absolutely do not mean to
I hope this isn't too vague a question, but here goes. I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.