Coding Rules

From TNG_Wiki
Jump to: navigation, search
Ambox notice.png The Coding Rules provides some syntax examples of coding comments in HTML, PHP, or CSS. Other coding syntax examples should be added to this page.
TNG All


Coding Rules describes the different syntax used to code comments in html, php, and css files.

HTML Comments

An HTML comment begins with <!--, ends with --> and does not contain -- or > anywhere in the comment.

HTML Reference

Basic PHP Syntax

A PHP scripting block always starts with <?php and ends with ?>.

Each code line in PHP must end with a semicolon.

There are two basic statements to output text with PHP: echo and print.

PHP Basic Syntax Reference

See Steve Voght's post on TNG Community Forum for some simple rules.

Comments in PHP

In PHP, either the # or the // are used to make a single-line comment or /* and */ to make a large comment block. For example,

Single line comment

# indicates this is a single line comment or that whatever follows is a comment


// also indicates this is a single line comment or that whatever follows is a comment

Multi-line comment

/*
indicates this is a
multi-line comment
*/

Using echo

If you are in PHP, you must use an echo statement to output html, such as

<?php
...
echo "<li><a href=\"public_html/xtra_pages/xtra_privacypolicy.php\" class=\"lightlink\">$text['mnuprivacy']</a></li>\n";
...
?>

Note that quotes must be escaped, that is preceded by a backslash within the echo statement otherwise the first quote encountered terminates the echo statement and since it is not followed by the semi-colon, it results in a syntax error.

However if you not within a <?php ... ?> wrapper or scripting block, you should code it as normal html, but will need to use a <?php ... ?> wrapper and an echo statement to output the php variable:

<li><a href=\"public_html/xtra_pages/xtra_privacypolicy.php\" class=\"lightlink\"><?php echo $text['mnuprivacy'] ; ?></a></li>;


Note that you could terminate the <?php block in the first example, to eliminate having to code an echo statement to output the html line, but will need to use a <?php ... ?> wrapper and an echo statement to output the php variable, as follows:

<?php
...
?>
<li><a href="public_html/xtra_pages/xtra_privacypolicy.php" class="lightlink"><?php echo $text['mnuprivacy'] ; ?></a></li>\n";
<?php 
...
?>

Note that it is not necessary to escape the quotes.

Quoted Variables

In strict PHP, variables must use single-quoted names to eliminate errors or warnings. TNG V8 is now using single-quoted variable names. The following are some examples of how this impacts the syntax when coding the variable name.

Within html statement

When used within an html statement, the single-quoted variable is simply coded as such. For example, as used within the topmenu.php file

<li><a href="<?php echo $cms['tngpath'] ?>searchform.php" class="lightlink"><?php echo $text['mnuadvancedsearch']; ?></a></li>

Within php statement

When used within a php statement, the single-quoted variable is simply coded as such. For example, as used within the topmenu.php file

<tr><td><input type="hidden" name="mybool" value="AND" /><input type="submit" name="search" value="<?php echo $text['mnusearchfornames']; ?>" class="small" /></td></tr>

Within an echo statement

When used within an php echo statement, the single-quoted variable must be enclosed within braces which tells PHP to evaluate the content of the variable. For example, as used within the topmenu.php file

echo "<li><a href=\"{$cms['tngpath']}logout.php\" class=\"lightlink\">{$text['mnulogout']}</a></li>\n";

Within another variable

When used to generate a text string within another php variable, the single-quoted variable must be enclosed within braces which tells PHP to evaluate the content of the variable. For example,

$photoByInfo = " {$text['providedby']} <b>$photoBy</b>";

Using Concatenation Option

Note that you can also use the period to concatenate string variables so that you don't necessarily need to enclose them within braces which would tell PHP to evaluate the content of the variable.

echo "<h3>" . $text['simplesearch'] . $text['ofcem'] . $text['stdavidcemetery'] . $text['intown'] . $text['madawaska'] ."</h3>\n";

Using Evaluation Option

The following statement produces the same result as that in the statement above where concatenation is used.

echo "<h3>{$text['simplesearch']}{$text['ofcem']}{$text['stdavidcemetery']}{$text['intown']}{$text['madawaska']}</h3>\n";

CSS Syntax

CSS Syntax Reference