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

Fluffeh
  • 33,228
  • 16
  • 67
  • 80
kovarthan
  • 137
  • 2
  • 2
  • 12

6 Answers6

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.

geomagas
  • 3,230
  • 1
  • 17
  • 27
  • 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 – kovarthan Oct 03 '13 at 09:44
  • 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) – geomagas Oct 03 '13 at 09:58
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)

Community
  • 1
  • 1
Alma Do
  • 37,009
  • 9
  • 76
  • 105
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;
}
Maulik Bhojani
  • 442
  • 4
  • 9
0

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

eval('$val = (100*2)');
echo intval($val); 
Chokchai
  • 1,684
  • 12
  • 12
0

Why not just:

$x=intval(100);// <- or an other value from the cleaned user input
$y=intval(2);
$val=$x*$y;
echo $val;
cb0
  • 8,415
  • 9
  • 52
  • 80
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.

mondjunge
  • 1,219
  • 14
  • 24
  • 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 – kovarthan Oct 01 '13 at 13:15