0

i got this error:

Function create_function() is deprecated in shipping.php

private function calculate_string( $mathString ) {
    $mathString = trim($mathString);     // trim white spaces
    $mathString = preg_replace ('[^0-9\+-\*\/\(\) ]', '', $mathString);    // remove any non-numbers chars; exception for math operators
    $compute = create_function("", "return (" . html_entity_decode($mathString) . ");" );
    return 0 + $compute();

as far as i found out, create_function is useless now. I considered all the suggestions on the web and came to the following conclusion

private function calculate_string( $mathString ) {
    $mathString = trim($mathString);     // trim white spaces
    $mathString = preg_replace ('[^0-9\+-\*\/\(\) ]', '', $mathString);    // remove any non-numbers chars; exception for math operators
    $compute = function() {
      return (" . html_entity_decode($mathString) . ");
    };
    return 0 + $compute();

but this time it gives this error:

Undefined variable: mathString in shipping.php on line 1592 Warning: A non-numeric value encountered in shipping.php

Also i tried return part like this ways but it is not working

      return html_entity_decode($mathString);
      return $html_entity_decode($mathString);

Where am I going wrong? Can you help me please?

ADyson
  • 57,178
  • 14
  • 51
  • 63
Batuhan
  • 11
  • 1
  • A deprecation notice is just that - a notice. It's not an error. It's telling you that the function is deprecated...that means it's likely to be removed in a _future_ release of PHP. It will still work fine in your current version. Just make sure that when you go live, notices are set to log to a file, not show on screen. However that doesn't mean you shouldn't plan to replace the function, so you don't get caught out when it finally does get removed. So in that sense your plan to remove it is good, but it's not an urgent problem, and you can take the time to get it right. – ADyson Dec 13 '21 at 23:15

1 Answers1

2

Use an anonymous function with eval().

$compute = function() use ($mathString) { eval("return (" . html_entity_decode($mathString) . ");"; }

You need the use option to make the variable available in the function.

Actually, there's no need for the function in the first place. You can just use eval() directly.

private function calculate_string( $mathString ) {
    $mathString = trim($mathString);     // trim white spaces
    $mathString = preg_replace ('[^0-9\+-\*\/\(\) ]', '', $mathString);    // remove any non-numbers chars; exception for math operators
    eval('return 0 + (' . html_entity_decode($mathString) . ');')
}

I have a feeling the original programmer either didn't know how to use eval(), or heard the advice that eval() is dangerous. But creating a function with the dynamic content is just as dangerous.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Because the original code is evaluating the math string when it creates a function dynamically with that content. – Barmar Dec 13 '21 at 23:20
  • Because this is supposed to do what the original code did. It executes the math string, it doesn't just return it. – Barmar Dec 13 '21 at 23:21