using <<< notation within a function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • samvb
    New Member
    • Oct 2006
    • 228

    using <<< notation within a function

    I have a small form generated from a function:

    Code:
    $form=<<<EOD
    <form action="index.php" method="POST" enctype="multipart/form-data" name="addform">
    
    <table id="contenttable" cellspacing="0">
    
    <tr><td class="error">{$errmsg}</td></tr>
    
    <tr class="firstrow"><td> Title</td><td><input type="text" size="50" name="ctitle" maxlength="150" value="{$_POST[ctitle]}"></td></tr>
    <input type="submit" class="buttons" name="go" value="Check"></td></tr>
    </table>
    </form>
    EOD;
    return $form;
    which works just fine. But when i try something like this, error (Parse error: syntax error, unexpected $end in C:\AppServ\www\ pv5\wana\fn_lis t.php on line 89) is generated:

    Code:
    function ListTitles(){
    $sql="SELECT title FROM nletters";
    
    $query=mysql_query($sql);
    
    $ttlfound=mysql_num_rows($query);
         
    	 mysql_free_result($query);
    	  if($ttlfound=="0"){
    	  return "No letters matching your critrea found.";
               }//if $ttlfound
    		   
    		   else{//at least one news found.
    		    //build the table now.
    			$form=<<<EOD
    			
    			EOD;
    			 
    		   }//close at least one title found.
    		   return $form;
    }
    How can i use <<< notation in this form? I don't want to echo the output but instead hold it within a variable $form. Is storing html form within string - like this:

    Code:
    $form="<table><tr><td><b>$title</b></td></tr></table>"
    the only way to go?
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    The closing line of the Heredoc syntax must be the first thing in a line. If you put anything before it, even a white-space, you get a parse error. This is true no matter where in the syntax you use it; inside a function, a class, or whatever else.

    This is the right way to use it inside of a function.
    Code:
    <?php
    function foo() {
        echo <<<TEXT
    
    The closing delimiter MUST be the first thing in the line.
    If you move it to match the indentation of the opening line,
    you get a parse error.
    
    TEXT;
    }
    ?>

    Comment

    Working...