Double vs. single quote and require vs. include

php is one of the most popular programming languages for web development. Many people might have different opinion but to me it is also one of the easiest programming languages to learn. Some features like declaring arrays, blending variables with HTML, segregating files to achieve MVC (model-view-controller) feeling made php really handy. Object orientation to the language brought another whole dimension to deal with more complex problems. I am a huge fan of object oriented programming and thought I should give a try to learn php. So, I came up with the idea of building my blog site while I am learning php and share (document more likely) my knowledge. As I am just learning, I build this site totally from scratch with completely basic php. 

Single vs Double quotes

Dealing with string type is quite interesting in php. In php string can be in both double and single quotes.

$name1 = "Jahan";
$name2 = 'Jahan';

Here, we have declared 2 variables name1 and name2 with a "$" (dollar) sign preceding them. "$" sign denotes that it is a variable. Then we have assigned same value once with double quote and then with single quote. We assign value to a variable using "=" (equal) sign in php.

Benefit of double quotes

If we use double quote in a string value, it is possible to use any variable name directly inside that string.

$name = "Jahan";
echo "Hello $name"; // variable inside double quote
echo “Hello {$name}”; // using {} makes obvious that its a variable

In php "echo" is a reserved function that prints the value of any variable. In this case, we are trying to print a string whose value should be Hello Jahan.

But what if we need a single quote?

In case, where we need to have single quote inside our string, we can append single quote inside the string by using black slash '\'.

echo "Hello \'{$name}\'";

Caveat of sigle quotes

We cannot use variable name directly inside a single quoted string. In this case we have to concatenate the variable.

echo 'Hello again ' . $name;

Here, we are concatenating two string using "." (period) sign.

Include vs Require

File segregation is very important in php to make your code maintainable and readable. Otherwise, it might (actually it does) get quite dirty and cumbersome. What either of these two do is make a reference to the mentioned file, so that any functions or variables from those mentioned file can be used. One of the classic example is having a file in your directory called "functions.php" where you might have all the necessary functions for your application. Now, if you want to call a function for example "GetTheFirstBlog()" in your "index.php" file, all you need to do is to refer to the "functions.php" file at the very top of your "index.php" file before you start writing any HTML or any other bit of php code. That's it, now you can call the "GetTheFirstBlog()" function and do whatever you want to do with the return value if there is any.


Main difference between "include" and "require" is in error handling. If we use "include" then php will produce an error and continue with the script if the included file is missing. Meaning, the application is not completely dependent on the file you are trying to use. On the other hand if we use "require" php will produce a fatal error and stop processing the script if the file is missing. Because, in this case the file you are trying to use is mandatory and your application can not live without it.

There are also "include_once" and "require_once", typically do the same thing as "include" and "require", meaning the file to be added only once in the entire application. My personal preference is not to use these versions of "include" or "require" unless you are completely confirm about what you are doing.

Thanks. Happy coding!

August 17, 2016
Jahan Sarwar