[easy] Does spacing your code out affect load times?

Discussion in 'PHP' started by Dirty-Rockstar, Jul 16, 2008.

  1. #1
    Simple question, I like to space my code out a bit. Queries, inserts, functions. And comment them to keep it organized. so 2000 lines becomes 3000 because of this. Does that cause lag? haha also this makes things easier for me too:

    
    $sql="SELECT
    
    table.field,
    table.field,
    table.field,
    table.field,
    table.field,
    table.field,
    table.field,
    table.field,
    
    
    table2.field,
    table2.field,
    table2.field,
    table2.field,
    table2.field,
    
    
    FROM 
    
    table,
    table2,
    
    WHERE 
    
    table.field,='$var' 
    
    AND
    
    table2.field, ='$var2' 
    
    ORDER BY
    
    table.field DESC";
    
    $query=mysql_query($sql) or die(mysql_error());
    $result=mysql_fetch_array($query);
    
    PHP:
    Insead of having it straight across....is that ok and wont cause lag?
    (newbie here)

    <3 thanks = )
     
    Dirty-Rockstar, Jul 16, 2008 IP
  2. CygnetGames

    CygnetGames Peon

    Messages:
    43
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    No, spacing out your PHP code won't cause any problems at all.
    If you did the same thing with HTML code, that would increase the size of the file sent to the client, and so would increase the load time of your page, but doing it in PHP won't affect anything.

    It's a good practice to space out your code, as it makes it easier to read.
    If you work on projects with other people who have to read your code, they would thank you for spacing it out!
     
    CygnetGames, Jul 16, 2008 IP
    Dirty-Rockstar likes this.
  3. Dirty-Rockstar

    Dirty-Rockstar Guest

    Messages:
    252
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks, I didnt know that about html tho. my html is dirty haha :p. another question came to mind, is there that much of a difference in load time ending php whenever possible and putting html on the page you make, then just printing it or using echo? (i am a print booster) currently i just use print combined with my php code with html. much difference? maybe something to do with caching

    thankies

    <3
     
    Dirty-Rockstar, Jul 16, 2008 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    Technically spacing does matter a little. PHP must parse the entire document, so the longer it is (in bytes) the longer it takes to parse. Blank spaces are mostly negligible though, so I wouldn't be worried about them as long as they are there for a purpose.

    The fastest way to handle html is to remove it from print and echo statements:

    
    <?php
    some php code {
    ?>
    <html here />
    <?php 
    }
    ?>
    
    PHP:
    If you need to echo / print strings, the fastest possible way is to 'echo' strings using commas . Also single ' are faster than ".

    
    //fastest
    echo '<something>' , '<something else>' , trim($some_other_string);
    
    PHP:
     
    jestep, Jul 16, 2008 IP
  5. sastro

    sastro Well-Known Member

    Messages:
    214
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #5
    Also your php will be faster when you encode it with zend guard
     
    sastro, Jul 16, 2008 IP