PHP: Difference between revisions

2,271 bytes added ,  25 May 2020
m
=== Advantages of PHP scripts ===
 
PHP is relatively easy to learn, because it can be used either in a very simple way, or when you learn more in a very sophisticated way.
PHP is relatively easy to learn, because it can be used either in a very simple way, or when you learn more in a very sophisticated way. One advantage of PHP scripts is because they are processed by the web server nobody can steal your script unless you use some functionality like that [https://cumulus.hosiene.co.uk/viewtopic.php?f=14&t=16425&p=126065 described here]. If you are writing a set of web pages, you can make your web suite very much easier to maintain by not putting every instruction into one script, you can use syntax like 'require' or 'include' to effectively bring in common code from other scripts. These other scripts might contain standard functions (like connecting to a database, reading from a database and calculating highest or lowest) or they might contain a standard navigation header that you want for all your web pages (with ability to identify which script is the current one, which CSS files to attach, and so on). So a standard header (and footer) can make all your web pages look similar and there is only one place where you need to edit to change that look. But equally using a script a web page can look different depending on any data you choose (time of day, current season, whether it is raining, how hot it is) or it can display different content depending on particular values.
 
One advantage of PHP scripts is because they are processed by the web server nobody can steal your script unless you use some functionality like that [https://cumulus.hosiene.co.uk/viewtopic.php?f=14&t=16425&p=126065 described here].
 
If you use PHP for a set of web pages, you can make your web suite very much easier to maintain by not putting every instruction into one script.
 
PHP allows you to use syntax like 'require' or 'include' to effectively bring in common code from other files. Put simply, when the PHP parser finds "require_once 'file_name';", "require 'file_name';", or the alternative include syntax, the parser reads the file referenced and treats it as if it is part of the original file from then onwards. "include" does not cause an error if it cannot find the file requested. "require" creates an error and aborts the main script if it cannot find the named file. The "_once" is used if the file you want to bring in includes setting variables that you later want to give a new value to, because your new value would be lost if the include happened again. Also a function can only be declared once so if script you select to bring in contains one or more function definitions, either that script must check if function has already been declared, or that script must be called with the "_once" variant.
There is almost no restriction on what you bring in:
*If you always include some HTML that is same on every web page, bring that in by specifying a HTML file, the normal file extension is '''.html'''.
**Remember, a standard header (and footer) can make all your web pages look similar and more professional, doing it this way means there is only one place where you need to edit to change that look. But remember it must be exactly same for every web page as any file with a HTML file extension is normally ignored by the PHP parser, so only client scripts like JavaScript can change the content of a HTML file.
*If you have a standard header (or footer) to repeat on every web page, but want to be able to send it some PHP variables to modify some of the content (what CSS to use, you have a common navigation menu but need to identify which page is the current one, you want to declare when the current web page had its structure or content last updated in a standard way on every web page), all of these and more can be achieved by using a php script. Remember to use the extension '''.php''' in the require/include and also to use a '''<?php''' to identify the start of the PHP in the file you are including. Don't use a '''?>''' in the require/include file, unless there is HTML directly after that in that file, because you have pHP next back in calling script.
**Remember, a standard header (and footer) can make all your web pages look similar and there is only one place where you need to edit to change that look. But equally using a script a web page can look different depending on any data you choose (time of day, current season, whether it is raining, how hot it is) or it can display different content depending on particular values.
*Some other scripts might contain standard functions (like connecting to a database, reading from a database, calculating highest or lowest, or they might contain some of your intellectual property in terms of a script that you want to place in a secure location only known when parsing within the web server, so cannot be accessed from the browser and so cannot be hacked.
 
=== Learning More about PHP ===
5,838

edits