Jump to content


Photo

Tag order


  • Please log in to reply
6 replies to this topic

#1 Lee

Lee
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 30 September 2006 - 08:33 AM

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

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

This works fine..

but when outputing a table..

   <?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 } ?>

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.

#2 intrik

intrik
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 30 September 2006 - 08:38 AM

Because

<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>

^^ 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

<?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>';
         }
 ?>

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

#3 Lee

Lee
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 30 September 2006 - 08:46 AM

Ah, I see. Thanks for that  ;D

So always single quotes for html output?

#4 intrik

intrik
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 30 September 2006 - 09:45 AM

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.


<?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.
?>

Makes sense, yeah?

#5 Lee

Lee
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 30 September 2006 - 10:32 AM

Makes perfect sense.

Thanks, appreciate it  ;D

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 30 September 2006 - 10:54 AM

Have a read of

http://www.php.net/m...ypes.string.php
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 Lee

Lee
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 01 October 2006 - 07:45 AM

That was very helpful, thanks :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users