PHP: Difference between revisions

22 bytes added ,  13:24, 25 May 2020
m
Line 42: Line 42:
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.
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.  
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 before it declares it, or that script must be called with the "_once" variant.  
There is almost no restriction on what you bring in:
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'''.
*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'''.
5,838

edits