0

I've been at this for over two weeks. The + seems to throw an non-numeric error and I can't figure out why.

<?php
define("number1", mt_rand(1,100));
define("number2", mt_rand(1,100));
echo(number1."<br>");
echo(number2);

echo("<br>".number1."x".number2."=".number1 * number2);
echo("<br>".number1."+".number2."=".number1 + number2);
echo("<br>".number1.":".number2."=".number1 / number2);
?>

And this is the error message:

Warning: A non-numeric value encountered on line 8

Qirel
  • 25,449
  • 7
  • 45
  • 62
Job
  • 13
  • 1
  • Put a parenthesis around your calculations when concatenating them to strings https://3v4l.org/1IdIa – Qirel Mar 03 '20 at 13:44

1 Answers1

0

You need to use ():

echo("<br>".number1."x".number2."=".(number1 * number2));
echo("<br>".number1."+".number2."=".(number1 + number2));
echo("<br>".number1.":".number2."=".(number1 / number2));

cause . means concatenation a string and + * / cannot be used in this way.

EDIT:

  • "<br>".number1."x".number2."=".number1 has a string datatype
  • string * number2 means string * numeric value => error
Aksen P
  • 4,564
  • 3
  • 14
  • 27