Here document

Discussion in 'PHP' started by nrvncanada, Dec 20, 2009.

  1. #1
    I am quite new to php. This code is not working and shows error at the last line

    <html>
    <head><title>Example of here document: HTML embedded inside PHP script</title></head>
    <body>
    <?php
    $name = "Joe Smith";
    $occupation = "Programmer";
    echo <<<EOF

    This is a heredoc section.
    For more information talk to $name, your local $occupation.

    Thanks!

    EOF;

    $toprint = <<<EOF

    Hey $name! You can actually assign the heredoc section to a variable!

    EOF;
    echo strtolower($toprint);


    ?>
    </body>
    </html>

    Can anyone please tell me where the problem is? I think code is right, may be some header file is required for here document.
     
    nrvncanada, Dec 20, 2009 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Works fine for me. Outputs
    <html>
    <head><title>Example of here document: HTML embedded inside PHP script</title></head>
    <body>
    
    This is a heredoc section.
    For more information talk to Joe Smith, your local Programmer.
    
    Thanks!
    
    hey joe smith! you can actually assign the heredoc section to a variable!
    </body>
    </html>
    HTML:
    What is the error you are getting?
     
    JAY6390, Dec 20, 2009 IP
  3. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #3
    What was the error?
     
    javaongsan, Dec 20, 2009 IP
  4. nrvncanada

    nrvncanada Guest

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    First of all thanks for having the concern

    I tried to run the code in apache server. The browser shows something like " unable to resolve line no 26" which happens to be the last tag ie </html>
     
    nrvncanada, Dec 21, 2009 IP
  5. nrvncanada

    nrvncanada Guest

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Exact error is

    Parse error: parse error in H:\EasyPHP 3.0\www\learningPHP5\Chap1\Examples\heredoc.php on line 26

    Please help me find where the problem lies
     
    nrvncanada, Dec 22, 2009 IP
  6. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    <html>
    <head><title>Example of here document: HTML embedded inside PHP script</title></head>
    <body>
    <?php
    $name = "Joe Smith";
    $occupation = "Programmer";
    echo "<pre>
    
    This is a heredoc section.
    For more information talk to $name, your local $occupation.
    
    Thanks!";
    
    $toprint = "
    
    Hey $name! You can actually assign the heredoc section to a variable!
    
    </pre>";
    echo strtolower($toprint);
    
    
    ?>
    </body>
    </html>
    PHP:
     
    xenon2010, Dec 22, 2009 IP
  7. nrvncanada

    nrvncanada Guest

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Still the same problem is occurring. If the solved code is working well on your server, may be problem is somewhere else. I presumed that header file might be needed. Or am I missing some required step to execute the code? I am using EasyPHP 3.0 package
     
    nrvncanada, Dec 25, 2009 IP
  8. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    use WAMP 2 server..
    it contains apache + mysql + php + sqlite etc..
    just open it and then place your script in www folder the run the script.
    and see if you still get errors..
     
    xenon2010, Dec 25, 2009 IP
  9. nrvncanada

    nrvncanada Guest

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Yes, code runs on Wampserver. Thanks a lot for taking time to solve the problem. Thanks once again buddy
     
    nrvncanada, Dec 30, 2009 IP
  10. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    no problem :)
    rep me up :D
     
    xenon2010, Dec 30, 2009 IP
  11. nrvncanada

    nrvncanada Guest

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I was trying to find out what went wrong with the previous PHP 3.0 package. I came to arrive at interesting conclusion and the problem was with indenting END; closing identifier for here document. When I remove any space before END; code runs well otherwise it gives parse error. This is little bis strange for me as I have read php takes care of multiple spaces.
     
    nrvncanada, Jan 11, 2010 IP