PHP Basics for Beginners

PHP Basics for Beginners

22 208

This article aims to serve for beginner PHP developers but those who are comfortable with PHP will also find this article useful. Basically, I’ll tell you the difference between some of PHP commonly used functions/features. By the end of this article you will have a clear difference among:

  • include VS require
  • include_once VS include
  • require_once VS require
  • echo VS print
  • Single quote VS Double quote

Apparently all functions in each above point work in similar fashion. However, they are slightly different from other. Continue reading to explore the differences.

[ad#chitika-460×120]

include VS require

Both are frequently used to include other files into our PHP code. Following are the differences between ‘include’ and ‘require':

  • ‘include’ throws a warning, if your specified file is not found. Rest of the code will be rendered.
  • ‘require’ throws a fatal error, if your specified file is not found. Rest of the code will not be rendered.

Testing with Code Example

I’m using some code to make it more clear. To check it yourself you may follow the steps with me. Create a new file  ‘inc.file.php’, put below code in it:

<?php
function printMe()
{
 return "I have some code <br>";
}

echo printMe();
?>

Save the file in any directory of server. Now create a new file ‘index.php’ and put the below code in it:

<?php
include 'inc.nofile.php'; //File does not exist
include 'inc.file.php'; //This line will be rendered
?>

Save the file in same directory. To see how ‘include’ works, run the ‘index.php’ file from your browser. You will see an error something similar to:

Warning: include(inc.nofile.php) [function.include]: failed to open stream: No such file or directory in…

But still you can see that the text – “I have some code” is printed to the screen.

However, same test for ‘require’, shows that second line will not be rendered. Here is the code for testing ‘require’. Just copy and paste below code to your ‘index.php’ file (Remove any existing code)

<?php
require 'inc.nofile.php'; //File does not exist
include 'inc.file.php'; //This line will not be rendered
?>

This time we again get an error, but note that nothing is rendered after an error occurred.

Conclusion

Use ‘include’ when you want to render the code even if an error occurs. And use ‘require’ when you do not want to render the remaining code after an error occurs.

include_once VS include AND require_once VS require

‘include_once’ and ‘require_once’ are same in nature. Here we will see the difference of ‘include_once’ against ‘include’. Following are the differences between ‘include’ and ‘include_once':

  • All includes for same file will be rendered, if you are using ‘include’.
  • While all includes for same file will be rendered only once, if you are using ‘include_once’. This helps you to prevent an error, if same file included again.

Testing with Code Example

To start off with experimenting, replace below code in ‘index.php’

<?php
include 'inc.file.php';
include 'inc.file.php'; //It will generate a fatal error
?>

Above code will be ended with following error,

Fatal error: Cannot redeclare printMe() (previously declared

Because, we are using same file in our includes. And our file – inc.file.php – has a function of printMe(). Since, we cannot re-declare a function in PHP, it throws an error.

But if we test below code by replacing it in our ‘index.php’ file, it will not throw any error.

<?php
include_once 'inc.file.php';
include_once 'inc.file.php'; //This will be ignored
?>

Since ‘inc.file.php’ is included using ‘include_once’, it will ignore the second line because it includes same file only once.

Conclusion

Use ‘include_once’, where you are in doubt of including the same file again somewhere in your code.

print VS echo

‘print’ and ‘echo’ are almost similar. Both are used to write text to screen. But there is a single difference between them:

  • ‘print’ does not accept multiple parameters.
  • While ‘echo’ accepts multiple parameters.

Above difference may be confusing to many people. I try to clear the confusion by explaining the difference by code.

Testing with Code Example

Let’s begin with ‘echo’ example. We pass two parameters to ‘echo':

<?php
echo ("Tech "), ("Mug");
?>

Above code will be rendered without any error. But if we try same thing with ‘print’, it will be ended with a parse error. Let’s try it:

<?php
print ("Tech "), ("Mug"); //This will generate an error
?>

Above code will give you an error something similar to,

Parse error: parse error in…

Conclusion

We may pass multiple parameters to ‘echo’ but not to ‘print’.

Single quote VS Double quote

This might be very confusing for many developers to choose between single and double quote for writing string. There is a couple of difference between using single and double quote:

  • Any variable or special character like \n \r etc under the single quote will not be rendered. It will be displayed as an string.
  • Any variable or special character like \n \r etc under the double quote will be rendered properly.

Test with Code Example

Test the below code in ‘index.php’ file and run the file.


<?php
$var = 'I am a string!';

echo 'I write - $var'; //It will not render $var variable
echo "I write - $var"; //It will render $var successfully

// Special formatting characters (\n \r etc) also do not
// work with single quote
echo 'My first line \n my new line'; // \n sequence will not be rendered and displayed as a string
echo "My first line \n my new line"; // \n sequence will be rendered
?>

Explanation of above code should be understandable by reading the comments, the code has.

Conclusion

It does not make any difference to use single or double quote until you want to render a variable and/or any special character in it.

[ad#chitika-468×250]

Hopefully, now you have a better understanding of above discussion and it helps for php beginner developers . Please feel free to share your thoughts or ask any question. Your comments are always welcome. If you like this article, please share it on your favorite website. It may help someone somewhere!

SIMILAR ARTICLES

22 COMMENTS

  1. Good share. Although I must say it’s very brief, there’s so much you can say on each topic.

    For example: although both echo and print are language constructs (not actually function), print behaves more like function and echo behaves more like a construct.

    That’s why, you can use print in a single line expression like this:
    (true) ? print ‘true’ : print ‘false';

    But, echo cannot be used like this:
    (true) ? echo ‘true’ : echo ‘false'; // this will give parse error

    None the less, good for beginners :)

  2. Thanks for knowledge sharing Fayaz!

    Oh Yeah! There is more to say! But actually I don’t want to confuse newbies more. My target is to clear the confusion of basic usage.

  3. Remove the parens in your include and require statements, nothing like starting people off with bad habits.

  4. Hello! Just want you to know that I really found this website really helpful to me. I will come back to this blog more often to read more. Thanks.

  5. I do think this is certainly among the most significant information in my situation. And im glad reading your article. But would like to remark on some general things, It style is wonderful, the articles is very excellent : D. Good job, cheers

Leave a Reply