Showing posts with label PHP include. Show all posts
Showing posts with label PHP include. Show all posts

Monday, 5 August 2019

include or require function in PHP with practical examples

Website design require header,footer,side bar,right bar, ad bar on multiple pages.It is not possible to change these on thousand of web pages whenever we want to change anything. Include function in php  solve this problem. We can  use include function in PHP and include the content of a PHP file into another PHP file before the server executes it.So If there is any change required then instead of changing thousand of files just change included file.

PHP has two functions which can be used to included one PHP file into another PHP file.
The include() Function
The require() Function
Both the function has different working .
The include (or require) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement
Include function in PhpRequire function in php
The include() function takes all the text in a specified file and copies it into the file that uses the include function.
Syntax
Include ‘<file_name>’
Example
<?php
include ‘/home/test/public_html/coded/fbscript.php’;
?>

The require() function takes all the text in a specified file and copies it into the file that uses the require function.
Syntax
require ‘<file_name>’
Example
<?php
Require ‘/home/test/public_html/coded/fbscript.php’;
?>

If there is any problem in loading a file then the include() function generates a warning but the script will continue execution.
If in above example if the fbscripts.php does not exists,then still code will run  and it will just generate warning
If there is any problem in loading a file then the require() function generates a fatal error and halt the execution of the script.
If in above example if the fbscripts.php does not exists,then still code will halt
Useful examples for Include function in Php
<!doctype html>
<html class=”no-js” lang=”en”>
<head>
<meta charset=”utf-8″ />
<title>Example Site</title>
<?php include ‘/home/app//public_html/comcode/stylesheets_depth.php’; ?>
<?php include ‘/home/app//public_html/comcode/javascript.php’; ?>
</head>
<body>
<?php include_once(“/home/app//public_html/comcode/analytics.php”) ?>
<?php include ‘/home/app//public_html/comcode/brow-update.php’; ?>
<?php include ‘/home/app//public_html/comcode/facebooscript.php’; ?>
<?php include ‘/home/app//public_html/comcode/header1.php’; ?>
< text here>
</body>
<?php include ‘/home/app//public_html/comcode/footer.php’; ?>
</html
The above page will still even if the php script are not present as we have used include
<?php
require_once “/home/app/public_html/header1.php”;
?>
<!doctype html>
<html class=”no-js” lang=”en”>
<head>

<TITLE> Example site </TITLE>
<META NAME=”revisit-after” CONTENT=”1″>
<?php include ‘/home/app/public_html/comcode/stylesheets.php’; ?>
<?php include ‘/home/app/public_html/comcode/javascript.php’; ?>



</head>
<body>
<?php include_once(“/home/app/public_html/comcode/analytics.php”) ?>
<?php include ‘/home/app/public_html/comcode/brow-update.php’; ?>
<?php include ‘/home/app/public_html/comcode/facebookscript.php’; ?>
<?php include ‘/home/app/public_html/comcode/header2.php’; ?>
<text here>
</body>
<?php include ‘/home/app/public_html/comcode/footer.php’; ?>
</html>
The above will fail if the first header file is not available as it uses the require function

Monday, 17 September 2018

Testing if a PHP include file was included

PHP's include() and include_once() functions can return values to the calling script which can be useful for testing to see if the file was actually included, for example if a configuration file whose filename is based on the website's domain name was included. If no return value is set then it will return integer 1 if included, or boolean false if not.

Code example

So you could do this, for example:
if(!@include_once('/path/to/some-script.php')) {
  // do something
}
The @ symbol suppresses warnings from being displayed if error reporting permits errors of E_WARNING to be reported and display_errors is on.

Using return

The included file can return a value to the calling script, for example like so:
return 1;
Just like in a function call, execution will return from the include file to the calling script at the point return is called, so no further code in the file will be executed.
If you returned 0 from the include file, then in the first example the code in the curly braces would be executed.
As noted in the opening paragraph, if no return value is set in an include file then it will default to integer 1 after the code executes or boolen false if the file could not be included.

require() and require_once()

Returning values works the same way for require() and require_once() as it does for include() and include_once(), also defaulting to integer 1 if not set.
However, a fatal error is issued if a file cannot be required so you can't test for it being required in your code.

Related posts:

Using an include file for footer links with PHP

I was asked an interesting question by email over the weekend and thought I'd share my response here. The question was "I wish to include in the footer links to my other developed websites. I don't wish to update the footer on each html page every time I create a new webpage. What would be your advice for implementing something like that using html, js and css".

Client-sided vs server-sided

The question asked about using Javascript as well as HTML and CSS and didn't mention a server-sided programming language at all. While it is possible to write out the footer links using Javascript from a common JS file, I wouldn't recommend it.
Apart from just feeling a little bit of a "hacky" way to do it, there are SEO advantages from having the links actually coded into the HTML of the page; if they're written out in Javascript search engines are unlikely to follow your links and assign weight to them etc.

Making static HTML files dynamic with PHP and Apache

Making the assumption that the files are a bunch of static HTML files, I suggested that if the site is hosted on a server that supports PHP then to make the regular HTML files be parsed as PHP, which I have shown how to do in my "get Apache to parse .html files as PHP" post.

Use include() to include the footer file

Now that the files are being parsed as PHP you can add blocks of PHP code to the HTML files. Nothing needs to be done to them to make this work, and even if there is no PHP in the files they will still be served as they are sending the existing HTML to the web browser.
At the appropriate place in the each HTML file, add the following tag, where 'footer-links.html' is the name of the file that contains the footer links:
<?php include('footer-links.html'); ?>
Then add links to the file, add stlying to it with CSS etc etc.

Conclusion

Ideally you really don't want your site to be a bunch of static HTML files because it makes updating the site structure etc in the future much more difficult, but if that's what you are stuck with then this is an easy enough to implement solution.
It's a bit quick and dirty but it means the links will be served in the HTML itself and maybe you can at the same time think about changing your site over to a more structured system with separation of content and template.

Related posts:

Wednesday, 12 September 2018

Get the included files with PHP

PHP's get_included_files() function returns an array containing a list of included files by your script, including the script itself. This post looks at the filenames returned in the array and corrects a couple of errors in the PHP documentation for this function.
Calling get_included_files() returns an array containing all the included files whether they were included with the include() include_once() require() or require_once() functions. It also includes the script itself, and any prepended scripts using the auto_prepend_file configuration directive (note that in the PHP documentation it says these are not included in the result). It does not include scripts included with the auto_append_file configuration directive.
For example, if you had the following script at /common/websites/test/example.php:
<?php
include('included.php');
include_once('included_once.php');
require('required.php');
require_once('required_once.php');
$included = get_included_files();
print_r($included);
The output of the above script would be:
Array
(
    [0] => /common/websites/test/example.php
    [1] => /common/websites/test/included.php
    [2] => /common/websites/test/included_once.php
    [3] => /common/websites/test/required.php
    [4] => /common/websites/test/required_once.php
)
If a file is included more than once it will be in the array just once.
If an included file did not exist and could not be included it will not be in the array.
The full path to the included files is included in the filenames. This is a second issue with the documentation on the PHP website: the example provided in the docs shows only the filename and not the full path as well. Perhaps this was the case in earlier versions of PHP, but at the very least in PHP 5.1.6 on CentOS and 5.2.6 on Debian it has the full path as well.

Related posts:

Thursday, 4 September 2014

PHP: Include & require functions In PHP

You can include the content of a PHP file into another PHP file before the server executes it. There are two PHP functions which can be used to included one PHP file into another PHP file.
  • The include() Function
  • The require() Function
This is a strong point of PHP which helps in creating functions, headers, footers, or elements that can be reused on multiple pages. This will help developers to make it easy to change the layout of complete website with minimal effort. If there is any change required then instead of changing thousand of files just change included file.

The include() Function

The include() function takes all the text in a specified file and copies it into the file that uses the include function. If there is any problem in loading a file then the include() function generates a warning but the script will continue execution.
Assume you want to create a common menu for your website. Then create a file menu.php with the following content.
<a href="https://thiscode4u.blogspot.com/">Home</a> - 
<a href="https://thiscode4u.blogspot.com/p/pig-setup-requirements-mandatory-unix.html">PIG</a> - 
<a href="https://thiscode4u.blogspot.com/p/sqoop-user-guide-v1.html">SQOOP</a> - 
<a href="https://thiscode4u.blogspot.com/p/test-page.html">HADOOP</a> <br />
Now create as many pages as you like and include this file to create header. For example now your test.php file can have following content.
<html>
<body>
<?php include("menu.php"); ?>
<p>This is an example to show how to include PHP file!</p>
</body>
</html>
This will produce following result
Home - PIG - SQOOP - HADOOP
This is an example to show how to include PHP file. You can include menu.php file in as many as files you like!

The require() Function

The require() function takes all the text in a specified file and copies it into the file that uses the include function. If there is any problem in loading a file then the require() function generates a fatal error and halt the execution of the script.
So there is no difference in require() and include() except they handle error conditions. It is recommended to use the require() function instead of include(), because scripts should not continue executing if files are missing or misnamed.
You can try using above example with require() function and it will generate same result. But if you will try following two examples where file does not exist then you will get different results.
<html>
<body>
<?php include("xxmenu.php"); ?>
<p>This is an example to show how to include wrong PHP file!</p>
</body>
</html>
This will produce following result
This is an example to show how to include wrong PHP file!
Now lets try same example with require() function.
<html>
<body>
<?php require("xxmenu.php"); ?>
<p>This is an example to show how to include wrong PHP file!</p>
</body>
</html>
This time file execution halts and nothing is displayed.
NOTE: You may get plain warning messages or fatal error messages or nothing at all. This depends on your PHP Server configuration.