Some of you might have problems using the <?php include “filename.php” ?>. In this tutorial, I will try to explain about it, since it’s one of the most used syntax in PHP Programming.
But remember, this is my first programming tutorial, so be easy on me.
The correct use of the syntax :
<?php include "filename.php" ?>
The “include” syntax in PHP is used to include all of the codes and texts in the source file, into the current file. Well, I don’t see any explanation that’s better than an example, so… Here’s one.
For example we are going to include “hello.php” in “index.php”.
Contents of Hello.php :
<p> Hello World </p>
Contents of index.php :
<body><?php include "Hello.php" ?></body>
When you run the index.php on your favorite browser, this is what will happen :
And Voila! Now you know what’s the function of “include” in PHP is, and you also know how to use it. We usually use “include” when there’s some line on our website that we want to appear on more than one page. For example, the footer. Usually, the footer are always the same on every page of the website. But it will be painful if every time you want to edit the footer, you have to go through all the pages. So, PHP “include” is the answer!
Some common problems on PHP Include :
- No Web-server is running. Well, basically all PHP syntax needs web-server for them to be able to run. If you run your .php files on local drives, they will only read the HTML syntax. Without local webservers such as apache, wamp, etc, you will need to upload your .php files to a PHP-supported web-hosting.
- Incorrect filename / path. One of the most common mistake is putting in an incorrect path to the file. Make If the included .php is on the same folder, simply put the filename, and if it’s not, put in the path to the folder where the included .php is.