PHP: Difference between revisions

5,076 bytes added ,  22 April 2015
→‎Using PHP Hypertext Pre-processor: rewrite now on separate page
(New Page)
 
(→‎Using PHP Hypertext Pre-processor: rewrite now on separate page)
[[Category: WebTips]]
 
= Incorporating Script =
 
There are various script languages that can be used to enhance what can be done in pure HTML as described on the [[Customised_templates|Customisation]] page.
 
== Using PHP templatesJavaScript ==
 
JavaScript comes in different versions, depending on the browser, generally it obeys the "ECMA-262 standard". Internet Explorer, especially old versions, offers jScript instead, and so there may be problems with writing code that all browsers can understand. Normally, you should include <noscript> ... </noscript> around some alternative HTML to be used when the browser does not offer JavaScript, or it has been switched off (for security reasons) by the user. So many sites do use JavaScript now that modern browsers like Firefox do not offer an easy way to turn JavaScript off.
 
Incidentally, Java is yet another programming language, and it is very different to JavaScript.
 
A quick comparison between pure HTML and HTML with JavaScript can be seen from [[Editing_content_of_a_webpage_using_either_HTML_or_Script|this example]]. Essentially, the Browser produces a page structure by reading the HTML, and then the browser will run the JavaScript instructions as it meets them (or a call to the separate file containing that code) and that can be interpreted to modify what is seen on the web page. A user can normally ask their browser to see the original HTML (display source) and to display the script that is modifying that. The user may be able to stop the script from running or run a different script, so JavaScript can have security implications. It can however be an advantage that code can be run easily to update part of a page, leaving the rest. A disadvantage is that different browsers may offer different versions of JavaScript, so the page designer cannot make assumptions about what JavaScript code will be available to all potential users.
= Using PHP Hypertext Pre-processor =
 
Many web servers support the PHP scripting language, this means that the web server will parse the contents and obey all the script instructions to generate a pure HTML page that it forwards to a user agent (e.g. browser). The advantage of this type of script is that by default the script cannot be seen (unless the developer has added some code allowing a query-string to be added to the url asking for some or all of the PHP source to be shown to the browser). This makes for a more secure script, because it only runs on the web site server. For instance if information is read from a database or in another way involves a password of some kind, all that code can be hidden, the end-user then has no way to modify the script to get information that should not be seen by others. A disadvantage is that it can be more complicated to update information on a page, requiring the whole page to be regenerated. (Consequently, a mix of scripts may be used with updating by AJAX or JSON, added to the PHP that generates the basic page. That is beyond the scope of this article).
 
This Wiki article cannot teach you PHP, nor tell you if your web server supports it, but the examples below give a brief hint of how PHP can be used.
PHP regularly issues new versions, with major and minor upgrades. You need to ensure that your development server and your production server use the same version, or that you only use features that are common to both versions. If somebody else runs your web server, you need to track what PHP version they use. But since the PHP parser creates a HTML page to pass to each user, it does not matter what any user has on their device.
 
There are volumes of information available on writing PHP scripts, both in your local bookshop, or library, and on the web. Some of these books cover a database query language and maybe running a web server as well as PHP. However, below is a basic introduction to understanding and writing your own PHP to generate your web-page. This Wiki article cannot teach you PHP because it includes a very large number of functions, nor can I tell you if your web server supports PHP, but the examples below give a brief hint of how PHP could be used and should help you to get started.
 
== Using PHP in Cumulus templates ==
 
This approach incorporates some PHP script in a page that still contains Cumulus web tags so still requires processing by Cumulus. This is suitable when you want to try PHP, but do not want a big redesign of your set-up.
 
