- How to use include within a function?
- 3 Answers 3
- file1.php
- index.php
- Execute a PHP script from another PHP script
- 12 Answers 12
- PHP Include
- Introduction to the PHP include construct
- PHP include example
- PHP include & variable scopes
- 1) Including outside a function example
- 2) Including within a function example
- Summary
How to use include within a function?
I have a large function that I wish to load only when it is needed. So I assume using include is the way to go. But I need several support functions as well -only used in go_do_it(). If they are in the included file I get a redeclare error. See example A If I place the support functions in an include_once it works fine, see Example B. If I use include_once for the func_1 code, the second call fails. I am confused as to why include_once causes the function to fail on the second call, it seems to not ‘see’ the code the second time but if nested functions are present, it does ‘see’ them. Example A:
?> Doing it'; nested_func() function nested_func() < echo ' in nest'; >?>
3 Answers 3
The problem with using include() within a function is that:
include 'file1.php'; function include2()
file1.php will have global scope. file2.php ‘s scope is local to the function include2 .
Now all functions are global in scope but variables are not. I’m not surprised this messes with include_once . If you really want to go this way—and honestly I wouldn’t—you may need to borrow an old C/C++ preprocessor trick:
If you want to go the way of lazy loading (which by the way can have opcode cache issues) use autoloading instead.
I have a large function that I wish to load only when it is needed. So I assume using include is the way to go.
Your base assumption is wrong. This kind of optimization is counter-productive; even if your function is hundreds of lines long there will be no noticeable benefit to hiding it from PHP’s parser. The cost for PHP to parse a file is negligible; real noticeable speed gains come from finding better algorithms or from better ways to talk to your database.
That said, you should be including the function definition in the included file. Rather than moving the body of the function into func_1.php , move the entire function into the file. You can then require_once the file containing the function inside each file where you need it, and be sure that it is included exactly once regardless of how many times you attempt to include it.
file1.php
index.php
require_once('file1.php'); include('table_of_contents.php'); test();
Execute a PHP script from another PHP script
How would I make my server run a php script by triggering it manually using php? Basically I have a pretty big cronjob file that is ran every 2 hours, but I want to be able to trigger the file manually myself without having to wait for it to load (i want it to be done on the server’s side). EDIT: I want to execute the file from a php file. Not command line.
The answers were only incorrect because you didn’t include vital details in your question. Given the details you provided, the answers are 100% correct. Now that you have described what you’re actually trying to do, the answers will improve. Very simple, you have no reason to be disgruntled. People are trying to help.
12 Answers 12
You can invoke a PHP script manually from the command line
hello.php Command line: php hello.php Output: hello world!
EDIT OP edited the question to add a critical detail: the script is to be executed by another script.
There are a couple of approaches. First and easiest, you could simply include the file. When you include a file, the code within is «executed» (actually, interpreted). Any code that is not within a function or class body will be processed immediately. Take a look at the documentation for include (docs) and/or require (docs) (note: include_once and require_once are related, but different in an important way. Check out the documents to understand the difference) Your code would look like this:
include('hello.php'); /* output hello world! */
Second and slightly more complex is to use shell_exec (docs). With shell_exec , you will call the php binary and pass the desired script as the argument. Your code would look like this:
$output = shell_exec('php hello.php'); echo "
$output"; /* output hello world! */
Finally, and most complex, you could use the CURL library to call the file as though it were requested via a browser. Check out the CURL library documentation here: http://us2.php.net/manual/en/ref.curl.php
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://www.myDomain.com/hello.php"); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) $output = curl_exec($ch); curl_close($ch); echo "
$output"; /* output hello world! */
Documentation for functions used
- Command line: http://php.net/manual/en/features.commandline.php
- include : http://us2.php.net/manual/en/function.include.php
- require : http://us2.php.net/manual/en/function.require.php
- shell_exec : http://us2.php.net/manual/en/function.shell-exec.php
- curl_init : http://us2.php.net/manual/en/function.curl-init.php
- curl_setopt : http://us2.php.net/manual/en/function.curl-setopt.php
- curl_exec : http://us2.php.net/manual/en/function.curl-exec.php
- curl_close : http://us2.php.net/manual/en/function.curl-close.php
PHP Include
Summary: in this tutorial, you will learn how to include code from a file using the PHP include construct.
Introduction to the PHP include construct
The include construct allows you to load the code from another file into a file. Here’s the syntax of the include construct:
include 'path_to_file';
Code language: PHP (php)
In this syntax, you place the path to the file after the include keyword. For example, to load the code from the functions.php file into the index.php file, you can use the following include statement:
// index.php file include 'functions.php';
Code language: HTML, XML (xml)
If PHP cannot find the ‘functions.php’ file in the src directory, it’ll issue a warning. For example:
Warning: include(functions.php): failed to open stream: No such file or directory in . on line 4 Warning: include(): Failed opening 'functions.php' for inclusion (include_path='\xampp\php\PEAR') in . on line 4
Code language: PHP (php)
When loading the functions.php file, PHP first looks for the functions.php file in the directory specified by the include_path . In this example, it’s ‘\xampp\php\PEAR’ . If PHP can find the functions.php file there, it loads the code from the file.
Otherwise, PHP searches the functions.php file in the directory of the calling script and the current working directory. If PHP can find the functions.php file there, it loads the code. Otherwise, it issues a warning if the file doesn’t exist.
When PHP loads the functions.php file, it actually executes the code inside the functions.php file. For example, if you place the following code in the functions.php file:
// functions.php function get_copyright() < return 'Copyright © ' . date('Y') . ' by phptutorial.net. All Rights Reserved!'; > echo get_copyright();
Code language: HTML, XML (xml)
and include the functions.php in the index.php file, you’ll see the following output when you run the index.php file:
Copyright © 2021 by phptutorial.net. All Rights Reserved!
Code language: CSS (css)
This demonstrated that the include construct does make PHP executes code in the functions.php file.
PHP include example
In practice, you’ll often use the include construct to the page elements from a general site design. For example, all pages in your website may have the same header and footer.
To avoid repeating these elements on multiple pages, you can place the code of the header and footer in separate files such as header.php and footer.php and include them on the pages.
Typically, you place the template files like header.php and footer.php in a separate directory. By convention, the name of the include directory is inc :
. ├── index.php ├── functions.php ├── inc │ ├── footer.php │ └── header.php └── public ├── css │ └── style.css └── js └── app.js
Code language: CSS (css)
The header.php file contains the code of the header of the page. It has a link to the style.css file located in the public/css directory:
html>
html lang="en"> head> meta charset="UTF-8" /> meta name="viewport" content="width=device-width, initial-scale=1.0" /> link rel="stylesheet" href="public/css/style.css"> title>PHP include Example title> head> body>Code language: HTML, XML (xml)
The footer.php file contains the code related to the footer of the page:
script src="js/app.js">
script> body> html>Code language: HTML, XML (xml)
In the index.php file, you can include the header.php and footer.php file like this:
include 'inc/header.php'; ?> h1>PHP include h1
> p>This shows how the PHP include construct works. p> include 'inc/footer.php'; ?>Code language: HTML, XML (xml)
If you run the index.php file and view the source code of the page, you’ll also see the code from the header.php and footer.php files:
html>
html lang="en"> head> meta charset="UTF-8" /> meta name="viewport" content="width=device-width, initial-scale=1.0" /> link rel="stylesheet" href="public/css/style.css" /> title>PHP include Example title> head> body> h1>PHP include h1> p>This shows how the PHP include construct works. p> script src="public/js/app.js"> script> body> html>Code language: HTML, XML (xml)
PHP include & variable scopes
When you include a file, all the variables defined in that file inherit the variable scope of the line on which the include occurs.
1) Including outside a function example
For example, the following defines the $title and $content variables in the functions.php :
// functions.php $title = 'PHP include'; $content = 'This shows how the PHP include construct works.';
Code language: HTML, XML (xml)
When you include the functions.php in the index.php file, the $title and $content variables become the global variables in the index.php file. And you can use them as follows:
include 'inc/header.php'; ?> include_once 'functions.php'; ?> h1> echo $title; ?> h1> p>
echo $content; ?> p> include 'inc/footer.php'; ?>Code language: HTML, XML (xml)
2) Including within a function example
However, if you include a file in a function, the variables from the included file are local to that function. See the following example:
include 'inc/header.php'; ?> include_once 'functions.php'; ?> function render_article() < include 'functions.php'; return " $title
$content "; > echo render_article(); ?> include 'inc/footer.php'; ?>
Code language: HTML, XML (xml)
In this example, we include the functions.php inside the render_article() function. Therefore, the $title and $content variables from the functions.php are local to the render_function() .
It’s important to note that all functions, classes, interfaces, and traits defined in the included file will have a global scope.