【问题标题】:PHP if-else statement throws syntax error unexpected if [duplicate]PHP if-else 语句抛出意外的语法错误 if [重复]
【发布时间】:2015-05-29 08:22:15
【问题描述】:

我有一个相当复杂的 if-else 语句,我需要帮助。

我只想检查 Session 中是否有值,如果有,则输出一个名为“alert-important”的字符串,它将作为未来更改的挂钩。

不起作用的部分是带有 if 语句的第二部分。

<div class="alert alert-{{ Session::get('flash_notification.level') . ' worksfine ' . if(Session::has('flash_notification.important') ? 'alert-important' : '') }} ">

我得到了错误

"syntax error, unexpected 'if' (T_IF)"

【问题讨论】:

    标签: php if-statement laravel


    【解决方案1】:

    这将是三元的,所以应该是

    (Session::has('flash_notification.important') ? 'alert-important' : '')

    【讨论】:

      【解决方案2】:

      看起来这句话是罪犯

      if(Session::has('flash_notification.important')

      简写if/else statements 不要使用if 关键字。这是正确的方法:

      (Session::has('flash_notification.important')

      【讨论】: