Praetorian Posted May 26, 2008 Share Posted May 26, 2008 Just thought I would share this with anyone who is interested and doesn't know. (IE.. PHP newbs like me).If you find yourself constantly forgetting to wrap html in quotes, or forgetting to escape quotes, or forgetting which quotes go where when using html in your script... here's a quick way around that.You can close your php before the html, and open it again on the otherside without interrupting the script. Here's an example from one of my scripts.You'll notice that I close off the php before I reach the html, and simply type it straight in instead of using echo. Then I open the php back up right after. Just make sure you remember to pick up exactly where you left off with the php, as if you had used the echo function. I place the closing/opening tags on the same level as the html to make it easy to spot.The script will act exactly the same as if you'd used echo. The html in this example will only appear if the IF statement returns true. Also remember, if you use variables in the html to wrap them in quick-tags. (IE.. <?= $var; ?>)Hope this helps. <?php include 'includes/connect_string.txt'; $letter = $_GET["letter"]; $o = $_GET["o"]; if (!empty($_GET['o'])) { $q = mysql_query("SELECT * FROM dictionary WHERE origin LIKE '$_GET[o]%' ORDER BY word ASC"); while($row = mysql_fetch_array($q)) { ?> <h2 class="words"><a href="<?= $_SERVER['PHP_SELF']; ?>?s=<?= $row[word]; ?>"><?= $row[word]; ?></a></h2> <?php } }?> Link to comment Share on other sites More sharing options...
Wander Posted May 26, 2008 Share Posted May 26, 2008 remember tho, that <?=$something?> only works when short-tags are enabled in ure php-ini settings (if im right) Link to comment Share on other sites More sharing options...
aspnetguy Posted May 26, 2008 Share Posted May 26, 2008 remember tho, that <?=$something?> only works when short-tags are enabled in ure php-ini settings (if im right)I believe they are also being removed in PHP6 along with support for ASP style tags like <% %>. Link to comment Share on other sites More sharing options...
Jesdisciple Posted May 26, 2008 Share Posted May 26, 2008 This is a very nice (although confusing) technique, but sometimes you want to handle your XML/HTML as a string value. In that case and if you still have trouble with the quotes, you can still use heredocs (parsed) or nowdocs (unparsed). But I have read that heredocs are slow, and this might also be true of nowdocs. Link to comment Share on other sites More sharing options...
Wander Posted May 26, 2008 Share Posted May 26, 2008 when you wanna have it as a string u can also do something like function something(){ $before = ob_get_contents(); ob_clean(); ?> <!--whatever u wan in the string--> <?php $in_string = ob_get_contents(); ob_clean(); echo $before; // now what u did between ?> and <?php is in a string instead of outputted..} but i dunno how memory-intensitive or how slow it is Link to comment Share on other sites More sharing options...
Praetorian Posted May 27, 2008 Author Share Posted May 27, 2008 That's a bummer if they're removing quick tags. I hope they replace it with something. It's much quicker than having to type <?php echo $var ?> Link to comment Share on other sites More sharing options...
Ingolme Posted May 27, 2008 Share Posted May 27, 2008 It's hardly shorter than <?php echo $var; ?>. I've never even used it before. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.