#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2016
    Posts
    69
    Rep Power
    2

    PHP inside variable


    I am thinking I would like to do some of the messy work in a seperate file, create several variables and then call them on a different page.

    Is this a terrible idea? The variable would also contain PHP, I have tried doing it but I think the tags are causing problems. I have googled it endlessly but cant really find a solution. Which leaves me to believe its a bad idea.

    When I use plain text in the variable, and call it in the location I want it works. As soon as I add php tags it breaks.

    Thanks for looking.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,342
    Rep Power
    630
    You are correct, a bad idea. I believe what you want is to use an include file.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,810
    Rep Power
    4536
    An included file will run the PHP code in that file in the context of where it was included. So if you define any variables, functions or classes in it, they can be used in the original PHP script.

    Does that do what you want?
    -- Cigars, whiskey and wild, wild women. --
  6. #4
  7. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,394
    Rep Power
    9645
    To be clear, the problem is
    Originally Posted by SGC3
    The variable would also contain PHP,
    that. Defining variables in one file, including the file elsewhere, and using those variables is a common practice.
    PHP Code:
    <?php // include.php

    $time time();

    ?>

    <?php // page.php

    include "include.php";
    echo 
    $time;

    ?>
    Don't put code into a string and then execute the string like code, which is possible but I'm not going to say how.
    PHP Code:
    <?php // include.php

    $set_time '$time = time();';

    ?>

    <?php // page.php

    /* execute $set_time */
    echo $time;

    ?>
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2016
    Posts
    69
    Rep Power
    2
    Thanks guys.

    I think what I was looking to do is more along the lines of what requinix said in the second example. Here is the code I was going to put on a different page:

    PHP Code:
    $b1 =
         
    'if ($vacjan1->num_rows > 0) {
        // output data of each row
        while($row = $vacjan1->fetch_assoc()) {
            echo "<br>".$row["Name"];
        }
    } else {
        echo "No Vacation Picked" . "<br/>";
    }


    if ($statjan1->num_rows > 0) {
        // output data of each row
        while($row = $statjan1->fetch_assoc()) {
            echo  $row["Name"]. "<br/>";
        }
    } else {
        echo "<br>No Stats Picked";
    }
    $conn->close()' 
    There would be several of these. I was hoping to keep my index page clean. And just add $B1, $B2 $B3 etc, where I need.

    But looks like that isnt good practice
    Last edited by SGC3; October 10th, 2017 at 07:57 AM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,342
    Rep Power
    630
    Please use the proper tags when posting code. See the sticky at the top of this forum that says READ THIS BEFORE POSTING.

    You can do what you want except put what you call b1, b2, etc. in separate files then use 'include' b1, b2, etc. where needed.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2016
    Posts
    69
    Rep Power
    2
    Originally Posted by gw1500se
    Please use the proper tags when posting code. See the sticky at the top of this forum that says READ THIS BEFORE POSTING.

    You can do what you want except put what you call b1, b2, etc. in separate files then use 'include' b1, b2, etc. where needed.

    Thanks. I changed the tags.

    So I would need a separate include file for each string?
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,342
    Rep Power
    630
    That is one way. You could also put all those in a single file with each b1, b2, etc. being a function and using 'require' on the file just once. Then call the appropriate function as needed. Depending on your objectives there may be others but putting code in a string is NOT a way.

    P.S. We don't really know what you want to accomplish but this is beginning to sound like you need to change your code from procedural to OOP and use different methods as appropriate.
    Last edited by gw1500se; October 10th, 2017 at 08:13 AM.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  16. #9
  17. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,810
    Rep Power
    4536
    Use a function or class to keep your code "clean". Define it all in another file, include it at the beginning, then call the function/methods where you want the code executed.

    Doing it the way you're envisioning is indeed a bad practice.
    -- Cigars, whiskey and wild, wild women. --
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2016
    Posts
    69
    Rep Power
    2
    Thanks. I am looking into learning about functions. And PDO. Lots to take in

IMN logo majestic logo threadwatch logo seochat tools logo