Should I use isset () with ctype_ *?

Is it better to print:

if (ctype_alnum($x)) echo true;

instead

if (isset($x) && ctype_alnum($x)) echo true;

?


I understand that it ctype_*finds NULLempty string values ​​as well false, and it does not give an error message if it $xdoes not exist, but does this mean that I should not use isset()with it?

Or am I just using it isset()in case future PHP updates change, and how does it ctype_*handle NULLempty string values?

+4
source share
3 answers

When $xthere is undefined and notifications are enabled, it issues a notification that it $xis undefined. And even if they are disabled, it is good practice to check it before use.

<?php
   echo ctype_alnum($x);
?>

result:

<br />
<b>Notice</b>:  Undefined variable: x in <b>[...][...]</b> on line <b>2</b><br />
+2
source
ctype_alnum(null);
// false
ctype_alnum('');
// false
ctype_alnum($x);
// Notice:  Undefined variable: x [...]

, , ctype_* null false, , @Flash Tunder, .

, , , isset().

+1

Well, of course, it’s better to check what is installed $x... but there is debate that is worse in performance, isset()or is producing notice.

I would use isset($x)and check, but:

... 
if (@ctype_alnum($x)) echo true;
...

using @to suppress errors might be better.

EDIT: according to this link

The difference was that suppressing the notification (notification!) Took 100% if you just checked to see if it existed in the first place.

so ... isset($x)that.

0
source

Source: https://habr.com/ru/post/1691884/


All Articles