phpternary-operator

PHP Undefined index using ternary operator


my PHP version is 5.3.5.

The code:

$num = $_REQUEST['num'] ?: 7;

The error:

Notice: Undefined index: num in C:\path\to\file.php on line 34

Any suggestions?


Solution

  • $num = isset($_REQUEST['num']) ? $_REQUEST['num'] : 7;
    

    I assume you want $_REQUEST['num'] if it's set otherwise 7.