Results 1 to 9 of 9
  1. #1
    Moderator
    Join Date
    April 6th, 2006
    Posts
    2,689
    PHP question - include(file.php) with variables
    Hi folks, I'm not a programmer, but have a question.. someone built a great script for me that combined 2 files, but disappeared before implementation was finished.

    I want to run this at the top of my main script, ie. include('file.php')

    The php file needs variables to run properly, ie. file.php old.txt new.txt

    Is there a way to run this include file (in my main php script) with variables?

    I've tried some variations, with no luck.

    Thanks!!!
    T.

  2. #2
    Grandma broke her coccyx! Uncle Rico's Avatar
    Join Date
    May 8th, 2007
    Location
    North Carolina
    Posts
    2,238
    Do the variables change or are they constant?

    If they are constant, then just add them to the script itself.

    Otherwise, I would make the contents of the script into a function(), include the script in your file and then call the function and pass it the variables.

    script.....
    function mySnazzyFunction($var, $var2) {

    ....all the stuff your programmer did goes inside function......

    }


    and then call the function from whatever page you want....

    include("myscript.pl");

    $ret = mySnazzyFunction(old.txt, new.txt, etc);


    I guess it all depends on what you have which I have no idea at this point.

  3. #3
    Moderator
    Join Date
    April 6th, 2006
    Posts
    2,689
    Thanks for the quick response... the variables aren't constant, so I don't want to hard code them into the script.

    I will read your suggestion carefully and give it a try!

  4. #4
    Moderator
    Join Date
    April 6th, 2006
    Posts
    2,689
    Getting there.. I think. Created new *.pl file with content of script, and set the variables.

    When I try to run the consolidated script, it's complaining about unexpected T_STRING, which (I think) usually means a missing ; or invalid character, at a line that starts with fwrite.

    It works fine when run on it's own, as php.

    I copied the contents into the pl fie (without the open/close <?php, ?>) - would there be a need to keep this in php?

    Thanks a mil!
    T.

  5. #5
    Grandma broke her coccyx! Uncle Rico's Avatar
    Join Date
    May 8th, 2007
    Location
    North Carolina
    Posts
    2,238
    Are we talking about a perl script or a php script? I see you just wrote pl, maybe that was a typo?

  6. #6
    Moderator
    Join Date
    April 6th, 2006
    Posts
    2,689
    I took your instructions literally, thought your reference to *.pl was deliberate! I'm back to using this in php.

    I'm reading up on the function command now - you've pushed me in the right direction, thanks a mil.

  7. #7
    Moderator
    Join Date
    April 6th, 2006
    Posts
    2,689
    I've hard coded the variables for the time being, will revisit function later.

    Just need to get this done as the author isn't around to help (quite frustrating, new site launch going on right now).

    Thanks for the help, SB.

  8. #8
    Grandma broke her coccyx! Uncle Rico's Avatar
    Join Date
    May 8th, 2007
    Location
    North Carolina
    Posts
    2,238
    Welcome. Ooops, I had .pl on the brain. I should have used .php in my sample.

  9. #9
    Newbie Akarin's Avatar
    Join Date
    August 26th, 2009
    Location
    Florida Keys
    Posts
    31
    All you need to do is assign your variables before the require("file.php")
    Code:
      <?PHP
      $fname = "Howard";
     $lname = "Duck";
      require("file.php");
      ?>
    if they are passed via GET index.php?fname=Howard&lname=Duck
    Code:
      <?PHP
     extract($_GET);
      require("file.php");
      ?>
    or via POST
    Code:
     <?PHP
      extract($_POST);
       require("file.php");
       ?>
    the extract function basically does this
    Code:
     <?PHP
      $fname = $_GET['fname'];
     $lname = $_GET['lname'];
       require("file.php");
       ?>
    Hope this helps

  10. Newsletter Signup

+ Reply to Thread

Similar Threads

  1. PHP include bug?
    By ercole in forum GoldenCAN
    Replies: 10
    Last Post: April 27th, 2009, 03:28 PM
  2. what php include code do you use and...why?
    By Adrian in forum Programming / Datafeeds / Tools
    Replies: 23
    Last Post: February 28th, 2007, 01:03 PM
  3. php include with calendars script
    By jaynaud in forum Cusimano.com Scripts
    Replies: 1
    Last Post: April 8th, 2006, 12:27 PM
  4. How do I Include Ads in PHP
    By Doug247 in forum Programming / Datafeeds / Tools
    Replies: 3
    Last Post: April 20th, 2004, 08:29 AM

Posting Permissions

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