1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Comment bien programmer en PHP ?

Discussion in 'PHP' started by youness_violon, Oct 6, 2008.

  1. #1
    Bonjour

    ma question et la suivante :

    Comment bien programmer en PHP ?

    Donc si vous avez des conseils, des truc à utiliser/éviter et d'autres choses.

    Par exemple :
    Code le plus court :
    Code :

    for ($i=0; $i<count($array); $i++)
    {
    echo $array[$i];
    }

    Code le plus rapide :
    Code :

    $count = count($array);
    for ($i=0; $i<$count; $i++)
    {
    echo $array[$i];
    }


    Merci d'avance​

     
    youness_violon, Oct 6, 2008 IP
  2. AT-XE

    AT-XE Peon

    Messages:
    676
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I get what you are trying to say but the main language of this forum is English UK/US.
    I would suggest you to edit your post because a mod may give you an infraction for it.

    No Offence
     
    AT-XE, Oct 6, 2008 IP
  3. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #3
    hrm... interesting. obviously, it will differ between callback and w/o it.
    in javascript, for example, the fastest loop can be achieved by a while(count--) { } where count is set to array.length.

    i coded a test for 4 common ways of walking array elements in PHP, timing how long it takes to process in ms:
    <?PHP 
    
    // populate the array
    for ($ii = 0; $ii < 1000; ++$ii)
        $array[] = rand(100000,100000+1000);
        
    // loop 1, with callback to count function
    $timeStart = microtime();
    for ($i=0; $i < count($array); ++$i) 
        echo $array[$i] . " ";
    
    echo "<h1>for loop, with count() callback: " .(microtime() - $timeStart) . " ms</h1>";
    
    // loop 2, count predefined.
    $timeStart = microtime();
    $count = count($array);
    for ($i = 0; $i < $count; $i++) 
        echo $array[$i] . " ";
    
    echo "<h1>for loop, no count() callback: " .(microtime() - $timeStart) . " ms</h1>";
    
    // loop 3. reverse loop 
    $timeStart = microtime();
    $count = count($array);
    while ($count--) 
        echo $array[$count] . " ";
    
    echo "<h1>while() reducing: " .(microtime() - $timeStart) . " ms</h1>";
    
    // loop 4, foreach 
    $timeStart = microtime();
    foreach($array as $item) 
        echo $item . " ";
        
    echo "<h1>foreach loop: " .(microtime() - $timeStart) . " ms</h1>";
    ?>
    PHP:
    here are my results:
    for loop, with count() callback: 0.001327 ms
    for loop, no count() callback: 0.00087000000000001 ms
    while() reducing: 0.00096599999999999 ms
    foreach loop: 0.00062400000000001 ms

    winner: foreach.

    if anyone else wants to comment on this - etc - i know the results are not conclusive enough and would vary dependent on my server load etc - this needs to run 1000 times and get averages... but i'll be interested in hearing people from their experience.
     
    dimitar christoff, Oct 6, 2008 IP