0

I tried running this simple php code in 2 different machines and i get different results for some reason.

the code:

<?php
$a = 5;
$b = "22ff67";
$s = $a - $b;
echo $s;‏
?>

in machine 1 i get the error:

Notice a non well former numeric value encountered in .... on line 4 17

and in machine 2 i get: -17

Notice: Use of undefined constant ‏ - assumed '‏' in C:\xampp\htdocs\aa.php on line 5

i run the code using XAMPP apache server...

  1. what could be the reason for the different results?
  2. the output is -17 because it tries to subtract 22 from 5 but because 22 is part of a string it gives the error? (it takes only 22 because it's the number before the first letter in the string (in strings which has signs other than '.', 'e','E' it takes the first number up to a sign other than those)

Thanks.

Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
Piepypye
  • 117
  • 1
  • 13
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Masivuye Cokile Oct 27 '17 at 08:45
  • [php syntax errors and how to solve them](https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – Masivuye Cokile Oct 27 '17 at 08:45
  • Are there any invisible characters in there, or is the minus some alternative dash like an em-dash? That might explain the constant notice. Re-type those lines and/or inspect the file with a hex editor. – deceze Oct 27 '17 at 08:45
  • $b is a string so you can't subtract it from integer $a clearly you understand that, the reason for the different errors per system is likely down to the logging level set in php.ini on each system, OR it could be different php versions, either way you want to extract the value to deduct from the string they cast it as $b = (int) extractedValue; – CodingInTheUK Oct 27 '17 at 08:47
  • 1
    @Chris PHP implicitly does the `(int)` casting for you; doing it manually won't make any difference. (Unless you use some regex to extract only the numeric characters…) – deceze Oct 27 '17 at 09:01
  • 1
    To be fair i always do the casting manually, it eliminates the question of what should be when another developer is looking at my code. – CodingInTheUK Oct 27 '17 at 09:27

0 Answers0