SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Assign varaible to html outside of <?php ?>

    Hi, I was wondering if it is possible to do something like this:
    PHP Code:
    <html>
    <?php
    var =
    ?>
    <lots of html>
    <?php
    ;
    ?>
    Is there something like that, that will work?
    Since I know you can wrap html inside of while(){} structures...

    Thanks

  2. #2
    SitePoint Member
    Join Date
    Oct 2007
    Location
    Islamabad
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it is not possible in php because after assign operator php looks for the value and value should be either integer,string,char,array or object with terminator ";" at end like that
    $var = '<lots of html>';
    so if you want to wrap html code inside php you should write it in string format by using single or double quotes around it
    Taufeeq ur Rehman
    Software Engineer
    Skills: PHP,MySQL,JavaScript,Ajax

  3. #3
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ShadowUnseen View Post
    Hi, I was wondering if it is possible to do something like this:
    PHP Code:
    <html>
    <?php
    var =
    ?>
    <lots of html>
    <?php
    ;
    ?>
    Is there something like that, that will work?
    No, but you can use heredocs or output buffering:

    PHP Code:
    <?php

    ob_start
    ();
    ?>
    <lots of html>
    <?php

    $var 
    ob_get_clean();

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And if you want to remain the HTML in the screen and get the buffer contents in a variable then do like this:
    PHP Code:
    <?php
    ob_start
    ();
    ?>
    <html tags>
    <?php
    $var 
    ob_get_contents();
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Or, if there is ALOT of HTML, you could store it in a separate file. Then, use something like:
    PHP Code:
    <?
    $var 
    file_get_contents("lotsofhtml.html");
    ?>
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hhmm ok thanks for all your replies

    I've thought of another method that works for me, since all I want to do is place the html in a seperate place to where it is defined. I used the following:
    PHP Code:
    <?php
    function get_html(){
    ?>
    <html>
    <?php
    }
    ?>

  7. #7
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    365
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try
    PHP Code:
    <?
    $string 
    = <<<ENDOFSTRING 
    <html>
    <
    head>
    <
    title></title>
    </
    head>
    <
    body>
    .
    .
    .
    </
    body>
    </
    html>
    ENDOFSTRING;  
    ?>

  8. #8
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ShadowUnseen View Post
    hhmm ok thanks for all your replies

    I've thought of another method that works for me, since all I want to do is place the html in a seperate place to where it is defined. I used the following:
    PHP Code:
    <?php
    function get_html(){
    ?>
    <html>
    <?php
    }
    ?>
    That's also not the bad idea. Depends on your requirement.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  9. #9
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    However, it cannot be used if you want to modify it or use it in any way, other than echoing it.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  10. #10
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    True, and it also gets really annoying when you try to pass variables contained within it, to other functions

    The buffer output works really well

  11. #11
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    But again, I would find it better to put the HTML in a separate file - that way it's easier to manage, the PHP script looks tidier, and you can put it all into a variable through file_get_contents()
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  12. #12
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    But again, I would find it better to put the HTML in a separate file - that way it's easier to manage, the PHP script looks tidier, and you can put it all into a variable through file_get_contents()
    If someone wanna have some dynamic variables in between the HTML, then it will not allow if they save in a separate file. So it is better to have in the same file and ob_get_contents() or ob_get_clean().
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •