Hello every one
«require_once» and «require» are language constructs and not functions. Therefore they should be written without «()» brackets!
require_once may not work correctly inside repetitive function when storing variable for example:
function foo () require_once( ‘var.php’ );
return $foo ;
>
to make sure variable bar available at each function call , replace require once with require. eg situation : https : //stackoverflow.com/questions/29898199/variables-not-defined-inside-function-on-second-time-at-foreach
function foo () require( ‘var.php’ );
return $foo ;
>
> php check2 . php
result :
bar
bar
bar
bar
bar
There’s been a lot of discussion about the speed differences between using require_once() vs. require().
I was curious myself, so I ran some tests to see what’s faster:
— require_once() vs require()
— using relative_path vs absolute_path
I also included results from strace for the number of stat() system calls. My results and conclusions below.
METHODOLOGY:
————
The script (test.php):
$start_time = microtime ( true );
/*
* Uncomment one at a time and run test below.
* sql_servers.inc only contains define() statements.
*/
//require (‘/www/includes/example.com/code/conf/sql_servers.inc’);
//require (‘../../includes/example.com/code/conf/sql_servers.inc’);
//require_once (‘/www/includes/example.com/code/conf/sql_servers.inc’);
//require_once (‘../../includes/example.com/code/conf/sql_servers.inc’);
$end_time = microtime ( true );
$handle = fopen ( «/tmp/results» , «ab+» );
fwrite ( $handle , ( $end_time — $start_time ) . «\n» );
fclose ( $handle );
?>
The test:
I ran ab on the test.php script with a different require*() uncommented each time:
ab -n 1000 -c 10 www.example.com/test.php
RESULTS:
———
The average time it took to run test.php once:
require(‘absolute_path’): 0.000830569960420
require(‘relative_path’): 0.000829198306664
require_once(‘absolute_path’): 0.000832904849136
require_once(‘relative_path’): 0.000824960252097
The average was computed by eliminating the 100 slowest and 100 fastest times, so a total of 800 (1000 — 200) times were used to compute the average time. This was done to eliminate any unusual spikes or dips.
The question of how many stat() system calls were made can be answered as follows:
— If you run httpd -X and then do an strace -p , you can view the system calls that take place to process the request.
— The most important thing to note is if you run test.php continuously (as the ab test does above), the stat() calls only happen for the first request:
first call to test.php (above):
——————————-
lstat64 («/www», lstat64 («/www/includes», lstat64 («/www/includes/example.com», lstat64 («/www/includes/example.com/code», lstat64 («/www/includes/example.com/code/conf», .
lstat64 («/www/includes/example.com/code/conf/sql_servers.inc», open («/www/includes/example.com/code/conf/sql_servers.inc», O_RDONLY) = 17
subsequent calls to test.php:
——————————
open («/www/includes/example.com/code/conf/sql_servers.inc», O_RDONLY) = 17
— The lack of stat() system calls in the subsequent calls to test.php only happens when test.php is called continusly. If you wait a certain period of time (about 1 minute or so), the stat() calls will happen again.
— This indicates that either the OS (Ubuntu Linux in my case), or Apache is «caching» or knows the results of the previous stat() calls, so it doesn’t bother repeating them.
— When using absolute_path there are fewer stat() system calls.
— When using relative_path there are more stat() system calls because it has to start stat()ing from the current directory back up to / and then to the include/ directory.
CONCLUSIONS:
————
— Try to use absolute_path when calling require*().
— The time difference between require_once() vs. require() is so tiny, it’s almost always insignificant in terms of performance. The one exception is if you have a very large application that has hundreds of require*() calls.
— When using APC opcode caching, the speed difference between the two is completely irrelevant.
— Use an opcode cache, like APC!
Konstantin Rozinov
krozinov [at] gmail
How to include a file in already included files in php?
Problem occurs when I try to include file from included file like this: Contents of index.php: Contents of Dir1/File1.php: Contents of Dir1/File2.php Contents of Dir1/File3.php File2.php and File3.php don’t get evaluated, but when I change for example File2.php to File99.php (some non-existent file) But when I change contents of Dir1/File1.php to include files like this: then files get evaluated.
How to include a file in already included files in php?
i have a index page in which i included 2 files . when user click on link 1 (file one) will be included and when on 2 (file two) will be included in a certain div.
link-1 link-2 else if($_GET['page']==2) < include("uploads/aboutus.php"); >else < include("uploads/home.php"); >?>
now look at this home.php file which include when pressed on link 1
Now problem is occurred. Now in home.php i want to add 2 more links 3,4 in home.php and wanted to include files when user press 3,4 link.
if anybody have any idea please share?
Hard to follow 🙂 If I get it right (including files in home.php):
if page = 1, include home.php. Then there is another switch in home.php, which will include further php files?
What about this, just as example: index.php?page=3 index.php:
$page = $_GET['page']; if ($page == 1 || $page==3) < include('uploads/home.php'); >
home.php could look like this:
A better way would be probably to introduce a second parameter $_GET[‘section’] or something else.
Check the $_GET[‘section’] in home.php and include your files.
Anyway, both suggestions are very ugly for me!
PHP include_once Keyword, The include_once keyword is used to embed PHP code from another file. If the file is not found, a warning is shown and the program continues to run. If the file was already included previously, this statement will not include it again. The include keyword The require keyword The require_once keyword
PHP — Does an Include inside and IF will be included if the execution does not enter the IF?
The question its self-explanatory. Example:
Does the file «stackoverflow.php» will be included?
and a Complementary Question :
Look to this example:
Lets suposse that the file «thispage.php» is sent to a lot of computers every second. does including the IF codes inside «stackoverflow.php» will make the file smaller to send to people if the IF has not been triggered?
No, stackoverflow.php will not be included.
Actually, why didn’t you just try it?
The code is only executed on the web server, so it really depends if there’s content in the include, or more code.
If the code executes and doesn’t send anything to the client, then it wouldn’t make anything smaller either way.
Php — Error handling & workarounds allow_url_fopen and, But running remotely fetched PHP code is a ENORMOUS security risk, any PHP code included in this way can literally do anything you can. DON’T DO IT! If you have access to your webserver, you may have to find your php.ini file, for example: /etc/php5/apache/php.ini
Include or required
What should I use in the following statement? Include or required.
If in a function, I know that one, either include or require, only includes the file when called, the other one will include the file regardless. Am I correct?
The difference between include and require is that include will only emit a warning when the file is not found, and require will terminate with a fatal error.
If you are loading vital program parts, you probably want to go with require .
require() is identical to include() except upon failure it will also produce a fatal E_ERROR level error. In other words, it will halt the script whereas include() only emits a warning (E_WARNING) which allows the script to continue.
PHP — Does an Include inside and IF will be included if the, Stack Overflow Public questions & answers; Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Talent Build your employer brand ; Advertising Reach developers & technologists worldwide; About the company
Include() fails silently when called from an included file
I have a problem with including files on remote server (but on my local Windows PHP+Apache configuration it works fine. ).
Problem occurs when I try to include file from included file like this:
File2.php and File3.php don’t get evaluated, but when I change for example File2.php to File99.php (some non-existent file) I get standard warning that file doesn’t exist.
But when I change contents of Dir1/File1.php to include files like this:
sounds like you don’t have «.» (current folder) in your include path. You should modify your php.ini file to add it. You can find out the value on your live and test server by doing
this works every time for me:
Try using a full path include instead:
include(dirname(__FILE__) . "/Dir1/File3.php");
The Best PHP Examples, These are special codes that put characters in your string that represent typically invisible characters. Examples include newlines \n, tabs \t, and actual backslashes \\. You can also embed PHP variables in double quoted strings to have their values added to the string.