1

Hello,

$expression="1*2";
var_dump(intval($expression));

    

gives int 1

why ?

Thanks for helping

Progman
  • 16,827
  • 6
  • 33
  • 48

2 Answers2

2

There is using common type cast rules. In string "1*2" second char is not numeric. Takes only left side. https://www.php.net/manual/en/language.types.integer.php#111523

1

If you are wanting the expression to be evaluated as an expression, use the eval() function.

But this is not recommended unless you filter $expression for dangerous characters.

Phil Y
  • 186
  • 1
  • 4