This is suitable when you have been using Cumulus and its standard web site. Sine then you have gained some grasp of [[Stylesheets]], some grasp of [[Customised_templates#HTML_5_-_a_very_quick_guide_to_structure|HTML]] and now want to try PHP, but do not want a big redesign of your set-up.
*TheAssume fileinitially stillyour have a customised template that contains thejust pure HTML shownand Cumulus web tags (like atin thea startstandard ofCumulus thistemplate page), indeed it might be a simple edit of a standard cumulus template.
*It still contains Cumulus web tags, but asAssume it is no longer a standard page, so it has to be listed on the ''Files'' tab of the '''Internet''' screen within the 'Configuration' menu; the remote file name mustcurrently havehas the extension ".phphtm" soor that the PHP processor will parse the file, before it is passed to the browser as a HTML page".html".
*Now change the file extension to ".php" so that the PHP pre-processor will parse the file, before it is passed to the browser as a HTML page.
*The php script might be used for a decision more complicated than the [[Webtags_as_boolean_operators_in_HTML|boolean options]] that can be achieved just with standard HTML.
 
 
=== An example of a Cumulus template using PHP ===
== Using PHP templates ==
 
*The php script might be used for a decision more complicated than the [[Webtags_as_boolean_operators_in_HTML|boolean options]] that can be achieved just with standard HTML. For example, inIn the UK vehicle headlights must be used between half-an hour after sunset to half-an-hour before sunrise, so maybe you want to modify a standard "xxxxT.htm" page that is currently processed into "xxxx.htm" and it has sunrise/sunset times on it. Change it to be processed into "xxxx.php" instead and incorporate 2 new chunks of code:
This approach incorporates some PHP script in a page that still contains Cumulus web tags so still requires processing by Cumulus. This is suitable when you want to try PHP, but do not want a big redesign of your set-up.
*The file still contains the HTML shown at the start of this page, indeed it might be a simple edit of a standard cumulus template.
*It still contains Cumulus web tags, but as it is no longer a standard page, it has to be listed on the ''Files'' tab of the '''Internet''' screen within the 'Configuration' menu; the remote file name must have the extension ".php" so that the PHP processor will parse the file, before it is passed to the browser as a HTML page.
*The php script might be used for a decision more complicated than the [[Webtags_as_boolean_operators_in_HTML|boolean options]] that can be achieved just with standard HTML. For example, in the UK vehicle headlights must be used between half-an hour after sunset to half-an-hour before sunrise, so maybe you want to modify a standard "xxxxT.htm" page that is currently processed into "xxxx.htm". Change it to be processed into "xxxx.php" instead and incorporate 2 new chunks of code:
<pre>
&lt;?php // start of PHP script
</pre>
 
Incidentally, do look at the Cumulus FAQ or [[Webtags#Day.2FNight.2FSun.2FMoon|web tags]] where what Cumulus reports as Sunrise time is explained as the last one before any midnight GMT, and what it reports as Sunset time refers to that occurring after the midnight GMT, so they are frequently on different days.
== Replacing HTML by PHP ==
 
If you want sunrise, sunset, darkness start, and darkness end to all refer to the same day, you will probably choose to use PHP functions for sunrise, sunset rather than Cumulus web tags. The example above was just to show what can be done, not what you should do!
#A more efficient approach, this is a bigger implementation change, is to set Cumulus to process just [[Php_webtags| something like this file]] to create PHP variables/arrays from the Cumulus web tags you want to use.
 
== Using PHP scripts not processed by Cumulus ==
 
#A more efficient approach, this is a bigger implementation change, is to set Cumulus to process just [[Php_webtags| something like this file]] to read the Cumulus web tags you want to use (some may have parameters to indicate what period to use or how to format the date they output) and then the processed file will create PHP variables/arrays fromfor theyour Cumulusother PHP web tags you wantpages to use.
#Then you create a new PHP script that does not get processed by Cumulus. For example "thismonth.php" would start with the same HTML5 as shown above, but the table is output by the PHP script:
<pre>
....
 
</codepre>
Note that only excerpts from the code are shown. Note the two ways of getting PHP to output HTML code, either by using separate echo commands for each line or using the "heredoc" approach so that one echo can be applied to multiple lines. In the latter way, the syntax allows a space after echo and after <<< but does not allow any other spaces (or tabs) in the two enclosing lines (that precede and follow all the listed output).
 
== Other PHP examples ==
 
The support forum includes many ideas for using PHP code, here are just a couple:
#[http://sandaysoft.com/forum/viewtopic.php?f=14&t=8631&p=71770 compare yesterday with one year ago page]
#[http://sandaysoft.com/forum/viewtopic.php?f=14&t=10755 Top Ten Records page]
5,838

edits