www.webdeveloper.com
Page 3 of 3 FirstFirst 123
Results 31 to 42 of 42

Thread: How to: write clean PHP

  1. #31
    Join Date
    Jul 2009
    Location
    My house, sitting at the computer. Duh!
    Posts
    118
    Quote Originally Posted by NogDog View Post
    It definitely saves time when you get an SQL error like "error near 'or' on line 20" versus "error near 'or' on line 1".
    That's for sure.

    I kind of prefer the syntax of languages like Python, where if you don't properly indent code, it simply doesn't work.

    Code:
    if x < y:
    	print 'something'

  2. #32
    Join Date
    Apr 2010
    Posts
    227
    Quote Originally Posted by THEFOOL View Post
    Belrick, nice with the ',' seperation but the deep indent takes to much time in notepad and would force horizontal scrolling in most forums? The brace on the function line makes it harder for us dislexics to pair them off. Brace as in two pistols. Not to mention the forcing of scrolling in the forums again but that is what indenting does. I like to do without it.
    I use netbeans IDE and selecting one brace highlights the matched pair.
    Also netbeans auto lines up each new line. SO if top line is 1 tab in a carraige return brings it to start point one tab in.

    Netbeans also allows you to set the number of spaces for each tab.
    You can also select multiple lines and tab as one.

    Takes literally seconds of extra coding but saves minutes++ when debugging and MCA

    Also

    When outputting HTML i like to indent to show element relationships.

    like

    HTML Code:
    echo "
    <table>
        <tr>
            <td>HI</td>
        </tr>
    </table>
    ";
    Last edited by Belrick; 05-06-2012 at 05:54 PM.

  3. #33
    Join Date
    Apr 2010
    Posts
    227
    Quote Originally Posted by THEFOOL View Post
    Right on NogDog, I still preffer this though, ie. no indent and space between statements.

    PHP Code:
    <?php 

    function foo(PDO $pdo$bar$time


    $sql 
    SELECT 
    foo_id, 
    foo_name, 
    foo_timestamp 
    FROM foo.bar 
    WHERE 
    foo_name = :bar 
    AND 
    foo_timestamp > :time 
    "


    $stmt $pdo->prepare($sql); 

    if(
    $stmt == false


    throw new 
    Exception("Prepare failed: ".print_r($pdo->errorInfo()));
     

        
    $stmt->bindParam(':bar'$barPDO::PARAM_STR); 
        
    $stmt->bindParam(':time'$timePDO::PARAM_STR); 
        
    if(
    $stmt->execute() == false

            
    throw new 
    Exception("Query failed: ".print_r($stmt->errorInfo())); 
        

        
    return 
    $stmt->fetchAll();
     
    }


    I know it may sound pathetic but i cannot handle your code.

    Im like a woman with OCD walking into a messy house just itching to clean up the place!
    "OK, time to break out the mops, the tabs and carriage returns!

  4. #34
    Join Date
    Apr 2010
    Posts
    227
    One other thing to do is instead of having lots of little functions. (say a class setters) i like to merge them into one function then seperate out each 'setter' by a switch. I do this because i end up with a lot of functions anyway normally.

    eg:
    PHP Code:

    function set_property($property,$value){

        switch (
    $property){ 
            case 
    "HEIGHT":
                
    $this->height $value;
            break;
      
        }   


  5. #35
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    Quote Originally Posted by Belrick View Post
    One other thing to do is instead of having lots of little functions. (say a class setters) i like to merge them into one function then seperate out each 'setter' by a switch. I do this because i end up with a lot of functions anyway normally.

    eg:
    PHP Code:

    function set_property($property,$value){

        switch (
    $property){ 
            case 
    "HEIGHT":
                
    $this->height $value;
            break;
      
        }   

    Or just use the "magic" __get() and __set() methods. (http://www.php.net/manual/en/languag...php#object.set)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #36
    Join Date
    Jul 2009
    Location
    My house, sitting at the computer. Duh!
    Posts
    118
    I'm glad we have NogDog on this forum. It it weren't for his wicked awesome mastery of the PHP language, I'd still be stuck in some nasty holes.

  7. #37
    Join Date
    May 2012
    Posts
    1
    For me a good PHP programmer means someone who is efficient - who can solve problems and build quality web based software quickly and in a way that allows easier maintenance and extending of the program.

  8. #38
    Join Date
    Nov 2002
    Posts
    2,632
    Quote Originally Posted by THEFOOL View Post
    notepad
    I never understand why people still use this considering the free alternatives. It's like seeing somebody use an abacus to do math.

  9. #39
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    Quote Originally Posted by spufi View Post
    I never understand why people still use this considering the free alternatives. It's like seeing somebody use an abacus to do math.
    +1 (Though an expert abacus user can do basic math calculations very quickly, mind you -- a better analogy might be calculating cube roots with just pencil and paper. )

    I can't imagine doing any serious coding without, at a minimum, built-in syntax-checking and auto-indent. And when you add things like syntax highlighting, auto-complete, and so forth; why would anyone use Notepad by choice?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  10. #40
    Join Date
    Jul 2009
    Location
    My house, sitting at the computer. Duh!
    Posts
    118
    I'm a NetBeans user coming from TextWrangler. While all I really "needed" was syntax highlighting, having the automatic error checking made my development go so much faster. And of course the auto complete is kinda nice too. The thing that really sold me on NetBeans though, was the built in SVN and Database management. Doesn't get much better than that.

  11. #41
    Join Date
    Apr 2010
    Posts
    227
    Quote Originally Posted by NogDog View Post
    Or just use the "magic" __get() and __set() methods. (http://www.php.net/manual/en/languag...php#object.set)
    Hi!

    Excellent point.

    Teach me for being lazy because in reality i use my example for a lot more complex functions.

    Cheers!

  12. #42
    Join Date
    Nov 2009
    Posts
    268

    Question How to: write clean PHP

    Yeah NogDog and ScottyBoy, I guess that I wanted to ask that this wouldn't be offensive enough to prohibit advice from forum administrators or advisers as that is the bottom line in my opinion.

    Example?
    PHP Code:
    $sql '
     
    SELECT username //not:user_name
    FROM pcusers //not:pc_users or camel case
    WHERE userid=2
     
    '

    The PHP code box already contains the Indent?
    The opening and closing are separated from the statement so that I can distinguish between the two?

    I know this is a short example and script pages seem to go on for ever but at present I'm the only one who reads mine other than the questions I post in the forums. I don't crunch them any more and I've decided to use this style in the last two months, specifically for posting to forums. I was born dyslexic and had a teacher who caught that fact when I was in Junior high school so I was soon there after awarded the 'Reading' prize? The main point again, being not to offend any would be help? Of course the teacher couldn't fix my A.D.D. (attention deficit disorder) which we now believe is the true cause for rampant use of 'IeSpell'.
    Last edited by THEFOOL; 05-11-2012 at 11:57 AM. Reason: The PHP code box already contains the Indent?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles