PHP not issuing any notice for undefined variable if the variable is passing to empty() or isset()

StackOverflow https://stackoverflow.com/questions/13877200

  •  08-12-2021
  •  | 
  •  

Вопрос

<?php

function myFunction($yesNname) { } 

empty($noName);
print_r($noName);
isset($noName);

myFunction($noName);

output:

PHP Notice:  Undefined variable: noName ... on line 6 // print_r
PHP Notice:  Undefined variable: noName ... on line 9 // myFunction

The undefined variable is used in empty() and isset(). But PHP didn't issue notice about it. Why PHP shows discrimination to some function? How can I write such type of function?

Это было полезно?

Решение

Neither isset() nor empty() are functions. As the manual explains:

this is a language construct and not a function

To get this behaviour you'd need to tweak the PHP source code written in C.

It's possible that you can also get this behaviour with a PHP extension, but you'd also need to write it in C and install it in your server.

Update:

Другие советы

Did you mean like:

if(!empty($noName)) {
 // print_r
 // function($noName);
}

There is no way to do it on function side. If you simply don't want to show errors you can either check variable first before calling function or use error control operator "@".

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top