28.4. The syntax of PHP code
PHP-Nuke is written in PHP and uses the MySQL database. In this section we will present some examples of PHP code. We don't try to teach you a new programming language, but only to illustrate how some things work.
28.4.1. Variables
Variables are those elements that are given variable values, depending on the case. Here is an example:
<? |

- As with every PHP script, we have to designate its start and end. Here is the start...

- We check if the foobar variable has the value 1.

- If it does, we output a message. We do this with the echo function. This functions will print whatever is included in the double quotes. The double quotes may contain other variables (in which case the echo command will print their values), HTML code etc. If you want to print double quotes, you have to escape them with a backslash.

- If it doesn't, we also output an aproppriate message.

- ...and here is the end of the script.
See also PHP variables.
28.4.2. Functions
Here is an example of a simple PHP function:
function get_lang($module) {
global $currentlang, $language;
HERE GOES THE FUNCTION CODE
} |
It has the following structure, common to every PHP function:
the function name, preceded by the keyword "function",
the function parameters in the parenthesis,
the global variables that we intend to use in this function, registered through the "global" keyword (we cannot use any global variables, if we do not register them as such),
the curly brackets that include the function's content
Functions may be called from anywhere in the PHP code. See also PHP functions.
28.4.3. Switches
A switch in PHP allows us to take certain actions depending on the value of a certain variable, as in the following example, where we check the value of the $pa variable:
switch($pa) {
case "showpage":
showpage($pid, $page);
break;
case "list_pages_categories":
list_pages_categories($cid);
break;
default:
list_pages();
break;
} |
In case $pa has the value “showpage”, we call the showpage() function, in case it has the value "list_pages_categories", we call the list_pages_categories() function. If $pa does not have any of these two values, or any value at all, we enter the default case and call list_pages(). See also PHP control structures.
Switch statements are often used in module development (especially in the module administration functions, see Section 21.4). So, if you declared in a module admin case file a switch like:
switch($func) {
case "func-one":
funct-one();
break;
} |
you must call it through a link like:
http://www.yoursite.com/modules.php?mop=modload &name=The_Web_Ring&file=index&func=func-one |
If your function needs to have parameters (see Section 28.4.2), your switch will look like:
switch($func) {
case "func-one":
funct-one($xid, $xname);
break;
} |
and your link will have to be:
http://www.yoursite.com/modules.php?mop=modload &name=The_Web_Ring&file=index&func=func-one&xid=$xid&xname=$xname |
See also the ADDONS-MODULES file that came with your PHP-Nuke package.