Crying Glowing Sun

Tricky concepts in PHP

Posted by: M A Hossain Tonu on: November 18, 2008


Include Vs Require

include() and require() are slightly different. Basically, include is conditional and require is not.

This would include ’somefile’ if $something is true:

if($something){

include(“somefile”);

}

This would include ’somefile’ unconditionally

if($something){

require(“somefile”);

}

This would have VERY strange effects if somefile looked like:

} echo “Ha! I’m here regardless of something: $something<br>n”;

if (false) {

Another interesting example is to consider what will happen if you use include() or require() inside a loop.

$i = 1;

while ($i < 3) {

require(somefile.$i);

$i ;

}

Using require() as above will cause the same file to be used every single iteration. Clearly this is not the intention since the file name should be changing in each iteration of the loop. We need to use include() as below. Include() will be evaluated at each iteration of the loop including somefile.0, somefile.1, etc as expected.

$i = 1;

while ($i < 3) {

include(somefile.$i);

$i ;

}

The only interesting question that remains is what file will be required above. It turns out that PHP uses the value of $i when it reads the require() statement for the first time. So, the require() loop above will include something.1 two times. The include() loop includes something.1 and something.2.


Echo Vs Print

There is a difference between the two, but speed-wise it should be irrelevant which one you use. print() behaves like a function in that you can do:

$ret = print “Hello World”;

and $ret will be 1.

That means that print can be used as part of a more complex expression where echo cannot. print is also part of the precedence table which it needs to be if it is to be used within a complex expression. It is just about at the bottom of the precedence list though. Only “,” AND, OR and XOR are lower.

echo is marginally faster since it doesn’t set a return value if you really want to get down to the nitty gritty.

If the grammar is:

echo expression [, expression[, expression] … ]

Then

echo ( expression, expression )

is not valid. ( expression ) reduces to just an expression so this would be valid:

echo (“howdy”),(“partner”);

but you would simply write this as:

echo “howdy”,”partner”;

if you wanted to use two expressions. Putting the brackets in there serves no purpose since there is no operator precedence issue with a single expression like that.


Tags: , ,

8 Responses to "Tricky concepts in PHP"

nice tips and triks!!!keep it up dr tonu!!!

Nice article.

Keep it up

its usefull to clear concept.

Just have a quick look and seems interesting! :)

Please note this points:
* No $ sign used before “somefile” in loop.
* $i is not being increased in loop anyway. You didn’t use increment operator.
* echo() is not a function. It’s a language construct of PHP.
* Prior to PHP 4.0.2, The conditional statement won’t affect require().
* The main difference between include() and requir() is that, include() produces a Warning on failure while require() results in a Fatal Error.

Thanks for this nice post.

@Anis: Thank you for review.
All i tried to give you some technical concepts not sprucing up the convention, :)

The main difference between require() and include() is require($somefile) always reads the $somefile, even if it is put within a condition which will never execute. But include($somefile) will read the $somefile only if the line [where the include() statement is] is executed.

For example:

if (1=2){
require($somefile);
}

will include the $somefile, but

if (1=2){
include($somefile);
}

will not include that.

Another important difference between them is, require() causes a fatal error if the file can’t be read for whatsoever reason, but include() just throws a warning.

Nice attempt though, (Y) Keep up the good work.

@Arif Bhai: thnx for ur explanation…

actually require() describe with its name. when you require a file to use. so it didn’t wait for any condition. and due the file requirement its arise a fatal error…

anyway nice tips and tricks

Leave a Reply

Intro

Hi! This is Tonu, I am an Engineer and an open source thinker, graduated from Dhaka University of Engineering and Technology,Bangladesh. with CSE background. Presently employed in an outsourcing software company in Bangladesh as Software Engineer. I love to work with PHP & AJAX. My fav php framework is CodeIgniter. Also my growing interests are on social networking technology and web security.

My Facebook Profile

M A Hossain Tonu's Facebook Profile

Favorite Quota

Good advice is often confusing, but example is always clear.

Follow Me in Twitter :)

My Linkedbd Profile

My Flickr Album

flame2

flame1

flame

S5001990

More Photos

Blog Stats

  • 1,336 hits

RSS DUET Alumni at alumnibd.org

  • An error has occurred; the feed is probably down. Try again later.

Live Traffic Feed