Lee Posted September 30, 2006 Share Posted September 30, 2006 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. Quote Link to comment Share on other sites More sharing options...
intrik Posted September 30, 2006 Share Posted September 30, 2006 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 Quote Link to comment Share on other sites More sharing options...
Lee Posted September 30, 2006 Author Share Posted September 30, 2006 Ah, I see. Thanks for that ;DSo always single quotes for html output? Quote Link to comment Share on other sites More sharing options...
intrik Posted September 30, 2006 Share Posted September 30, 2006 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 goecho"<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? Quote Link to comment Share on other sites More sharing options...
Lee Posted September 30, 2006 Author Share Posted September 30, 2006 Makes perfect sense.Thanks, appreciate it ;D Quote Link to comment Share on other sites More sharing options...
Barand Posted September 30, 2006 Share Posted September 30, 2006 Have a read ofhttp://www.php.net/manual/en/language.types.string.php Quote Link to comment Share on other sites More sharing options...
Lee Posted October 1, 2006 Author Share Posted October 1, 2006 That was very helpful, thanks :) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.