Jump to content

Archived

This topic is now archived and is closed to further replies.

Lee

Tag order

Recommended Posts

I'm learning from a video, and noticed an opening & closing tag where I wouldn'e expect it (see below).

[code]
  <?php
  $Apples = 10;
  $Oranges = 20;
  $Bananas = 5;
 
  if ($apples != 8)
 
  echo "<center>Apples are more than 8</center> <p>"
  ?>
 
  <?php
[/code]

This works fine..

but when outputing a table..

[code]
  <?php
 
  if ($Oranges >= 10 && $Bananas == 5) { ?>
          <table align="center" width="450" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center" valign="middle" bgcolor="#D6DCEB">Those conditions were true </td>
  </tr>
  <tr>
    <td align="center" valign="middle" bgcolor="#F3F7FC">This table appears </td>
  </tr>
</table>
        <?php } ?>
[/code]

It works perfectly, but why does there need to be a closing php tag after the opening curley brace, and an opening php tag before the closing curley brace in order for this to work?
The guy narrating the vid didn't explain this, I just noticed it on his page.

Share this post


Link to post
Share on other sites
Because

[code] <table align="center" width="450" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center" valign="middle" bgcolor="#D6DCEB">Those conditions were true </td>
  </tr>
  <tr>
    <td align="center" valign="middle" bgcolor="#F3F7FC">This table appears </td>
  </tr>
</table>[/code]

^^ is HTML. the stuff inside <? and ?> is php, so when you want to use php you got to open / close the php tags.

Alternatively you can do this

[code]<?php
 
  if ($Oranges >= 10 && $Bananas == 5) {
          echo'<table align="center" width="450" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center" valign="middle" bgcolor="#D6DCEB">Those conditions were true </td>
  </tr>
  <tr>
    <td align="center" valign="middle" bgcolor="#F3F7FC">This table appears </td>
  </tr>
</table>';
        }
?>[/code]

^^ Then there's no need to be opening and closing tags all the time

Share this post


Link to post
Share on other sites
Ah, I see. Thanks for that  ;D

So always single quotes for html output?

Share this post


Link to post
Share on other sites
Nope, I just couldn't be bothered escaping all your quotes so they'd parse :).

echo"<table align=\"center\" width=\"450\" cellspacing=\"0\"";

And so on.

Otherwise if you go

echo"<table align="center" width="450" cellspacing="0"";

The first " after align= and before center (in align="center"), will close the echo. So to get around it you do a \". A \ will escape any character in PHP.  I'm not that great at explaning things. But I use a single ' in the example I showed you before so that the first " wouldn't close it. Does that make sense?

echo'testing these quotes " abcd';

Is fine, because the echo is encapsulated in ', therefore " won't close it.

echo"testing these quotes ' abcd";

Is fine, because the echo is encapsulated in ", therfore ' won't close it.

echo"testing this " abncd";

^^ Is not fine, because the echo is encapsulated in ", therfore " will try and close it.

Makes sense?

Same goes with all php syntax.


[code]
<?php
$var = "test's"; //Is fine
$var = "test"s"s; //Is not
$var = 'test's'; //Is not
?>
See how the code is now broken above?

<?php
//The only other way around it is to do this.
$var = "test\"s"; //The \" will escape the character and won't be considered as syntax.
?>
[/code]

Makes sense, yeah?

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.