开发者

How to avoid a Netbeans error with an HTML block nested in PHP if statement

Consider the following example:

<table>
    <tr>
        <td>Row One</td>
    </tr>
    <?php
    if ($rowtwo)
    {
    ?>
    <tr>
        <td>Row Two</td>
    </tr>
    <?php
    }
    ?>
</table>

If $rowtwo is true, the second row is output, otherwise it is skipped.

The code works as desired, however I am evaluating Netbeans (7 beta) as a PHP IDE (instead of just using a text editor). Netbeans flags the code with an error:

Misplaced non-space characters insided [sic] a table.

Should I consider an alternate way of writing this code, or is Netbeans incapable of understanding this flow control wrapper for HTML output?

Edit

The example does not show the indentation I originally used, however various methods of indentation do not affect Netbeans in flagging it as an error.

Removing whitespace prior to the <?php and ?> tags as well as using endif; instead of curly braces both result in the same functionality, but are still flagged.开发者_开发百科

I think this is a problem and/or weak point with Netbeans' code parsing.


One of the advantages of PHP is being able to insert PHP code fragments right in your HTML. Your code above is fine. Alternatively, you can do it like this, but both are exactly the same.

<table>
    <tr>
        <td>Row One</td>
    </tr>
    <?php
    if($rowtwo) {
      echo "<tr>\n";
      echo "\t\t<td>Row Two</td>\n";
      echo "\t</tr>\n";
    }
    ?>
</table>


This is pretty typical syntax for presentational views in PHP and other similar languages. It's not elegant, and it's likely to confuse IDEs, but it's not generally considered bad practice.


Your code is valid php. Netbeans may be trying to validate it as HTML, but I don't know much about netbeans.

A couple suggestions:

  • don't put white-space before php opening tags (<?php) it makes it easier to scan a file and see where the php blocks are located
  • use the if (...): endif; blocks for HTML content:

I.E.:

<table>
  <tr>
    <td>Row One</td>
  </tr>
<?php if ($rowtwo): ?>
  <tr>
    <td>Row Two</td>
  </tr>
<?php endif; ?>
</table>

it helps so you can see where the if statement ends, rather than trying to determine which closing bracket is which. you can also do this for other control structures.

Edit for more details:

I break out of my html indentation with my php indentation. A next level block would look something like this:

<table>
  <tr>
    <td>Row One</td>
  </tr>
<?php if ($rowtwo): ?>
  <tr>
    <td>Row Two</td>
  </tr>
<?php   if ($condition): ?>
  <tr>
    <td>More Content</td>
  </tr>
<?php   endif; ?>
  <tr>
    <td>Even more content</td>
  </tr>
<?php endif; ?>
</table>

I should also mention that my body element is indented one level, so I rarely ever have html that is flush against my php tags. Often it's more like:

    <table>
<?php if ($condition): ?>
      <tr><td></td></tr>
<?php endif; ?>
    </table>

Which helps to make the PHP stand out.


It's almost acceptable by my standards (What I look for in other people's code). The only change I would make is to indent properly so that it's easier to see at a glance what's happening (make your code more readable). For example:

<table>
    <tr>
        <td>Row One</td>
    </tr>
    <?php
    if ($rowtwo) {
        ?>
        <tr>
            <td>Row Two</td>
        </tr>
        <?php
    }
    ?>
</table>

That way, it's trivial to see where the if construct ends at a glance...


It is not bad practice or wrong to a certain extent. However, a better practice is to separate your display logic from your business logic. Your IDE is probably assuming this, and therefore is confused with mixed code.

But again, nothing wrong with mixing html/php!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