1

Hello,

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

    

gives int 1

why ?

Thanks for helping

Progman
  • 14,690
  • 4
  • 32
  • 46

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
  • 126
  • 1
  • 3