The Most Active and Friendliest
Affiliate Marketing Community Online!

“Adavice”/  “RollerAds”/

include vs. include_once vs. require vs. require_once - examples

ForumJoiner

New Member
affiliate
include ('sponsor_links.php');
If the file sponsor_links is present in the current folder, it will be included ( way may want it to appear both - at the top and at the bottom).


include_once ('statistics.php');
If the file statistics.php is present, it means we want statistics for the page. Otherwise, we don't want statistics. Deleting statistics.php will not affect the main program.


require ('navigation.php');
We may want to include the navigational links both both at the top and the bottom at the page. If navigation.php is not present, the main program will stop running and an error message will be displayed.


require_once ('google_adsense_top.php');
Because a page has only one top, only one instance of google_adsense_top.php will be included. If google_adsense_top.php is not present, the main program will stop running and an error message will be displayed.


If you want more details about how each of include/require works, please reply to this post.
 
ForumJoiner, I understand that "require" means the file navigation.php for example needs to be present for the program to run. With "include" it would not affect the main program if navigation.php was not present.

But I would like to clarify the following:
require ('navigation.php') or include('navigation.php'): : Can be used to include a navigation bar in several places of the site. But I suppose you would call it again in the desired location?
require_once ('navigation.php') or include_once('navigation.php'): Can be used to include a navigation bar in one place only?
Let me know if I got this right.

Then how would you apply any styles to a navigation bar called from a PHP file? Does PHP offer layout and style elements?

Cheers,
lala
 
I understand that "require" means the file navigation.php for example needs to be present for the program to run. With "include" it would not affect the main program if navigation.php were not present.
You got it right.




require ('navigation.php') or include('navigation.php'): : Can be used to include a navigation bar in several places of the site. But I suppose you would call it again in the desired location?
Yes, you have to call it several times, once for each location. If you designed a navigational system, you should use "require", because that navigational system is a required part of your page.

"My page needs:
- a logo (mandatory, only once)
- a top navigational system (mandatory, I may have it again in this page)
- the main content (mandatory, only once)
- the sponsor's ads (optional, but only once - only if there are ads, that is, a file called 'ads.php' is present)
- a bottom navigational system (mandatory, I may have it again in this page)"
translates into:
PHP:
- require_once ('logo.php');
- require ('navigation.php');
- require_once ('content.php');
- include_once ('ads.php');
- require ('navigation.php');

"Mandatory" means "require" and "optional" means "include".




require_once ('navigation.php') or include_once('navigation.php'): Can be used to include a navigation bar in one place only?
Yes. Because the navigation bar is mandatory, you should use "require". Because you need it only once, you should use "require_once".




Then how would you apply any styles to a navigation bar called from a PHP file? Does PHP offer layout and style elements?
No, PHP does not offer layout and style elements, because it doesn't need to.
Imagine the way your page it will look in the browser, after the PHP code was executed.

For instance, you have this navigational code, in the file 'navigation.php'.
PHP:
<div class = "navigation"> ... </div>
In the header section of your main page, you'll link to an external CSS file. When all pieces will be put together, the class "navigation" will apply its characteristics on your div. You can see this by looking to the source of the page you see on your browser, when you test your program.
 
That's great! Thank you so much :p

Just one more check: Would it be at all possible to link to stylesheets directly from navigation.php? I mean, it makes more sense to link to your stylesheet from one location, but I am curious...

Thanks again,
lala
 
Yes, you can put this into the navigation file:
PHP:
<!-- We link here to the external CSS file -->
<link 
      rel = "stylesheet" 
      type = "text/css" 
      href = "example.css" 
/>
 
<!-- We use the CSS file -->
This is <span class = "test">red bold</span> text.


If you link to the CSS in the <head> section, the CSS instructions are available right from the start. This is useful, for instance, when you set the <body> properties using the CSS.

I attached a HTML version. You know that you can include directly HTML code inside the PHP page, outside the <?php ... ?> tags.
 
I believe they tried with other functions what you suggested, but maybe the programmers wanted to have separate names for each case. For instance, even there is a function called mysql_fetch_array, which accept parameters, there is also a function mysql_fetch_assoc, just for the case you want to use an associative array.


Another reason might be performance. In the PHP manual, they say:
Performance: An important thing to note is that using mysql_fetch_array() is not significantly slower than using mysql_fetch_row(), while it provides a significant added value.

Maybe the same happened with include / require.


I also believe it's easier to remember the syntax for include_once, than to remember the order of parameters for include ("once", "$filename", "require")
 
I haven't really understand much of the difference between require and include not until now. Thanks for the info
 
MI
Back