Whats the difference between this syntax & that syntax

Hello,
I was wondering what is the difference between include " “; and include(” "); ? Also why are there 2 versions?
Lastly, I know The double quotes can be interchanged with single quotes— does doing so change what the code does or can do?

http://www.nusphere.com/kb/phpmanual/function.include.htm

http://php.net/manual/en/function.include.php

They are just two ways of writing the same thing. One important point to consider is that include is not a function, it is a language construct. Unlike functions, which take a list of arguments within a pair of parentheses (e.g. foo($bar, $baz)), include takes the value of the expression following it (hopefully a file path) and includes that.

It might be useful to hark back to the very basic elements of PHP at this point. Expressions are everywhere in PHP and, put simply, they are anything that has a value. Breaking down your two examples, the two underlined parts are the expressions being passed to include:

  • include [U]" "[/U];
  • include[U](" ")[/U];

Both of these expressions have the same value, a string containing a single space character.

To help demonstrate, lets find an example that hopefully clarifies this a little bit.


if (include('example.php') == 'OK') {
    echo 'OK';
}

Now, you may think that the condition within the if statement behaves in the following manner. Execute what is inside the if block when the return value of include('example.php') equals OK. That’s right, right?

Well actually (brownie points if you spotted this), the above description is incorrect. include rather takes the entire expression following it, which in this case is ('example.php') == 'OK'. This expression is comparing the two strings, example.php and OK. Since they’re not equal, the value of the expression is FALSE. It is this value that is passed to include and as you might think, include FALSE isn’t really what the author wanted to do.

Off Topic:

In the specific case above, the correct condition would have been if ((include 'example.php') == 'OK') {.

Normally, we don’t fall into the type of trap seen above because most includes are simply written like include("example.php"); and the semicolon tells include where to stop.

Does that make things clearer? The following are a few different ways of performing the exact same include.


include 'example.php';
include ('example.php');
include('example.php');
include('ex') . 'ample' . ('.php');
include 'exam' . substr('simple', 2) . '.php';

Differences between single- and double-quoted strings are explained thoroughly on the Strings manual page. The main differences, to put it briefly, are:

  • double-quoted strings can interpolate variables within them.
    E.g. if the variable $fruit contained banana, then "I ate a $fruit" would have the value I ate a banana but 'I ate a $fruit' would not substitute the variable’s value.
  • double-quoted strings have more escape sequences available
    E.g. "\ " is a string containing a horizontal tab character; '\ ' is a string containing two characters, a backslash and the letter t.

If your string does not contain any variable name, or any of the extra escape sequences, then the single and double quotes are interchangeable.

Hope that helps a little.

Reading:

wow thank you. that is a lot. i really appreciate it!

@Salathe; Awesome post! Learned something new there about includes, and that after almost 9 years in the business :wink:

@Salathe,

I like your simple explanation, nice and easy to understand.


  
  if (include('example.php') == 'OK') { 
      echo 'OK'; 
  }


I would have lost money on the above result - I didn’t realise include rather takes the entire expression… :frowning: