简体   繁体   English

PHP:为什么我收到这个错误??? “语法错误,意外'{'”

[英]PHP: why i am getting this error ??? “syntax error, unexpected '{'”

i wanted to arrange this arrays element in assending order and wrote the below code:我想按顺序排列这个 arrays 元素并编写以下代码:

<?php 
  $a=array("z","s","a","j","t","b");
  for($i=0;$i<=6;$i++)
  {
  if ($i[0]<$i[1]) { echo $i[1]; }
  else if ($i[1]<$i[2]) { echo $i[2]; }
  else if ($i[2]<$i[3]) { echo $i[3]; }
  else if ($i[3]<$i[4]) { echo $i[4]; }
  else if ($i[4]<$i[5]) { echo $i[5]; }
  else if ($i[5]<$i[6]) { echo $i[6]; }
  else if ($i[6]<$i[7]) { echo $i[7]; }
  else if ($i[7]<$i[8]) { echo $i[8]; }
  else if ($i[8]<$i[9]) { echo $i[9]; }
  else if ($i[9]<$i[10]) { echo $i[10]; }
  else if ($i[10]<$i[11]) { echo $i[11]; }
  else ($i[11]<$i[12]) { echo $i[12]; }

 }

?>

but i an getting the following error:但我收到以下错误:

Parse error: syntax error, unexpected '{' in C:\wamp\www\arange.php on line 16解析错误:语法错误,第 16 行 C:\wamp\www\arange.php 中出现意外的“{”

how can i rectify it我该如何纠正它

This snippet is the problem:这个片段是问题:

else ($i[11]<$i[12]) { echo $i[12]; }

Either edit it into elseif or remove the ($i[11]<$i[12]) .将其编辑为elseif或删除($i[11]<$i[12])

I'd do this differently.我会以不同的方式做这件事。 Consider using PHP's built in sort() function.考虑使用 PHP 的内置sort() function。

$a = array("z","s","a","j","t","b");
sort($a);
foreach ($a as $element) {
    echo "$element\n";
}

Also read about the foreach statement .另请阅读foreach 语句

if ($i[5]<$i[6]) { echo $i[6]; }

actually will output something like this;实际上会 output 是这样的;

if ( b < ) { echo ; } 

That why you see kinda error...这就是为什么你看到有点错误......

$b = '';
$a=array("z","s","a","j","t","b");
foreach($a as $i) if($i > $b) $b = $i;
echo $b;

Check out the manual for a clear example of the elseif/else if syntax.查看手册以获取elseif/else if语法的清晰示例。 The else part in your code is the problem.代码中的 else 部分是问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM