Please login or register.

Login with username, password and session length
Advanced search  

News:

You need/want an older version of sNews ? Download an older/unsupported version here.

Pages: [1] 2

Author Topic: php coding - why the *really* wide lines?  (Read 13250 times)

lessismore

  • Jr. Member
  • **
  • Karma: 0
  • Posts: 67
php coding - why the *really* wide lines?
« on: April 14, 2008, 11:32:08 pm »

Most printers only print 80 chars/line and most editors are similar - when a line of code is really long, it wraps onto the next line.

Example, the html_input() function in snews has 15 parameters. When it is invoked, the call is one contiguous string which wraps in a window (IMHO, this makes indenting of programming for control structure hard to follow).

Is there a reason carriage returns are not used to limit wrapping to maintain visual control structure?

Example, is there something wrong with this (other than three CRs and six whitespaces?):

Code: [Select]
echo html_input('text', 'home_sef', 'webSEF',
    s('home_sef') == '' ? l('home_sef') : s('home_sef'), l('a_home_sef'),
    '', 'onkeypress="return SEFrestrict(event);"',
    '', '', '', '', '', '', '', '');
vs
Code: [Select]
echo html_input('text', 'home_sef', 'webSEF', s('home_sef') == '' ? l('home_sef') : s('home_sef'), l('a_home_sef'), '', 'onkeypress="return SEFrestrict(event);"','', '', '', '', '', '', '', '');
I come from a C/ASM background where compilers are used and I know PHP is interpreted. When compilers are used, the format is somewhat irrelevant as long as the syntax is correct - I'm thinking old-school coding styles may slow down interpreted PHP processing? Although, a couple of bytes to improve readability cannot be that bad - but I rarely see it?

Logged

centered

  • Guest
Re: php coding - why the *really* wide lines?
« Reply #1 on: April 14, 2008, 11:38:11 pm »

I am beginning to dislike html_input and sometimes just for that one reason another reason is I have disliked not knowing what each 15 parameters are...

What is more understandable
(default 1.6 contact)
Code: [Select]
if (!isset($_POST['contactform'])) {
echo '<div class="commentsbox"><h2>'.l('contact').'</h2>';
echo '<p>'.l('required').'</p>';
echo html_input('form', '', 'post', '', '', '', '', '', '', '', '', '', 'post', db('website'), '');
echo html_input('text', 'name', 'name', '', '* '.l('name'), 'text', '', '', '', '', '', '', '', '', '');
echo html_input('text', 'email', 'email', '', '* '.l('email'), 'text', '', '', '', '', '', '', '', '', '');
echo html_input('text', 'weblink', 'weblink', '', l('url'), 'text', '', '', '', '', '', '', '', '', '');
echo html_input('textarea', 'message', 'message', '', '* '.l('message'), '', '', '', '', '', '5', '5', '', '', '');
echo mathCaptcha();
echo '<p>';
echo html_input('hidden', 'ip', 'ip', $_SERVER['REMOTE_ADDR'], '', '', '', '', '', '', '', '', '', '', '');
echo html_input('hidden', 'time', 'time', time(), '', '', '', '', '', '', '', '', '', '', '');
echo html_input('submit', 'contactform', 'contactform', l('submit'), '', 'button', '', '', '', '', '', '', '', '', '');
echo '</p></form></div>';
$_SESSION[db('website').'contact'] = 0;
}

(taken from html source and added some php)
Code: [Select]
if (!isset($_POST['contactform'])) {
?><h2>Contact</h2>
<p>* = required field</p>
<form method="post" action="<?php echo _SITE?>" id="post" accept-charset="<?php echo s('charset'); ?>">
<p><label for="name">* Name</label>:<br /><input type="text" name="name" id="name" class="text" value="" /></p>
<p><label for="email">* Email</label>:<br /><input type="text" name="email" id="email" class="text" value="" /></p>
<p><label for="weblink">Website URL</label>:<br /><input type="text" name="weblink" id="weblink" class="text" value="" /></p>
<p><label for="message">* Message</label>:<br /><textarea name="message" rows="5" cols="5" id="message"></textarea></p>
<?php echo mathCaptcha(); ?>
<p>
<input type="hidden" name="ip" id="ip" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>" />
<input type="hidden" name="time" id="time" value="<?php echo time(); ?>" />
<input type="submit" name="contactform" id="contactform" class="button" value="Submit" />
</p>
</form>
<?php 
$_SESSION[_SITE.'contact'] = 0;


I prefer the bottom since I know what is going on.

Logged

codetwist

  • Hero Member
  • *****
  • Karma: 50
  • Posts: 940
Re: php coding - why the *really* wide lines?
« Reply #2 on: April 14, 2008, 11:57:03 pm »

@ lessismore : Before and around php v.3 it was considered relevant to minimize code line numbers. AFAIK after v.3 it basically doesn't matter performance-wise as far as different code formatting is considered.
Logged

lessismore

  • Jr. Member
  • **
  • Karma: 0
  • Posts: 67
Re: php coding - why the *really* wide lines?
« Reply #3 on: April 15, 2008, 12:29:31 am »

Thanks codey & eq.

I tend to not like long/wide lines of code - and tabs are the work of the devil.  I still wonder why it's done - it throws indentation to show control flow under the bus and makes printouts unreadable.

The html_input func is sophisticated, but that is also it's problem - I'm trying to make some custom forms and it does suck to not have full comprehension of what is going on under the hood. It's a good learning experience though.

Makes me want to stick with my caveman vi editor and old-school K&R C-style programming.
Logged

Joost

  • Guest
Re: php coding - why the *really* wide lines?
« Reply #4 on: April 15, 2008, 02:42:15 am »

Although I don't like function html_input for the same reasons as Jason, it looks good in my editor, when its kept on one line.
Tabs? Not my style. More than two tabs in a row, I find annoying. I guess, it has got something to do with the way we adjust our highlighters, or not using them at all.

For performance:
Echoing strings one after another,  is faster than echoing concatenated strings.

This:
echo  l('divider'),' <a href="',_SITE,'index,php?action=admin_article&amp;id=',$r['id'],'">',l('edit'),'</a> ';
is faster than
echo  l('divider').' <a href="'._SITE.'index.php?action=admin_article&amp;id='.$r['id'].'">'.l('edit').'</a> ';

Logged

lessismore

  • Jr. Member
  • **
  • Karma: 0
  • Posts: 67
Re: php coding - why the *really* wide lines?
« Reply #5 on: April 15, 2008, 03:18:52 am »

ooh - nice tidbit.

this site is a plethora of info - mucho gracious amigos.
Logged

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6020
  • Semantically Challenged
    • snews.ca
Re: php coding - why the *really* wide lines?
« Reply #6 on: April 15, 2008, 03:40:15 am »

For performance:
Echoing strings one after another,  is faster than echoing concatenated strings.

This:
echo  l('divider'),' <a href="',_SITE,'index,php?action=admin_article&amp;id=',$r['id'],'">',l('edit'),'</a> ';
is faster than
echo  l('divider').' <a href="'._SITE.'index.php?action=admin_article&amp;id='.$r['id'].'">'.l('edit').'</a> ';


Ok Joost... hang tight on this one for a minute... you're getting yourself into a possible tutorial here. Help me out a bit.
Do you mean we can replace those periods between variables with commas and it still works?
What is a "concatenated string" and what makes it different from a non-concatenated string?

Some time ago I read somewhere that tying (for example) 20 strings together as one long echo was more efficient than having 20 separate echo strings... because they'd all be parsed in one shot. Kind of like saying a hundred words in one long breath rather than taking breaths between shorter sentences... like this:

Code: [Select]
<?php

echo 
'<div class"something_nice">'
.'<p>Hello. My name is Bob. What is your name?</p>'
.'<p>I go to a nice bar on Saturday nights in another neighbourhood.'
.'Sometimes I take a few friends along with me and we jam some tunes.'
.'We are always careful not to drink more than a few beers.'
.'Otherwise, Joost the bartender will toss us out earlier than we like.</p>';
.'</div>';
?>


instead of this:

Code: [Select]
<?php

echo 
'<div class"something_nice">';
echo '<p>Hello. My name is Bob. What is your name?</p>';
echo '<p>I go to a nice bar on Saturday nights in another neighbourhood.';
echo 'Sometimes I take a few friends along with me and we jam some tunes.';
echo 'We are always careful not to drink more than a few beers.';
echo 'Otherwise, Joost the bartender will toss us out earlier than we like.</p>';
echo '</div>';

?>

Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

jlhaslip

  • Sr. Member
  • ****
  • Karma: 16
  • Posts: 374
    • My snews with AEF Forum site
Re: php coding - why the *really* wide lines?
« Reply #7 on: April 15, 2008, 06:58:37 am »

Do you want that Benchmarked, Keyrocks?
Logged
Yes! I have no siggy.

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6020
  • Semantically Challenged
    • snews.ca
Re: php coding - why the *really* wide lines?
« Reply #8 on: April 15, 2008, 02:01:10 pm »

Do you want that Benchmarked, Keyrocks?

If you can... please do. I've never set myself up to benchtest code... not sure how to do that.
Perhaps we might make up a couple more larger examples to test as well.
It would be interesting to know if this actually does have a bearing on performance or not.
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

jlhaslip

  • Sr. Member
  • ****
  • Karma: 16
  • Posts: 374
    • My snews with AEF Forum site
Re: php coding - why the *really* wide lines?
« Reply #9 on: April 16, 2008, 06:49:35 am »

K, I'll get right on it...
Logged
Yes! I have no siggy.

jlhaslip

  • Sr. Member
  • ****
  • Karma: 16
  • Posts: 374
    • My snews with AEF Forum site
Re: php coding - why the *really* wide lines?
« Reply #10 on: April 16, 2008, 09:58:11 am »

Code: [Select]
1 3 30 300 3000


echos .00008201599121090 .00007891654968260 .00021386146545400 0.00291681289673 1.73170995712
.00009083747863770

concat .00007295608520510 .00007987022399900 .00017595291137700 .00313401222229000 .84457778930700000
.00007200241088870

html .00008296966552730 .00007486343383790 .00021195411682100 .00305891036987000 .75493597984300000
.00007104873657230


I'm thinking another system for the test should be done. Possibly the PEAR Library on another test? Let me know if it will be required. ???
               
               
« Last Edit: April 16, 2008, 11:45:20 am by jlhaslip »
Logged
Yes! I have no siggy.

centered

  • Guest
Re: php coding - why the *really* wide lines?
« Reply #11 on: April 16, 2008, 11:29:38 am »

What exactly was tested here?
Logged

jlhaslip

  • Sr. Member
  • ****
  • Karma: 16
  • Posts: 374
    • My snews with AEF Forum site
Re: php coding - why the *really* wide lines?
« Reply #12 on: April 16, 2008, 11:48:39 am »

Several different methods for outputting the strings as per above.
Concatenated strings, Echo'd strings and html output. The code I used is here:
Code: [Select]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Benchmark Script</title>


  <meta http-equiv="content-type" content="text/html; charset=utf-8" />

  <meta http-equiv="content-style-type" content="text/css" />

  <meta http-equiv="keywords" content="" />

  <meta http-equiv="description" content="Script to Benchmark a Site download time, Loops 'n' times. Configure site/file name and the script returns the average time for access (n) times." />

  <meta http-equiv="reply-to" content="jlhaslip@yahoo.ca" />

  <meta http-equiv="author" content="Jim Haslip" />

  <style type="text/css">
/*<![CDATA[*/
/* =======
Template CSS
============*/
* html { margin:0; padding:0;}
html, body { height: 100%; }
#wrapper { min-height: 100%; } /* div you want to stretch */
body { margin:0 auto; text-align:center; background-color: #eeddbb; }
p { margin: 1em; padding:1em; text-align:left }
#wrapper { margin: 0 auto; width: 1000px; background-color: #ffeecc; border: 1px solid #666666; }
#header { margin: 1em 0; text-align:center; border-bottom: 1px solid #666666; }
#header h1 { margin: 1em 0; }
#page_contents { text-align: left; }
#footer { margin: 1em auto; padding: 1em; text-align:center; border-top: 1px solid #666666; }
#footer a { margin: 1em auto; padding: .15em; }
/* =======
Page specific CSS Below this line
============*/
/*]]>*/
  </style><!--[if lte IE 6]> <style type="text/css"> #wrapper { height: 100%; } </style> <![endif]-->
</head>


    <body>

<div id="wrapper">
<div id="header">
<h1> ... Page Header here ... </h1>

<!-- header --> </div><!-- header -->
<div id="page_contents" style="padding: 1em;">
<div id="contentheader" style=""></div>

<div>

<?php
/*
file: benchmark/index.php
author: jlhaslip@yahoo.ca
*/

/*
* Error seetings for debugging
*/

ini_set("display_errors"1);
error_reporting(E_WARN);

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;
}
/**
 * Simple function to replicate PHP 5 behaviour
 */
function microtime_float() {
    list(
$usec$sec) = explode(" "microtime());
    return ((float)
$usec + (float)$sec);
}
function 
getmtime() {
$a explode (' ',microtime_float());
return(double) $a[0] + $a[1];
}

/**
* Output Header
*/

$time_start microtime_float();


/**
* Main Loop Start
*/

$iter 3// number of iterations
$start getmtime();
for (
$i 1$i <= $iter$i++){



echo '<div class"something_nice">';
echo '<p>Hello. My name is Bob. What is your name?</p>';
echo '<p>I go to a nice bar on Saturday nights in another neighbourhood.';
echo 'Sometimes I take a few friends along with me and we jam some tunes.';
echo 'We are always careful not to drink more than a few beers.';
echo 'Otherwise, Joost the bartender will toss us out earlier than we like.</p>';
echo '</div>';



}

$time_end microtime_float();
$time $time_end $time_start;


/**
* Overall Average Load Time
*/

echo tabs(1),nls() . '<p><b>Total Load Time: &nbsp;' $time '</b> for '$iter ' loops.' ;


/**
* End of Script
*/
?>


</div>

</div>

<div id="footer" style="clear: both;"> <a href="http://validator.w3.org/check?uri=referer">validate
the xhtml</a>&nbsp;&nbsp;&copy;&nbsp;<a href="http://jlhaslip.com/">jlhaslip.com</a>&nbsp;&nbsp;<a href="http://jigsaw.w3.org/css-validator/">validate the css</a>
<a rel="license" href="http://creativecommons.org/licenses/by-sa/2.5/ca/"><img alt="Creative Commons License" style="border-width: 0pt;" src="http://i.creativecommons.org/l/by-sa/2.5/ca/88x31.png" /></a>
This script is licenced under a
<a rel="license" href="http://creativecommons.org/licenses/by-sa/2.5/ca/">Creative
Commons Licence</a>. </div>

<!-- footer -->
</div>
<!--
concatenation
echo '<div class"something_nice">'
.'<p>Hello. My name is Bob. What is your name?</p>'
.'<p>I go to a nice bar on Saturday nights in another neighbourhood.'
.'Sometimes I take a few friends along with me and we jam some tunes.'
.'We are always careful not to drink more than a few beers.'
.'Otherwise, Joost the bartender will toss us out earlier than we like.</p>'
.'</div>';
Echo's
echo '<div class"something_nice">';
echo '<p>Hello. My name is Bob. What is your name?</p>';
echo '<p>I go to a nice bar on Saturday nights in another neighbourhood.';
echo 'Sometimes I take a few friends along with me and we jam some tunes.';
echo 'We are always careful not to drink more than a few beers.';
echo 'Otherwise, Joost the bartender will toss us out earlier than we like.</p>';
echo '</div>';
Html
<div class"something_nice">
<p>Hello. My name is Bob. What is your name?</p>
<p>I go to a nice bar on Saturday nights in another neighbourhood.
Sometimes I take a few friends along with me and we jam some tunes.
We are always careful not to drink more than a few beers.
Otherwise, Joost the bartender will toss us out earlier than we like.</p>
</div>
-->

<!-- wrapper -->
</body>
</html>
Logged
Yes! I have no siggy.

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6020
  • Semantically Challenged
    • snews.ca
Re: php coding - why the *really* wide lines?
« Reply #13 on: April 16, 2008, 02:19:08 pm »

echos:      .00009083747863770 = 9.08 hundred-thousandths of a second      
concat:      .00007200241088870 = 7.20 hundred-thousandths of a second
html:      .00007104873657230 = 7.10 hundred-thousandths of a second            

So... from this one test comparison... the results appear to suggest:
1) When parsing the script within a PHP session... it is more efficient to combine all lines within one echo statement (concatenating) rather than run it as it separate echo strings.

2) The HTML version parses a little quicker than the concatenated version.
   This suggests that... when you have a large block of HTML in a PHP function...
   it gets parsed quicker by escaping PHP to run the HTML in its entirety then starting a new PHP session once completed.

3) The differences between the three are so miniscule that the outcome seems moot.  :)
« Last Edit: April 16, 2008, 02:28:59 pm by Keyrocks »
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

centered

  • Guest
Re: php coding - why the *really* wide lines?
« Reply #14 on: April 16, 2008, 03:07:18 pm »

Doug, did you look at the 300x numbers?  echo takes 1.73 seconds to complete, now think about echoing html_input....

Actually, why was html_input put in place of the html blocks back in 1.4?
Logged
Pages: [1] 2