If you have lots of functions or one very long one it can make the page cluttered. You may also want to use the function on many pages. To do that put the function (or functions) in an external file.
Create a completely empty page called phpmyfunctions.php. Add <?php and ?> as two lines with an empty line between them. In phpfunctions.php you currently have a function. Take the whole function out of that page and put it into the new one inside the empty PHP block.
Save both pages and try the first one again (phpfunctions.php). You should get an error as the function is missing. This time the error is final (fatal) as the whole function is missing so the page stops there.
To fix that place this line of code where the function was in phpfunctions.php:
include("phpmyfunctions.php");
Although the function is now in a separate file the include() function effectively copies it and pastes it in the original page where it works as before.
Including once and your PHP template
You might want to call that saySomething() function more than once on a page to say things more than once. However, sometimes you need something included just once and if it is run again there might be problems. The best example is connecting to an SQL database.
In _template.php you should have something very similar to these two lines if you did the SQL tutorial:
$dbconnection=mysqli_connect("localhost", "root", "") or die("Error connecting to the database:".mysqli_error($dbconnection));
mysqli_select_db($dbconnection, "databasename") or die("Error selecting the database to use:".mysqli_error($dbconnection));
Those form a connection to the database so you can send data to and from it in your PHP. You need the two lines on any page which needs access to the database. That means it makes sense to put them in a separate file and include them in any page when needed. Then if the database server password or other details change you can make one change and all pages still connect. To do this:
- create a new empty file with just <?php and ?> in it and save it as _databaseinclude.php (the _ sorts it to the top)
- inside the PHP tag cut the two lines (as above) from your template
- if they are still there delete the two database lines from your template and replace them with:
include_once("_databaseinclude.php");
As the template doesn't do anything you cannot test it properly but the two lines should work exactly as before when you get to SQL again.
Now you can use the include_once() line on any page where you need those two lines of code to run. It works exactly the same as include but will not work twice on a page. You would never want to connect to the same database twice. This method of including prevents that. It will run the lines once per page but if you accidentally try to do it twice it will ignore you.
An added benefit is that your password is not shown on the screen as you edit pages as it is in the included file (which you will rarely edit).
Save the template if you have not already. When you do SQL again you will see the include working.
Including your error settings
You can include any code which you might use on every page. One other example is the three lines which set error reporting levels:
error_reporting(E_ALL);
ini_set("display_errors", TRUE);
/* error_reporting(0); */
You should put them in an external file and include them in in your template. Then if you make a site public you can change the error reporting levels by editing one file rather than every page. Do that now and call the new file _errorsettings.php.
Bear in mind that you now need those two files in any folder where you create PHP pages and use them in an include.