简体   繁体   English

PHP-这种PHP语法有什么问题?

[英]PHP - What's wrong on this PHP syntax?

I make this PHP script, but Dreamweaver points (as parsing of the code written) that this string is Incorrect. 我制作了这个PHP脚本,但是Dreamweaver指出(作为对编写代码的解析),该字符串不正确。

In any case it works perfectly, but maybe I'm wrong somethings : 无论如何,它都可以完美地工作,但也许我错了:

for($i=1; $i<=$npagine; $i++) {
?> <a <? if($i==$index) { ?> class="paginas" <? } else { ?> class="pagine" <? } ?> href="index.php?<?=$splitter_zone?>&index=<?=$i?>"><?=$i?></a> <?
}

Any ideas? 有任何想法吗?

The code looks okay. 该代码看起来还可以。 However, your code is very difficult to read - I can almost understand dreamweaver for choking on it :) 然而,你的代码是非常难以阅读-我几乎能理解Dreamweaver中窒息吧:)

Here is a simpler suggestion: 这是一个更简单的建议:

for($i=1; $i<=$npagine; $i++)
  {
   $class = ($i == $index ? "paginas" : "pagine");
   echo "<a class='$class' href='index.php?$splitter_zone&index=$i'>$i</a>";
  }

Not sure, but you are using the short tag for php. 不确定,但是您正在为php使用short标签。 And it's not supported on all installs like it use to be. 而且它并不像以前那样在所有安装中都受支持。 short_open_tag must be on to use short tags. 必须启用short_open_tag才能使用短标签。

<?php 
// bla
?>

not

<?
//bla
?>

The first thing that's wrong with it is that you should be using <?php to open your PHP tags, not just <? 首先,您应该使用<?php打开PHP标记,而不仅仅是<? . The short form has been deprecated, and may not always work correctly. 简写形式已被弃用,可能无法始终正常运行。

Additionally, the short form <?= to print the output is deprecated. 此外,不赞成使用简短格式<?=来打印输出。 You should be using <?php print or <?php echo . 您应该使用<?php print<?php echo (Yes, I know it makes the code longer... don't grumble about it! ;-)) (是的,我知道它会使代码更长……不要抱怨!;-))

This is probably what's breaking your program. 这可能是破坏您程序的原因。 New installations of PHP will choke on the short form PHP tags. PHP的新安装将使简短形式的PHP标签停滞不前。 Its as simple as that. 就这么简单。

But while I'm here... The second problem you have is the horrible mixing in and out of PHP to and from HTML. 但是,当我在这里时...您遇到的第二个问题是PHP与HTML之间的混入和混入。 You should clean up your code so that you don't have to have so many small bits of PHP. 您应该清理代码,以便不必拥有太多PHP。 Doing it this way makes it virtually impossible to keep track of your tags in HTML and your braces in PHP. 以这种方式进行操作实际上几乎无法跟踪HTML中的标签和PHP中的括号。 Almost guaranteed to lead to errors, and very difficult to read when you come back to it two years down the line to make a bug fix. 几乎可以保证会导致错误,并且在两年后再次进行错误修复时很难读懂。

To solve this, I would suggest writing your code more like this: 为了解决这个问题,我建议您更像这样编写代码:

<?php
for($i=1; $i<=$npagine; $i++) {
    if($i==$index) {$class='paginas';} else {$class='pagine';}
    ....  //output your HTML here, without the if() condition embedded in it.
}
?>

You could simplify that even further using a ternary operator. 您可以使用三元运算符进一步简化该操作。

Switching to the long-form PHP tags <?php actively discourages excessive switching between PHP and HTML in this way, so you may want to take the opportunity to re-write your code in a more readable form. 切换到长格式的PHP标签<?php积极地阻止以这种方式在PHP和HTML之间进行过多的切换,因此您可能想趁此机会以更易读的形式重新编写代码。

In a case like this, there's nothing wrong with using print or echo to output the whole of the HTML tag, rather than switching to HTML mode to print it. 在这种情况下,使用printecho输出整个HTML标签没有什么错,而不是切换到HTML模式进行打印。

So you could end up with code like this: 因此,您可能会得到如下代码:

<?php
for($i=1; $i<=$npagine; $i++) {
    $class = ($index == $i) ? 'paginas' : 'pagine';
    print "<a class='{$class}' href='index.php?{$splitter_zone}&index={$i}>{$i}</a>";
}
?>

Much simpler and easier to read, I'm sure you'll agree. 我相信您会同意的,它更加简单易读。

One final point I'd make is that I always advise to avoid using single-character variable names like $i . 我要说的最后一点是,我总是建议避免使用像$i这样的单字符变量名称。 Try to use something more descriptive to what you're using it for. 尝试使用更具描述性的内容。 It seems harmless enough, but imagine trying to search a large program for $i to find a bug. 这似乎已经足够无害了,但可以想象一下,尝试在大型程序中搜索$i以查找错误。 You'd get a lot of false hits. 您会得到很多错误的点击。

The PHP and HTML is fine ( fsvo. "fine"; for one thing, turn off smart-tags !). PHP和HTML很好( fsvo。 “ fine”;一方面,关闭smart-tags !)。 Dreamweaver doesn't know how to highlight it properly. Dreamweaver不知道如何正确突出显示它。

for($i=1; $i<=$npagine; $i++) {
  $class=$i==$index?"paginas":"pagine";
  echo"<a class='{$class}' href='index.php?{$splitter_zone}&index={$i}>{$i}</a>\r\n";
}

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

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