Skip to content
Advertisement

Shorter form of $var= isset($_REQUEST[ ‘var’ ]) ? filter_var

Is there any shorter form of this?

 $var =  isset($_REQUEST[ 'var' ]) ? filter_var($_REQUEST[ 'var' ], FILTER_SANITIZE_NUMBER_INT ): 0 ;

If i use the ?? operator I miss the filter_var processing.

And if I do not check if it is empty, I get an undefined index[var] error. notice

Advertisement

Answer

It could be shortened to:

$var = filter_var($_REQUEST['var'] ?? 0, FILTER_SANITIZE_NUMBER_INT);

Instead of applying the null-coalescing operator to the entire expression, apply it only to the argument of the function.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement