简体   繁体   中英

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.

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 :)

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. And it's not supported on all installs like it use to be. short_open_tag must be on to use short tags.

<?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 <? . 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 . (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. 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. You should clean up your code so that you don't have to have so many small bits of PHP. Doing it this way makes it virtually impossible to keep track of your tags in HTML and your braces in 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.

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.

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 . 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. You'd get a lot of false hits.

The PHP and HTML is fine ( fsvo. "fine"; for one thing, turn off smart-tags !). Dreamweaver doesn't know how to highlight it properly.

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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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