I always hated writing big queries in php by concatenating strings. Its very ugly and can get hard to follow with quotes and double quotes. Heredoc to the rescue. If you’re unfamiliar with heredoc, its basically a syntax with php (and some other perl based languages) to type blocks of text without having to escape characters. This can also be useful when writting our big multi join/union/where/group/having/order by query. Here’s how:

$sql=<<<EOD
select *
from pages
where pagename='$pn'
EOD;

You have to be very cautious though with spacing. Heredoc does not like spaces on the lines with the opening and closing identifiers (EOD in this case), however everything in between can be spaced to suit your fancy. Give it a shot!

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • StumbleUpon
  • Technorati
  • Twitter

Related posts:

  1. Exploring Various SQL RegEx Syntax
  2. JsonSQL Ported to PHP
  3. Should ODBC Classes have a Syntax Layer?
  4. Handling PHP Form Arrays with jQuery
  5. RETS Retrieval with PHP and VieleRETS: Part 5