douji8347 2012-01-15 18:54
浏览 21
已采纳

php如果条件语法错误

I have these code:

<?php 
$i=0;
while (true):
   echo "test";
   $i<4 ? $i++ : break;
endwhile; 
?>

error message: Parse error: syntax error, unexpected T_BREAK in C:\xampp\htdocs\data\index.php on line 5

there is a syntax error with the if condition, I can really make out what. can anybody help. thanks a lot.

  • 写回答

2条回答 默认 最新

  • doukong1391 2012-01-15 18:56
    关注

    The ternary operator is used for assignment or to return a value. You cannot place break in the else (:) case of a ternary operation.

    // Assignment
    $x = $i<4 ? $i : 0;
    // For a value returned into a function argument $i or 0
    do_something($i<4 ? $i : 0);
    

    It cannot be used for a regular else case containing a language construct as you're doing with break. Instead use a regular if()

    $i = 0;
    while (true):
      if ($i<4) {
        $i++
      }
      else {
        break;
      }
    endwhile;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案