Tricky concepts in PHP


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.


9 thoughts on “Tricky concepts in PHP

  1. 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.

    Like

  2. 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.

    Like

  3. 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

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.