By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,393 Members | 1,405 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,393 IT Pros & Developers. It's quick & easy.

include/require cached and fopen is not?

P: n/a

Hi,

Please help me settle a geek argument :).

I believe that php caches included code whenever possible.
So given the 2 files bellow.

// file.php
-------------------------------------
<?php
$value = 'Some value';
?>
-------------------------------------

// file.txt
-------------------------------------
Some value
-------------------------------------

If I try and 'use' both files to set a value.
-------------------------------------
EXAMPLE A
<?php
$value = '';
include 'file.php';
echo $value; // 'Some value'
?>
-------------------------------------

-------------------------------------
EXAMPLE B
<?php
$fd = @fopen( 'file.txt', 'rb');
$contents = '';
while (!feof($fd)) {
$contents .= fread($fd, 8192);
}
fclose($fd);
echo $contents; // 'Some value'
?>
-------------------------------------
Ignoring the fact that Example A is simpler.

Is it true that 'EXAMPLE A' will open the file, (file.php), and assign
$value only once and then not read the file anymore, (unless it is
changed).
I am talking about in the life of the php.exe process, not in the life
of the script run itself.

Whereas EXAMPLE B will open, read, close the file every single time
there is a page request.

What do you think? Does include open, read, close the file in every
request? Or will the content of both files be kept for the life of the
process?

Reagards,

FFMG
--

