0

Previous versions of PHP shows all errors during execution. Now PHP7 seems to me, that it checks only that part of the code which will be executed. If there is a conditional part, after an if, and it won't be executed, even there is an undefined function, the code is executes with no error.

I tried even the error_reporting(-1); setting - not any effect.

if (true)
    $mycon = some_function_that_is_undefined();
// result: Fatal error: Uncaught Error: Call to undefined function 

if (false)
    $mycon = some_function_that_is_undefined();
// result no error, the following code is executed

I expected that if there is a fatal error in my code, it will cause en error even if it wont't be executed in the actual conditions. It works the same way if there is variable instead of the true/false constant.

Andreas
  • 23,304
  • 5
  • 28
  • 61
pf555
  • 1
  • 1
  • 7
    `mysql_*` was deprecated in PHP 5.5 and completely removed in PHP 7. Use `mysqli_*` or `PDO` instead. – M. Eriksson Mar 29 '19 at 14:47
  • Is error reporting enabled? – lucid Mar 29 '19 at 14:47
  • `mysql_connect()` does not exist on php 7 it was removed use mysqli or pdo – Masivuye Cokile Mar 29 '19 at 14:47
  • 1
    I believe that is what OP is trying to ask about! He says there is no error the code above is an code that should create an error but it doesn't since the statement is false – Andreas Mar 29 '19 at 14:49
  • `if(true)` what? – Masivuye Cokile Mar 29 '19 at 14:51
  • 1
    Meaning this question is not a duplicate of any MySQL questions and the code is not at risk due to MySQL injection – Andreas Mar 29 '19 at 14:51
  • No, it will only throw the fatal error if it gets to that part of the code. Parse/syntax errors will stop the code dead, but undefined functions only matter when the function is attempted. – aynber Mar 29 '19 at 14:52
  • @Andreas if the code enters the if then it will produce error... `if(true)` what? exactly – Masivuye Cokile Mar 29 '19 at 14:53
  • True! If(true) then it is true. What is so hard about that to understand? – Andreas Mar 29 '19 at 14:54
  • 2
    The question is probably about what @Andreas mentioned. But OPs observation is likely flawed. Previous PHP versions also just do function/identifier checks at runtime (in contrast to e.g. syntax deviations). // Though gonna closevote this anyhow; the example does not reproduce an error handling difference in PHP versions, except as symptom here. – mario Mar 29 '19 at 14:54
  • what exactly is true? – Masivuye Cokile Mar 29 '19 at 14:55
  • *what exactly is true?* **TRUE**!! – Andreas Mar 29 '19 at 14:56
  • 4
    Attempting to run a function that isn't defined is a runtime error, not a syntax error. If the code that calls the undefined function never runs, then you don't get an error, simple as that. This was true in PHP4 and hasn't changed for 5 or 7. [Example](https://3v4l.org/vYZt7) – Alex Howansky Mar 29 '19 at 14:56
  • I'd like to know if my code is error free. I would be unfortunate thing if a conditional part of the code cause a fatal error later when the condition changes. – pf555 Mar 29 '19 at 14:58
  • 1
    _"I would be unfortunate thing if a conditional part of the code cause a fatal error later when the condition changes."_ That's what unit testing is for. – Alex Howansky Mar 29 '19 at 15:02
  • Also, tools like [PHPStan](https://github.com/phpstan/phpstan) can help identify potential runtime issues like this. – Alex Howansky Mar 29 '19 at 15:03

0 Answers0