Question

I have a problem with the syntax for a heredoc. Let me show the code first:

    function format($user_id,$user_note,$image,$dt){

        //this is the problem
      if($image=='NULL'){
     //don't display image
     }

     else {
  echo '<img src="userpics/$image" width="480" height="480">';
}
         return <<<ENDOFRETURN
       <div class ="commentbox">
                         $date
                         </div>
                             <div class="leftpanel">

                           $user_note
                        $image
                                 <div class="date">
                                    $rel
                                 </div>
                             </div>
    ENDOFRETURN;

}

The $image variable comes from the database, it's either NULL or has a filename. If an image is null I don't want to display the <img> tag, if it does have a value then I want to show it. Do you know how I can solve this problem? I have been trying everything, but nothing has worked yet!! :))

Was it helpful?

Solution

If data in a MySQL database is NULL, you would check that with the is_null() function, not comparing to the string 'NULL':

function format($user_id,$user_note,$image,$dt)
{
  if(!is_null($image)){
     $image = "<img src=\"userpics/$image\" width=\"480\" height=\"480\">";
  }
  return <<<ENDOFRETURN
   <div class ="commentbox">$date</div>
   <div class="leftpanel">
     $user_note
     $image
     <div class="date">$rel</div>
   </div>
ENDOFRETURN;
}

Also, as others have mentioned, the end of the heredoc must not be indented.

EDIT: I just edited a few other things in your code and showed the whole function to give you a better idea.

OTHER TIPS

I can see several problems with your code, but where the heredocs syntax is concerned, try removing the spaces before ENDOFRETURN;.

There are several problems, the correct way would be something like:

function format($user_id,$user_note,$image,$dt){

    //this is the problem
    if($image !== NULL){
        $output .= '<img src="userpics/' . $image . '" width="480" height="480">';
    }
    $output .= <<<ENDOFRETURN
    <div class ="commentbox">
                     $date
                     </div>
                         <div class="leftpanel">

                       $user_note
                    $image
                             <div class="date">
                                $rel
                             </div>
                         </div>
ENDOFRETURN;

    return $output;
}

you see:

  • ENDOFRETURN; has no spaces before it

  • all output is returned in one ... return

For the sake of being thorough:

NULL is a keyword, just like: print, echo, if, for. Meanwhile,'NULL' (note the quotes) is a string, just like if you had typed 'if' (note the quotes, again) it would be a string, not the beginning of an if statement.

Your echo statement in single quotes as above, won't do what you intend it to. The variable $image will not be expanded in a single-quoted string. Reverse your single and double quoting instead as

echo "<img src='userpics/$image' width='480' height='480'>";

Also the ENDOFRETURN; at closing your heredoc must not have any spaces before it.

simply adjust

{spaces or tab}ENDOFRETURN;

to

{no spaces/tab}ENDOFRETURN;

rtm - exactly same problem as yours

It is very important to note that the line with the closing identifier must contain no other characters, except possibly a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including Mac OS X. The closing delimiter (possibly followed by a semicolon) must also be followed by a newline.

If this rule is broken and the closing identifier is not "clean", it will not be considered a closing identifier, and PHP will continue looking for one. If a proper closing identifier is not found before the end of the current file, a parse error will result at the last line.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top