Jump to content

Tag order


Lee

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.
Link to comment
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
Link to comment
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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.