Search code examples
phpif-statementinline

PHP inline if statement always returns false


can't seem to start this working properly, any idea what could be the problem? The variable APP_NAME is equal to string 'test', if I do this if statement in a regular way it works as it should.

<?=form_radio('filter', 'y', $info['enable_categories'] == 'y' ? true : false, 'id="app" '.APP_NAME == "test" ? '' : 'disabled'.'')?>

The disabled parameter has to be put into the same last parameter as id.

But this way I get the false value even if APP_NAME equals 'test'.

The function form_radio has these parameters:

function form_radio($data = '', $value = '', $checked = FALSE, $extra = '') so the extra parameter has to be ID and also the "disabled" string if the value is not equal to 'test'.


Solution

  • When you write

    'id="app" '.APP_NAME == "test" ? '' : 'disabled'.''
    

    the operator precedence makes this evaluate as

    ('id="app" '.APP_NAME) == "test" ? '' : 'disabled'.''
    

    So you should use parentheses around the ternary clause:

    'id="app" '.(APP_NAME == "test" ? '' : 'disabled'.'')
    

    You can also get rid of the .''. Also the clause

    $info['enable_categories'] == 'y' ? true : false
    

    can be simplified to

    $info['enable_categories'] == 'y'