Jump to content
xisto Community
jlhaslip

[php] Clean Code Functions Clean up your html output from php scripts

Recommended Posts

There is another Topic about writing 'clean' HTML code posted elsewhere on the Forum. I'll edit this Topic and add the link so you can review it on your own, and there is no need for me to comment on it in this thread, but the purpose of this Topic is to introduce a pair of functions which can be used for making sure that the HTML output from my scripts is readable when a view-source is reviewed.

 

Two handy functions are included here. They work together quite nicely, and I will start this Tutorial with a short summary of the reasons for their 'being'.

 

Purpose Explained:

 

For the most part, Web Designers use php as a tool for creating 'dynamic content' on their Web sites. PHP is used to generate the code for a Web Site in HTML (or xml, javascript, etc.). Essentially, the script send off pieces of 'text' which a Browser 'interprets' onto the screen. te text which is sent is (x)HTML, for all intents and purposes. Quite often, the HTML code will send out "<br />" tags for adding a new line of text on the Browser BUT the new line is displayed on the screen only, not in the html code itself. In other words, the source code lists the <br /> tags but the source code is not 'formatted' when you do a view source of the page. So... as a tool for cleaning up the display of the HTML code on a view-source, I often include the following two functions into my scripts to do exactly that.

 

The first one is for issuing Tabs for indenting blocks of code. The second one injects a new-line control character for a new line.

function tabs($t=1){ // to print $t number of tab characters

for($x = 1; $x <= $t; $x++){

$output .= "\t"; // back-slash t is a tab control character

}

return $output;

}

function nls($n=1){ // to print $n number of new-lines

for($x = 1; $x <= $n; $x++){

$output .= "\n"; // back-slash n is a new-line control character ( \n\r on some systems... Linux or Macs?)

}

return $output;

}

Explanation of the Functions:

 

The function " tabs" is defined with a default parameter value of "1" which is the default number of tab characters you will include as you use the function. To produce a tabs character in your HTML output, simply call this function in your script as "echo'd" or printed output. the default value is one, but including an integer value as a passed parameter for the function will increase the number of tabs or new-lines in your HTML output.

 

Examples:

echo tabs(); // will provide a single tab characterecho tabs(4); // will provide 4 tab characters

Similarly, you can have your HTML output issue a new line by using the

echo tabs(); // will provide a single new-lineecho tabs(4); // will provide 4 new-lines

 

Usage:

 

Here is a sample of a using these functions in a php script to write a readable table :

<?phpecho nls()'<table>'; // start the table tag on a new lineecho nls(),tabs(1) . '<tr>'; // start the table row tag on a new line with a single tabecho nls(),tabs(2) . '<td>iterations</td>'; //echo nls(),tabs(2) . '<td>Interval</td>';   // start the table data tags on a new line with two tabsecho nls(),tabs(2) . '<td>Average</td>';  // echo nls(),tabs(2) . '<td>Wait</td>';		//echo nls(), tabs(1) . '</tr>'; // start the table row end tag on a new line with a single tabecho '</table>'; // start the table end tag on a new lineecho nls();?>

I can not emphasis enough that the tabs characters and new lines will affect the view-source output only, not the actual output in your page.

 

Play with it a little.

 

*** Note: the tabs characters should be issued AFTER any newlines, or the tabs are effectively added to the previous lines and to not Indent the code in the view-source output properly.

*** Note: HtmlTidy is a script available as an add-on that does an very nice job of formatting the view-source, also. check it out at the Mozilla Extensions Site. It is also a part of the Firebug extension. (I think)

 

Hope this helps someone, and post any comments you might have about this Tutorial.

Share this post


Link to post
Share on other sites

Ahh. What a well written tutorial. You've even got the default values for the parameters in the functions. A nice extra touch. I am not a very neat coder, but I may use this script somewhere on the net. But as some of you know I love short code, the shorter the better and if you don't mind I would like to provide a recursion example:

Tabs:

function tabs($t=1){ // to print $t number of tab charactersreturn ($t==1)?"\t":"\t".tabs($t-1);}

Nls:
function nls($n=1){ // to print $n number of new-linesreturn ($n==1)?"\n":"\n".nls($n-1);}

Edited by alex7h3pr0gr4m3r (see edit history)

Share this post


Link to post
Share on other sites

$output .= "\n"; // back-slash n is a new-line control character ( \n\r on some systems... Linux or Macs?)

\n is the control character that represents a linefeed. On Linux and UNIX systems (and a wide array of others), the linefeed character is used to represent a new line. \r is the carriage return character, and is used by Windows in conjunction with the linefeed character to create a new line. The sequence to cause a new line on Windows is in fact \r\n in that order!

 

The reason \n works on Windows is because it gets translated 'behind the scenes' into the \r\n sequence.

Share this post


Link to post
Share on other sites

I have written myself a function that automatically adds line feeds, but i never thought to do the tabs. That's a good move.My new line code looks like this

function writeln($line) {//obviously i stole this from javascript, but i use it in place of echo most of the time.	echo $line."\n";}

Share this post


Link to post
Share on other sites

Very nice tutorial. I have always done something similar but nothing this oop. This tutorial could definitely be expanded into a 'clean' html php class so that one just includes it and uses these functions from that class. I like the tutorial and probably will do something with it in the future.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.