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

  • Home
  • SEARCH
  • 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 8900135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:02:00+00:00 2026-06-15T01:02:00+00:00

I have following code: <?php function calculate_string( $mathString ) { $mathString = trim($mathString); //

  • 0

I have following code:

<?php
function calculate_string( $mathString )    {
   $mathString = trim($mathString);     // trim white spaces
   $mathString = preg_replace("/[^0-9\(\)\+\-\*\/\.]/", "", $mathString);       
   return eval("return $mathString;");   
 }

$string1 = " (1 + 1) *3+ (2 + 2)";
echo calculate_string($string1);
$string2 = " (1 + 1) *3+* (2 + 2)";
echo calculate_string($string2);
?>

function calculate_string() must calculate math expression given in string as a param. First call on string1 work good.

But, how I can detect if math. string have math. error in syntax like in seconda call,
and return some value depends of error type, or simple return nothing(better solution)?
So, problem is parse_error in second call when math expression contain errors in syntax.

Please help.

  • 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-06-15T01:02:06+00:00Added an answer on June 15, 2026 at 1:02 am

    Ok, so you’d like to sport something like *3+* in:

    $string2 = " (1 + 1) *3+* (2 + 2)";
    

    Well you can do this in many ways, by checking string yourself for some common errors or by checking eval()‘s return value (docs):

    As of PHP 7, if there is a parse error in the evaluated code, eval()
    throws a ParseError exception. Before PHP 7, in this case eval()
    returned FALSE and execution of the following code continued normally.
    It is not possible to catch a parse error in eval() using
    set_error_handler().

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

Sidebar

Related Questions

I have the following code: <?php function search_reset() { $query = $_GET['q']; if($query) echo
I have the following code: <?php function foo($bar) { global $products; //$products = array();
I have the following code: $(document).ready(function() { $.getJSON('../includes/_quiz.php?class_id=163&course_id=183',function(data){ $.each(data,function(k,v){ questions[k] = v.question; answers[k*4] =
I have the following jquery code: jQuery(function(){ jQuery(select#rooms).change(function(){ var options = ''; jQuery.getJSON(/admin/selection.php,{id: jQuery(this).val(),
I have the following php code which I found here : function download_xml() {
currently I have the following code inside my 'function.php' is function calcTime($database_name,$currentTime){ global $startTime;
i have the following PHP code: <?php ini_set( 'default_charset', 'UTF-8' ); mb_internal_encoding('UTF-8'); function labelValidate($label)
I have the following code function _getTime(msgStr){ var theStr:int = (int)msgStr.split(' '); return theStr;
I have the following php code: function mapWordsToOriginal() { try { $d = array();
I have the following code: <?php function s2int($pinned_id) { $action=aaa; if ( $action &&

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.