0
$a=rand(1, 10);
$b=rand(1, 11);
$belgilar = ['+', '-', '*'];
$belgi = $belgilar[rand(0, 2)];
$masala = $a." ".$belgi." ". $b." = ";
echo $masala;
$c = ($a.($belgi).$b);
echo $c;

(the return: 4 - 3 = 4-3)::but the answer should not be so. It should give me a result of 4-3 that is 1.

Ravshan
  • 1
  • 1
  • `.` is for string concatenation. You need to run that snippet/formula as code to be evaluated and then calculated. One way to do this would be https://3v4l.org/9CZpR – user3783243 Jun 01 '22 at 20:15
  • try eval("return $a $operator $b;"); – Stefan Avramovic Jun 01 '22 at 20:20
  • @StefanAvramovic I'd discourage that. As manual states `Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct`. Very rarely if every should eval be used. I've never had a reason to use eval since PHP 4. – user3783243 Jun 01 '22 at 20:22
  • (With dup suggestion I don't recommend `eval` usage. See all other usages and notes of `eval` dangers) – user3783243 Jun 01 '22 at 20:24
  • In case it's not clear, adding brackets doesn't make any difference here: `$c = ($a.($belgi).$b);` just means the same thing as `$c = $a . $belgi . $b;` – IMSoP Jun 01 '22 at 22:06
  • Thank you, @Stefan Avramovich. eval("return $a $operator $b;"); => this code worked. – Ravshan Jun 02 '22 at 15:57

0 Answers0