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 853435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:45:32+00:00 2026-05-15T07:45:32+00:00

I have a large mathematical expression that has to be created dynamically. For example,

  • 0

I have a large mathematical expression that has to be created dynamically. For example, once I have parsed “something” the result will be a string like: "$foo+$bar/$baz";.

So, for calculating the result of that expression I’m using the eval function… something like this:

eval("\$result = $expresion;");
echo "The result is: $result";

The problem here is that sometimes I get errors that says there was a division by zero, and I don’t know how to catch that Exception. I have tried things like:

eval("try{\$result = $expresion;}catch(Exception \$e){\$result = 0;}");
echo "The result is: $result";

Or:

try{
    eval("\$result = $expresion;");
}
catch(Exception $e){
    $result = 0;
}
echo "The result is: $result";

But it does not work. So, how can I avoid that my application crashes when there is a division by zero?

Edit:

First, I want to clarify something: the expression is built dynamically, so I can’t just eval if the denominator is zero. So… with regards to the Mark Baker’s comment, let me give you an example. My parser could build something like this:

"$foo + $bar * ( $baz / ( $foz - $bak ) )"

The parser build the string step by step without worrying about the value of the vars… so in this case if $foz == $bak there’s in fact a division by zero: $baz / ( 0 ).

On the other hand as Pete suggested, I tried:

<?php
$a = 5;
$b = 0;

if(@eval(" try{ \$res = $a/$b; } catch(Exception \$e){}") === FALSE)
        $res = 0;
echo "$res\n";
?> 

But it does not print anything.

  • 1 1 Answer
  • 1 View
  • 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-15T07:45:32+00:00Added an answer on May 15, 2026 at 7:45 am
    if ($baz == 0.0) {
        echo 'Divisor is 0';
    } else {
        ...
    }
    

    Rather than use eval, which is highly dangerous if you’re using user-input within the evalled expression, why not use a proper parser such as evalmath on PHPClasses, and which raises a clean exception on divide by zero

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

Sidebar

Related Questions

i have large numbers of text files and i am in problem that i
I have large video files (~100GB) that are local on my machine. I have
I have a Java method that repeatedly evaluates the following expression in a very
I have a very complicated mathematica expression that I'd like to simplify by using
I will be making a cross-platform, graphical mathematical modeling application, for example, a user
I have a very large, dense, labeled graph that I have visualized with GraphPlot.
I currently have a large expression with many terms of the form Abs[-2 b
We have large C++ project that we used to compile with the /MP switch
I have large string of coordinate data similar to the following: -83.6530304633209,34.8237504877659,-83.65536046332301,34.8248804877671, etc.... I
I have large table, with around 200 fields. Around a 100 of those fields

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.