0

Updating:

The php is considering the code beyond the EOM; closing tag.

Here is how my script is structured:

<?php
switch($x){

case "a":
$var = <<< EOM;
...the html...
EOM;
break;

case "b":
...some code...
break;

}
?>

And the script is giving erro just after the EOM;

Old:

I'm using the following code.

$var = <<< EOM

... some html ...

<?php 
if (date("j",strtotime($row["dinicio"]))==$i){echo "selected='selected'";}
?>

EOM;>

but it fails with the following error

Parse error: syntax error, unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Apache\htdocs\ancp\adm\adm_functions.php on line 132

removing the $row['dinicio'] solves the problem. but i need this field

Any ideas?

Edit:

For more reference here a more 'complete' segment:

<select id="ddia" name="ddia" class="form-normal">
            <option value="-1">dia</option>
            <?php
                for ($i=1; $i<32; $i++){
                    echo "<option value='";
                    echo ($i<10)?"0":"";
                    echo "$i' ";
                    if (date("j",strtotime($row["dinicio"]))==$i){echo "selected='selected'";}
                    echo ">$i</option>\n";
                }
            ?>
        </select>
4
  • Uhm... are you sure that there's just "some html" between the $EOM and the <?php? Commented Dec 9, 2009 at 21:45
  • Yes. As you can see there are 131 lines before and nothing goes wrong. Futhermore as said removing the $row['dinicio'] makes the script works correctly. Commented Dec 9, 2009 at 21:49
  • What is the purprose of this? Is it supposed to put the PHP code into the variable, or is it supposed to include the result of that PHP code in the variable? Please fix the syntax errors as well... Commented Dec 9, 2009 at 21:52
  • This is an exceptionally poor construction. Try to refactor your code to eliminate the heredoc altogether. Consider using a template engine as this is apparently what you are going for anyway. Commented Dec 9, 2009 at 22:52

3 Answers 3

1

You can't escape into PHP in the middle of a HEREDOC (the <<< EOM ... EOM; block) - it is supposed to be just a string.

if you are simply echoing this straight out to the screen, then do something like:

...
switch($x){

  case "a":
  ?>
  <select id="ddia" name="ddia" class="form-normal">
    <option value="-1">dia</option>
    <?php $j = date('j', strtotime($row["dinicio"])); ?>
    <?php for($i = 1; $i <= 31; $i++): ?>
      <option value="<?php echo str_pad($i, 0, 2, STR_PAD_LEFT); ?>"<?php if($j == $i) echo ' selected="selected"'; ?>>
        <?php echo $i; ?>
      </option>
    <?php endfor; ?>
  </select>
  <?php
  break;
...
Sign up to request clarification or add additional context in comments.

2 Comments

Well, you evidently can parse PHP in a heredoc. But just because you can doesn't mean you should.
fair enough - to be honest, i didn't actually try and run it - it just felt wrong.
1

try

if (date("j",strtotime({$row["dinicio"]}))==$i){echo "selected='selected'";}

and using more spaces :D

1 Comment

Beware that selected='selected' is not valid (X)HTML. Escape the quotes using a backslash instead: selected=\"selected\"
0

Here's a cleaned up version of your code:

<select id="ddia" name="ddia" class="form-normal">
        <option value="-1">dia</option>
        <?php $j = date('j', strtotime($row["dinicio"]));
        for($i = 1; $i <= 31; $i++): ?>
        <option value="<?php echo str_pad($i, 0, 2, STR_PAD_LEFT); ?>"<?php if($j == $i) echo ' selected="selected"'; ?>>
            <?php echo $i; ?>
        </option>
        <?php endfor; ?>
</select>

1 Comment

thx Tatu, But now i got anther problem. The php is going beyond the EOM; ending tag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.