'webmaster forum' (http://www.httppoint.com) | 'Free Blogs'
(http://www.journalhome.com/) | 'webmaster Directory'
(http://www.webhostshunter.com/)
'Recreation Vehicle insurance'
(http://www.insurance-owl.com/other/car_rec.php) | 'Free URL
redirection service' (http://urlkick.com/)
------------------------------------------------------------------------
FFMG's Profile: http://www.httppoint.com/member.php?userid=580
View this thread: http://www.httppoint.com/showthread.php?t=22555

Message Posted via the webmaster forum http://www.httppoint.com, (Ad revenue sharing).

Nov 24 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a
FFMG wrote:
Hi,

Please help me settle a geek argument :).

I believe that php caches included code whenever possible.
So given the 2 files bellow.

// file.php
-------------------------------------
<?php
$value = 'Some value';
?>
-------------------------------------

// file.txt
-------------------------------------
Some value
-------------------------------------

If I try and 'use' both files to set a value.
-------------------------------------
EXAMPLE A
<?php
$value = '';
include 'file.php';
echo $value; // 'Some value'
?>
-------------------------------------

-------------------------------------
EXAMPLE B
<?php
$fd = @fopen( 'file.txt', 'rb');
$contents = '';
while (!feof($fd)) {
$contents .= fread($fd, 8192);
}
fclose($fd);
echo $contents; // 'Some value'
?>
-------------------------------------
Ignoring the fact that Example A is simpler.

Is it true that 'EXAMPLE A' will open the file, (file.php), and assign
$value only once and then not read the file anymore, (unless it is
changed).
I am talking about in the life of the php.exe process, not in the life
of the script run itself.
No, this is not correct. It will be done on every page request.
Whereas EXAMPLE B will open, read, close the file every single time
there is a page request.
Correct.
What do you think? Does include open, read, close the file in every
request? Or will the content of both files be kept for the life of the
process?

Reagards,

FFMG


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Nov 24 '07 #2

P: n/a
FFMG wrote:
Hi,

Please help me settle a geek argument :).

I believe that php caches included code whenever possible.
So given the 2 files bellow.

// file.php
-------------------------------------
<?php
$value = 'Some value';
?>
-------------------------------------

// file.txt
-------------------------------------
Some value
-------------------------------------

If I try and 'use' both files to set a value.
-------------------------------------
EXAMPLE A
<?php
$value = '';
include 'file.php';
echo $value; // 'Some value'
?>
-------------------------------------

-------------------------------------
EXAMPLE B
<?php
$fd = @fopen( 'file.txt', 'rb');
$contents = '';
while (!feof($fd)) {
$contents .= fread($fd, 8192);
}
fclose($fd);
echo $contents; // 'Some value'
?>
-------------------------------------
Ignoring the fact that Example A is simpler.

Is it true that 'EXAMPLE A' will open the file, (file.php), and assign
$value only once and then not read the file anymore, (unless it is
changed).
I am talking about in the life of the php.exe process, not in the life
of the script run itself.

Whereas EXAMPLE B will open, read, close the file every single time
there is a page request.

What do you think? Does include open, read, close the file in every
request? Or will the content of both files be kept for the life of the
process?

Reagards,

FFMG

No, you are wrong. Stat functions are cached, but file reading and
writing are not.

On my IRC bot application, I want to change the response text without
restarting my bot, so I used 'include'.

Seems that PHP doesn't cache the included files.
Nov 25 '07 #3

P: n/a

Jerry Stuckle;105090 Wrote:
>

No, this is not correct. It will be done on every page request.
Whereas EXAMPLE B will open, read, close the file every single time
there is a page request.

Correct.
Are you saying that the included scripts are revalidated every single
time?

That doesn't seem very efficient/logical to me.
Are you sure?

Surely some code is in memory.

FFMG
--

'webmaster forum' (http://www.httppoint.com) | 'Free Blogs'
(http://www.journalhome.com/) | 'webmaster Directory'
(http://www.webhostshunter.com/)
'Recreation Vehicle insurance'
(http://www.insurance-owl.com/other/car_rec.php) | 'Free URL
redirection service' (http://urlkick.com/)
------------------------------------------------------------------------
FFMG's Profile: http://www.httppoint.com/member.php?userid=580
View this thread: http://www.httppoint.com/showthread.php?t=22555

Message Posted via the webmaster forum http://www.httppoint.com, (Ad revenue sharing).

Nov 25 '07 #4

P: n/a
FFMG wrote:
Jerry Stuckle;105090 Wrote:
>>
No, this is not correct. It will be done on every page request.
>>Whereas EXAMPLE B will open, read, close the file every single time
there is a page request.
Correct.

Are you saying that the included scripts are revalidated every single
time?

That doesn't seem very efficient/logical to me.
Are you sure?

Surely some code is in memory.

FFMG

Nope. They are revalidated every time. But it's pretty fast.

Examine the source code if you want. It's available on the PHP site.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Nov 25 '07 #5

P: n/a

Jerry Stuckle;105173 Wrote:
FFMG wrote:
Jerry Stuckle;105090 Wrote:
>
No, this is not correct. It will be done on every page request.

Whereas EXAMPLE B will open, read, close the file every single
time
>there is a page request.

Correct.
Are you saying that the included scripts are revalidated every
single
time?

That doesn't seem very efficient/logical to me.
Are you sure?

Surely some code is in memory.

FFMG

Nope. They are revalidated every time. But it's pretty fast.

Examine the source code if you want. It's available on the PHP site.
I am not doubting you, I just find it very strange that some code has
not been optimized.

I would have thought that some code, (or even sections of code), are
only parsed once.

That is why, (in the example given), I would have thought that EXAMPLE
A would be far more efficient than EXAMPLE B.
But essentially they are the same.

FFMG
--

'webmaster forum' (http://www.httppoint.com) | 'Free Blogs'
(http://www.journalhome.com/) | 'webmaster Directory'
(http://www.webhostshunter.com/)
'Recreation Vehicle insurance'
(http://www.insurance-owl.com/other/car_rec.php) | 'Free URL
redirection service' (http://urlkick.com/)
------------------------------------------------------------------------
FFMG's Profile: http://www.httppoint.com/member.php?userid=580
View this thread: http://www.httppoint.com/showthread.php?t=22555

Message Posted via the webmaster forum http://www.httppoint.com, (Ad revenue sharing).

Nov 25 '07 #6

P: n/a
FFMG wrote:
Jerry Stuckle;105173 Wrote:
>FFMG wrote:
>>Jerry Stuckle;105090 Wrote:
No, this is not correct. It will be done on every page request.

Whereas EXAMPLE B will open, read, close the file every single
time
>>>>there is a page request.
>
Correct.

Are you saying that the included scripts are revalidated every
single
>>time?

That doesn't seem very efficient/logical to me.
Are you sure?

Surely some code is in memory.

FFMG

Nope. They are revalidated every time. But it's pretty fast.

Examine the source code if you want. It's available on the PHP site.

I am not doubting you, I just find it very strange that some code has
not been optimized.

I would have thought that some code, (or even sections of code), are
only parsed once.

That is why, (in the example given), I would have thought that EXAMPLE
A would be far more efficient than EXAMPLE B.
But essentially they are the same.

FFMG

And why are you surprised? Are you having a performance problem? Do
you see anyplace where this causes a problem? Parsing is quite fast. I
have never seen a performance problem cause by the parser.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Nov 25 '07 #7

P: n/a
FFMG wrote:
I believe that php caches included code whenever possible.
You believe incorrectly. However, there are add-ons available which do
this. eAccelerator is one -- it hooks itself straight into mod_php, so you
don't need to make any adjustments to your actual PHP scripts.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 19:43.]

It'll be in the Last Place You Look
http://tobyinkster.co.uk/blog/2007/11/21/no2id/
Nov 25 '07 #8

P: n/a
..oO(FFMG)
>Are you saying that the included scripts are revalidated every single
time?

That doesn't seem very efficient/logical to me.
Are you sure?

Surely some code is in memory.
Not by default. But there are code caches like APC available, which keep
the compiled bytecode in memory to speed up following requests.

Micha
Nov 25 '07 #9

P: n/a
include() doesn't cache files. Thats why there is include_once() to
skip inclusion of a file, if it has already been included (in the
script's runtime)
Nov 26 '07 #10

P: n/a
Kailash Nadh wrote:
include() doesn't cache files. Thats why there is include_once() to
skip inclusion of a file, if it has already been included (in the
script's runtime)
Which has absolutely nothing to do with caching...

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Nov 26 '07 #11

P: n/a

Kailash Nadh;105271 Wrote:
include() doesn't cache files. Thats why there is include_once() to
skip inclusion of a file, if it has already been included (in the
script's runtime)
Sorry this was not really what I was talking about.

FFMG
--

'webmaster forum' (http://www.httppoint.com) | 'Free Blogs'
(http://www.journalhome.com/) | 'webmaster Directory'
(http://www.webhostshunter.com/)
'Recreation Vehicle insurance'
(http://www.insurance-owl.com/other/car_rec.php) | 'Free URL
redirection service' (http://urlkick.com/)
------------------------------------------------------------------------
FFMG's Profile: http://www.httppoint.com/member.php?userid=580
View this thread: http://www.httppoint.com/showthread.php?t=22555

Message Posted via the webmaster forum http://www.httppoint.com, (Ad revenue sharing).

Nov 26 '07 #12

P: n/a

Jerry Stuckle;105188 Wrote:
>
And why are you surprised? Are you having a performance problem? Do
you see anyplace where this causes a problem? Parsing is quite fast.
I
have never seen a performance problem cause by the parser.
(Bearing in mind that I have only just started looking at the php
source code)
I am surprised because I thought that once the code was parsed it is
executed, and therefore that the code was only parsed once.

I am not having any issues with the parser or anything, I am just
trying to understand the way it works.

I simply thought that include(...) was faster than the second example I
gave.
But include(...) does pretty much the same work.

FFMG
--

'webmaster forum' (http://www.httppoint.com) | 'Free Blogs'
(http://www.journalhome.com/) | 'webmaster Directory'
(http://www.webhostshunter.com/)
'Recreation Vehicle insurance'
(http://www.insurance-owl.com/other/car_rec.php) | 'Free URL
redirection service' (http://urlkick.com/)
------------------------------------------------------------------------
FFMG's Profile: http://www.httppoint.com/member.php?userid=580
View this thread: http://www.httppoint.com/showthread.php?t=22555

Message Posted via the webmaster forum http://www.httppoint.com, (Ad revenue sharing).

Nov 26 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.