How can I set some code to be a variable?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • londres9b
    New Member
    • Apr 2010
    • 106

    How can I set some code to be a variable?

    I need help using quotes on PHP.

    I have this code:

    Code:
    <?php
    $myfile = "$xxx.txt";
    $work = fopen ($myfile, "r");
    $hits = fread ($work, filesize($myfile));
    fclose ($work);
    echo "xxxx: <b>$hits</b>";?>);
    $filenameb = "stats.php";
    $fdb = fopen ($filenameb, "a");
    fwrite ($fdb,$contentsb);
    fclose ($fdb);
    ?>
    And what I want is to set all that code as a variable.
    But I can't because of the double quotes! And if I use single quotes then the code won't work.

    What I have is this:

    Code:
    $MYVARIABLE = "
    
    <?php
    $myfile = "$xxx.txt";
    $work = fopen ($myfile, "r");
    $hits = fread ($work, filesize($myfile));
    fclose ($work);
    echo "xxxx: <b>$hits</b>";?>);
    $filenameb = "stats.php";
    $fdb = fopen ($filenameb, "a");
    fwrite ($fdb,$contentsb);
    fclose ($fdb);
    ?>
    
    ";
    You can see... I have plenty of double quoutes there...

    Is there any way of doing this?
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    Look at heredoc statements.

    Also, read up on escaping strings with '\' and using addslashes.

    Comment

    • londres9b
      New Member
      • Apr 2010
      • 106

      #3
      Thank you so much!

      Comment

      • TheServant
        Recognized Expert Top Contributor
        • Feb 2008
        • 1168

        #4
        Originally posted by londres9b
        Thank you so much!
        No worries, let us know how you go. I am curious to find out how you plan to use this variable?

        Comment

        • londres9b
          New Member
          • Apr 2010
          • 106

          #5
          Oh, it's for a URL shortener statistics.

          That variable is going to be on the page that redirects the user and what it does (as you can see) is append to the statistics another code that opens (and reads) a txt file where the hits for that url have already been incremented.

          Sounds confusing... probably there is a simpler way of doing this but I'm on a "make-my-own-way" kind of mood.. :)

          When it's done (the Url Shortener) I'll tell you about it.

          Comment

          • londres9b
            New Member
            • Apr 2010
            • 106

            #6
            Ah....

            One thing: does Heredoc allow multiple semicolons?

            My code has and needs to have semicolons and I did:

            Code:
            $MyVariable = <<<EOD
            <?php
            $myfile = "$xxx.txt";
            $work = fopen ($myfile, "r");
            $hits = fread ($work, filesize($myfile));
            fclose ($work);
            echo "xxxx: <b>$hits</b>";
            ?>
            EOD;
            and it doesn't work.

            Can you help?

            ===============

            Sorry, it Does work.
            Last edited by londres9b; Jul 10 '10, 10:57 PM. Reason: Mistake.. It does work

            Comment

            • Samishii23
              New Member
              • Sep 2009
              • 246

              #7
              Try using file_get_conten ts()
              It would be a little easier on the eyes.

              Comment

              • londres9b
                New Member
                • Apr 2010
                • 106

                #8
                Originally posted by Samishii23
                Try using file_get_conten ts()
                It would be a little easier on the eyes.
                Thanks! It's much simpler this way

                Comment

                Working...