2

I'm very new to php .I want to convert the string to integer value.

I used following code to convert

$val='(100*2)';
echo (int)($val); //This will showing output as 0

But echo (int)((100*2)); /This will showing output as 200

Please help me any one to solve this .Thanks advance

4
  • 1
    you will have to parse the string - where is it coming from? Commented Oct 1, 2013 at 12:03
  • Why are you even using it as a string? Commented Oct 1, 2013 at 12:04
  • I concatenate the values using some condition to generate formula .Finally the output comes like $string='((33*4)+( 3+5))'; I need to calculate the integer value from the above string . Commented Oct 1, 2013 at 12:09
  • just use simple $value=100*2; echo $value; here you are useing '' and this is used for string so no need to use '' for int var Commented Oct 1, 2013 at 12:10

6 Answers 6

3

(int)($val) evaluates to 0 because $val's value is not a numeric string (ie one that can be directly cast to a number).

If you really need this kind of functionality, try eval():

$val='(100*2)';
echo (int)($val); //This will showing output as 0
eval('$newval='.$val.';');
echo $newval;

But be warned: eval() can be dangerous!

From http://php.net/manual/en/function.eval.php:

Caution

The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.

EDIT: Added .';' to eval parameter to make it a legit php instruction.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi just i used this following code .What was requested from your end . $val='(100*2)'; echo (int)($val); //This will showing output as 0 eval('$newval='.$val); echo $newval; //This will showing output as (100*2) echo round($newval); //This will showing output as 0 , But i need the output as 200 instead of 0 .Can you help me
Hi, the 0 you are seeing comes from the first echo. eval() didn't work because I missed a semicolon at first (see my edit). Sorry! Now the output will be 0200 (0 from the first echo and 200 from the second)
1

The most common suggestion will be - evaluate your string as PHP code, like:

$val = '(100*2)';
eval('$val = '.$val.';');

-but that's unsafe, eval should be avoided as long as possible.

Alternatively, there is bcParser for PHP, which can solve such issues. That's more safe than eval.

Finally, I doubt that you really need do such things - it seems you're solving some problem with wrong method (see XY-problem description)

Comments

1

You can do it using php eval function. For that first you have to check for special characters and equation characters.

$val='(100*2)';
echo matheval($val);

function matheval($equation)
{
   $equation = preg_replace("/[^0-9+\-.*\/()%]/","",$equation);
   // fix percentage calcul when percentage value < 10
   $equation = preg_replace("/([+-])([0-9]{1})(%)/","*(1\$1.0\$2)",$equation);
   // calc percentage
   $equation = preg_replace("/([+-])([0-9]+)(%)/","*(1\$1.\$2)",$equation);
   // you could use str_replace on this next line
   // if you really, really want to fine-tune this equation
   $equation = preg_replace("/([0-9]+)(%)/",".\$1",$equation);
   if ( $equation == "" )
   {
        $return = 0;
   }
   else
   {
        eval("\$return=" . $equation . ";" );
   }
   return $return;
}

Comments

0

I not recommended this, but you can use eval to suit your need

eval('$val = (100*2)');
echo intval($val); 

1 Comment

0

Why not just:

$x=intval(100);// <- or an other value from the cleaned user input
$y=intval(2);
$val=$x*$y;
echo $val;

Comments

0

You are not giving a goal.

I can just explain why your code works like it does.

'(100*2)' is a String that cannot be converted to int, since it does contain other chars than numbers. Every String that cannot be converted will result in 0.

echo (int)(100*2) will work, because you use numbers and no strings. No need to convert, either, would work without the cast, just echo (100*2);

Remember PHP use loose typing. You will almost never need to convert types. This scenario is very set up.

1 Comment

My ultimate aim of the code is used to one function to create a formula using certain rules and condition.Now this function return a formula string .here i want make it this string to integer actual value.Please help me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.