views:

84

answers:

5

In my code editor, it normally displays PHP code in bold blue print. I'm finding some of my code is not showing up as bold blue print. Can someone please look at the code below and tell me if I'm doing something wrong? Thanks in advance.

    <td class="hr"><?php echo htmlspecialchars("payroll number")."&nbsp;" ?></td>
    <td class="dr"><input type="text" name="GevityNo" maxlength="10" value="<?php echo str_replace('"', '&quot;', trim($row["GevityNo"])) ?>"></td>
</tr>
<tr>
    <td class="hr"><?php echo htmlspecialchars("employee name")."&nbsp;" ?></td>
    <td class="dr"><textarea cols="25" rows="1" name="employee_name" maxlength="75"><?php echo str_replace('"', '&quot;', trim($row["employee_name"])) ?></textarea></td>
</tr>
<tr>
    <td class="hr"><?php echo htmlspecialchars("Sex")."&nbsp;" ?></td>
    <td class="dr"><select name="Sex">
    <option value=""></option>
    <?php
        $lookupvalues = array("male","female");
        reset($lookupvalues);
        foreach($lookupvalues as $val){
            $caption = $val;
            if ($row["Sex"] == $val) {$selstr = " selected"; } else {$selstr = ""; }
            ?>
            <option value="<?php echo $val ?>"<?php echo $selstr ?>><?php echo $caption ?></option>
    <?php } ?>
    </select>
</td>
+5  A: 
$ cat > /home/tkn/tmp/foo.php
  ... snip ...
^C
$ php -l /home/tkn/tmp/foo.php 
No syntax errors detected in /home/tkn/tmp/foo.php

So, yes it's valid php code. Get a better editor.

troelskn
+1  A: 

The syntax-highlighting is part of the code editor you are using. If your code is working fine, there is no problem then.

Sarfraz
A: 

Usually these problems are related with wrongly escaped \". Without seeing the whole code is hard to say.

Anyway, double check that this part of your code is ok:

<?php echo $selstr ?>><?php echo $caption ?><

Check the >>< part

Carlos Tasada
That's valid, even if it's not pretty...
Rowland Shaw
A: 

According to http://www.php.net/manual/en/language.expressions.php , the semicolon terminates an expression (as opposed to seperating two expressions). I would guess maybe that in code like <?php echo $val ?>, must there not be a semicolon to make it 100% conforming?

Self-correction: http://www.php.net/manual/en/language.basic-syntax.instruction-separation.php

The closing tag of a block of PHP code automatically implies a semicolon

phresnel
A: 

To make your code more readable, think about using the alternative syntax for control structures:

<?php foreach(array("male","female") as $val): ?>
    <option value="<?php echo $val ?>" <?php echo ($row["Sex"] == $val)?'selected="selected"' : '' ?> > <?php echo $val ?> </option>
<?php endforeach; ?>

I my opinion this way it is easier to see the structure of the code.

Felix Kling
This doesn't really change much: `: ... end` is really no different to `{ ... }`. The important part to take from this answer, whichever way you choose, is to indent the PHP and HTML consistently.
bobince
@bobince: I no that it doesn't change much, but when you e.g. nested control structures, I guess one can get really confused by all the `<?php } ?>` because it is not necessarily clear where it belongs to. I just wanted to point out that one you check his code easier for errors if it is more readable.
Felix Kling