DISQUS

ElbertF Blog: Private: Tips for writing compact PHP code

  • Rik · 9 months ago
    These are some pretty interesting tips, didn't know of some they would actually work. But understand that this doesn't make understanding the code more easy in some cases. 7. for example, when an inexperienced programmer tries to change the value of $bar, this could happen:
    $foo =
    $bar = false
    $foobar = true;

    But thanks for the tips, because they definitely are usefull!
  • svenn · 8 months ago
    Agree, simply write them in 1 line. $foo = $bar = $foobar = false;
    Great article Elbert.
  • Abi · 9 months ago
    I'm with Rik, while these are all perfectly capable of making your code more compact there is literally no reason to do so. The goal of good code is to be readable, and while I think most of the tips are perfectly acceptable it might make it harder for someone not as experienced to figure out what's going on.

    Though I would like to point out that certain ways of coding are actually faster/more efficient, for instance in example 8 using the code this way..

    $variable[] = "value1";
    $variable[] = "value2";

    is slower than doing...

    $variable ( 'value1', 'value2' );


    Also one tip for writing more readable code is by using switch statements, long if else statements can get messy, use a switch to make it easy and readable
  • CoenJacobs · 9 months ago
    Great article, although I think that some tips aren't working for me. But most of it should help me to compress the length of my codings. Great blog btw, congrats! I hope to read more from you!
  • Ozh · 8 months ago
    Syntax itself can make things shorter.

    if (condition)
    {
    do something;
    }
    else
    {
    do something else;
    }

    is 8 lines, while

    if (condition) {
    do something;
    } else {
    do somerhing else;
    }

    is only 5 lines and more readable to me