How do I call a PHP script from within another PHP script?

by on February 2, 2010

Q: Is there are way to call a PHP script within PHP?

I have tired exec() and system() but when running the script from my host (Network solutions) they do not allow command line/cron jobs to run. I have also tried passing a form as well.

All I need is that upon completion of script A then script B (independent of script A) is called by script A.


9 Responses to “How do I call a PHP script from within another PHP script?”
  1.  

    You will want to use include(), include_once(), require(), or require_once().

     
    •  

      that does not work, as those functions invoke the presence of the file, but do not call the file for execution. remember, there is no relation between the calling script and the script being called… by invoking require, require_once, etc
      the calling file will inherite all scope from invoking script…

      probably will have to rely on jquery to make it happen…

      thanks

       
      •  

        I think you need to be a lot more specific then because using include or require will let you do just about anything you could ever want to do unless you are calling a different scripting language. You can do include(‘footer.php’) to do simple html stuff but you can also do include(‘myscript.php’) that would have the code that calls the classes/methods you want to process. There is nothing you can do with jquery that you could not do with a simple include or require because jquery does nothing more than calling a function and returning the data.

         
        •  

          hi matt,

          correct. in a perfect world i will use exec(), once script A completes
          execution then it call script B to run from the command line—but since
          using exec() could be a security risk and using safe mode for php is not an
          option… i simply want to mimic:

          $cmd = “blah.php”;
          exec(escapeshellcmd($blah));

          an option is also to use jquery, dhtml, etc to “manually” execute the
          command… one way could be using the onload function within the body tag
          in html

          hope that simplifies my inquiry—thank you for your feedback and time :-)

          pancho

           
          •  

            I guess I am not understanding why you think exec() does anything different than include() does. The only difference between exec() and include() is that exec() will run on the command line (PHP via CLI) where include will run right within your existing script.

            If you have control over both scripts it is very easy to do what you want to do just by making the include call the main function in the other file that would be called when you execute it on the command line.

             
  2.  

    i guess that is where i lack the php skills. the include(blah.php) file does not have any functions—only sql queries that are processed via variables and a few stored procedure calls…. no functions
    i have tried
    include ‘blah.php’;
    echo $variable in blah.php

    calling from script A… no go…

     
    •  

      The only issue I could see you having is if you had a sql connection already in the first script. If you do I would suggest closing that sql connection before calling the include.

      Other than that you should have no problems. Remember doing an include(‘header.php’) and include(‘footer.php’) was one of the first methods people used to create templates in PHP. So we know it can be done.

      I have no idea where $variable is getting set before you echo it out either but if you are testing with a simple script that just has echo ‘hello world’ in it and that is not working then the problem is you might not have the correct include path.

       
  3.  

    will give it a try… going to close sql_connection first and see if that works…
    should include() only without calling a function, variable work?
    thanks!!!

     
    •  

      You should destroy everything related to any sql connections and by that I mean if you have a handle then unset() it and make sure to disconnect and if you have a username, password, host, etc store in variables then unset() them as well.

      I am not sure what you mean by without calling a function but here are two examples of an include file that you can use

      1.
      <?php

      echo ‘hello world’;
      ?>

      2.
      <?php

      my_hello_world_function();

      function my_hello_world_function()
      {
      echo ‘hello world’;
      }
      ?>

      Both of those will work just fine and will give you the same results. One just has the code right in it while the other uses a function to organize the code. You could even go a step further and put everything in classes and then call the classes via $myHelloWorldObj = new HelloWorld(); and then do a echo $myHelloWorldObj->message;

      So in short you can do anything you want. I have a feeling the big problem here is that you are using other peoples scripts and you don’t really understand exactly what you are supposed to be doing. I suspect you installed some software and it required you run script one and then run script two and you are trying to make it easier on yourself by making them run at the same time. Obviously I could be way off but anything you want can be done it is just a matter of reading up on it and figuring out how to do it.