Skip to content

Instantly share code, notes, and snippets.

@stevedev
Last active December 27, 2015 04:48

Revisions

  1. stevedev revised this gist Nov 1, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion steves-php.md
    Original file line number Diff line number Diff line change
    @@ -178,7 +178,6 @@ class Multiplication {
    }

    // Bad:
    <?php

    class Multiplication {
    // Need to set this somewhere.
  2. stevedev revised this gist Nov 1, 2013. 1 changed file with 24 additions and 0 deletions.
    24 changes: 24 additions & 0 deletions steves-php.md
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,8 @@
    Don't use newline braces.

    ```php
    <?php

    // Good:

    if ( $foo ) {
    @@ -29,6 +31,8 @@ else
    Always use pascal case.

    ```php
    <?php

    // Good:
    class MyAwesomeClass { ... }

    @@ -41,6 +45,8 @@ class terrible_class { ... }
    Use camelCase.

    ```php
    <?php

    // Good:
    function myFoo( $bar ) { ... }

    @@ -62,6 +68,8 @@ if ( $foo == 1 ) {
    Indent your case statements, and the code inside the case statements. Don't forget the break;

    ```php
    <?php

    // Good:
    switch ( $option ) {
    case 'foo':
    @@ -81,6 +89,8 @@ switch ( $option ) {
    Use underscore + lowercase.

    ```php
    <?php

    // Good:
    $my_foo = 1;

    @@ -93,6 +103,8 @@ $myFoo = 1;
    Always have a space between opening and closeing parens. Also, best number of params is 0. Every number after that is worse. If I see a function with more than 3 params, pepare for a war.

    ```php
    <?php

    // Good:
    $baz = goFoo( $bar );

    @@ -111,6 +123,8 @@ PHP arrays are bastards. They are a mix of associative indexed and scalar indexe
    Having two or three items is fine to keep inline, more than that put each on it's own line. Indent only two spaces.

    ```php
    <?php

    // Good:
    $my_foo = array( 'item1', 'item2', 'item3');
    $my_foo = array(
    @@ -128,6 +142,8 @@ $my_foo = array( 'item1', 'item2', 'item3', 'item4', 'item5', 'item6' );

    Also, do nice indenting when you have arrays of arrays:
    ```php
    <?php

    $my_foo = array(
    'item1' => 1,
    'other' => array(
    @@ -143,6 +159,8 @@ $my_foo = array(
    Group your code into similar functionality. I like to structure my functions like this: define local variable stuff, newline, process something, newline, return.

    ```php
    <?php

    // Good:
    class Multiplication {

    @@ -160,6 +178,8 @@ class Multiplication {
    }

    // Bad:
    <?php

    class Multiplication {
    // Need to set this somewhere.
    private $multiplier = false;
    @@ -176,6 +196,8 @@ class Multiplication {
    Always declared as uppercase, separated with underscores. This is just a PHP standard.

    ```php
    <?php

    define('MAX_VOLUME', 11);

    class Foo {
    @@ -188,6 +210,8 @@ class Foo {
    If your file only contains PHP, do not put in a closing PHP statement.

    ```php
    <?php

    // Good:
    <?php

  3. stevedev revised this gist Nov 1, 2013. 1 changed file with 22 additions and 1 deletion.
    23 changes: 22 additions & 1 deletion steves-php.md
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,6 @@
    Don't use newline braces.

    ```php
    <?php
    // Good:

    if ( $foo ) {
    @@ -184,3 +183,25 @@ class Foo {
    }
    ```

    # Opening/closing PHP Statements

    If your file only contains PHP, do not put in a closing PHP statement.

    ```php
    // Good:
    <?php

    class Something { ... }

    --- EOF ---

    // Bad:

    <?php

    class something { ... }

    ?>

    --- EOF ---
    ```
  4. stevedev revised this gist Nov 1, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions steves-php.md
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@
    Don't use newline braces.

    ```php
    <?php
    // Good:

    if ( $foo ) {
  5. stevedev revised this gist Nov 1, 2013. 1 changed file with 20 additions and 18 deletions.
    38 changes: 20 additions & 18 deletions steves-php.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    Don't use newline braces.

    <code>
    ```php
    // Good:

    if ( $foo ) {
    @@ -22,24 +22,25 @@ else
    rejection();
    }
    </code>
    ```

    # Class Naming

    Always use pascal case.

    <code>
    ```php
    // Good:
    class MyAwesomeClass { ... }

    // Bad:
    class terrible_class { ... }
    </code>
    ```

    # Function Naming

    Use camelCase.

    <code>
    ```php
    // Good:
    function myFoo( $bar ) { ... }

    @@ -54,13 +55,13 @@ if ( $foo == 1 ) {
    } else {
    // Yeah this is starting to go badly.
    }
    </code>
    ```

    # Switch Statements

    Indent your case statements, and the code inside the case statements. Don't forget the break;

    <code>
    ```php
    // Good:
    switch ( $option ) {
    case 'foo':
    @@ -73,30 +74,31 @@ switch ( $option ) {
    // Some sort of default
    break;
    }
    ```

    # Variable naming

    Use underscore + lowercase.

    <code>
    ```php
    // Good:
    $my_foo = 1;

    // Bad:
    $myFoo = 1;
    </code>
    ```

    # Function Params

    Always have a space between opening and closeing parens. Also, best number of params is 0. Every number after that is worse. If I see a function with more than 3 params, pepare for a war.

    <code>
    ```php
    // Good:
    $baz = goFoo( $bar );

    // Bad:
    $baz = goFoo($bar);
    </code>
    ```

    # Indenting:

    @@ -108,7 +110,7 @@ PHP arrays are bastards. They are a mix of associative indexed and scalar indexe

    Having two or three items is fine to keep inline, more than that put each on it's own line. Indent only two spaces.

    <code>
    ```php
    // Good:
    $my_foo = array( 'item1', 'item2', 'item3');
    $my_foo = array(
    @@ -122,10 +124,10 @@ $my_foo = array(

    // Bad:
    $my_foo = array( 'item1', 'item2', 'item3', 'item4', 'item5', 'item6' );
    </code>
    ```

    Also, do nice indenting when you have arrays of arrays:
    <code>
    ```php
    $my_foo = array(
    'item1' => 1,
    'other' => array(
    @@ -134,13 +136,13 @@ $my_foo = array(
    ),
    'item3' => 3
    );
    </code>
    ```

    # New lines

    Group your code into similar functionality. I like to structure my functions like this: define local variable stuff, newline, process something, newline, return.

    <code>
    ```php
    // Good:
    class Multiplication {

    @@ -167,17 +169,17 @@ class Multiplication {
    return $product;
    }
    }
    </code>
    ```

    # Constants:

    Always declared as uppercase, separated with underscores. This is just a PHP standard.

    <code>
    ```php
    define('MAX_VOLUME', 11);

    class Foo {
    const MAX_FOO_COUNT = 5;
    }
    </code>
    ```

  6. stevedev created this gist Nov 1, 2013.
    183 changes: 183 additions & 0 deletions steves-php.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,183 @@
    # Braces

    Don't use newline braces.

    <code>
    // Good:

    if ( $foo ) {
    send($foo);
    } else {
    rejection();
    }

    // Bad:

    if ( $foo )
    {
    send($foo);
    }
    else
    {
    rejection();
    }
    </code>

    # Class Naming

    Always use pascal case.

    <code>
    // Good:
    class MyAwesomeClass { ... }

    // Bad:
    class terrible_class { ... }
    </code>

    # Function Naming

    Use camelCase.

    <code>
    // Good:
    function myFoo( $bar ) { ... }

    // Bad:
    function my_foo($bar) { ... }

    // Good:
    if ( $foo == 1 ) {
    doAThing();
    } elseif ( $foo == 2 ) {
    // probably should have used a switch statement at this point. ;)
    } else {
    // Yeah this is starting to go badly.
    }
    </code>

    # Switch Statements

    Indent your case statements, and the code inside the case statements. Don't forget the break;

    <code>
    // Good:
    switch ( $option ) {
    case 'foo':
    doFoo();
    break;
    case 'bar':
    doBar();
    break;
    default:
    // Some sort of default
    break;
    }

    # Variable naming

    Use underscore + lowercase.

    <code>
    // Good:
    $my_foo = 1;

    // Bad:
    $myFoo = 1;
    </code>

    # Function Params

    Always have a space between opening and closeing parens. Also, best number of params is 0. Every number after that is worse. If I see a function with more than 3 params, pepare for a war.

    <code>
    // Good:
    $baz = goFoo( $bar );

    // Bad:
    $baz = goFoo($bar);
    </code>

    # Indenting:

    Always use spaces. Only use two spaces. Do not use tabs. Tabs make code look different on different setups (see: servers). Make it consistent by using spaces.

    # Array/Hash definitions

    PHP arrays are bastards. They are a mix of associative indexed and scalar indexed items. They can contain anything. You could describe the PHP array as a Hash + Array all jumbled together in to some sort of frankenstein data type.

    Having two or three items is fine to keep inline, more than that put each on it's own line. Indent only two spaces.

    <code>
    // Good:
    $my_foo = array( 'item1', 'item2', 'item3');
    $my_foo = array(
    'item1',
    'item2',
    'item3',
    'item4',
    'item5',
    'item6'
    );

    // Bad:
    $my_foo = array( 'item1', 'item2', 'item3', 'item4', 'item5', 'item6' );
    </code>

    Also, do nice indenting when you have arrays of arrays:
    <code>
    $my_foo = array(
    'item1' => 1,
    'other' => array(
    'sub1' => 1,
    'sub2' => 2
    ),
    'item3' => 3
    );
    </code>

    # New lines

    Group your code into similar functionality. I like to structure my functions like this: define local variable stuff, newline, process something, newline, return.

    <code>
    // Good:
    class Multiplication {

    // Need to set this somewhere.
    private $multiplier = false;

    function mutiplyNumber ( $multiplicand ) {
    $multiplier = $this->multiplier ? $this->multiplier : 2; // Default to two.

    $product = $multiplicand * $multiplier;

    return $product;
    }

    }

    // Bad:
    class Multiplication {
    // Need to set this somewhere.
    private $multiplier = false;
    function mutiplyNumber ( $multiplicand ) {
    $multiplier = $this->multiplier ? $this->multiplier : 2; // Default to two.
    $product = $multiplicand * $multiplier;
    return $product;
    }
    }
    </code>

    # Constants:

    Always declared as uppercase, separated with underscores. This is just a PHP standard.

    <code>
    define('MAX_VOLUME', 11);

    class Foo {
    const MAX_FOO_COUNT = 5;
    }
    </code>