TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-06-2008, 04:53 PM   #1 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Help Indentation style of an array

I am merely wondering as to how people indent their arrays. For example, when you have an array with some pretty substantial lines, putting it all on one line isn't usually the greatest style in the world because you then get stupidly long lines.

I used to format my arrays like the following:

php Code:
$this->m_aAccounts[$szUsername] = array (
                                            'username' => $szUsername,
                                            'password' => $szPassword,
                                            'level_name' => $pLevel->getLevelName(),
                                            'level_flags' => $pLevel->getLevelFlags()
                                        );

But even that is wasting so much white-space to the left of each item. Therefore recently I've been using the following styling method which I feel is far better:

php Code:
$this->m_aAccounts[$szUsername] = array
(
    'username' => $szUsername,
    'password' => $szPassword,
    'level_name' => $pLevel->getLevelName(),
    'level_flags' => $pLevel->getLevelFlags()
);

This is taken from the way I format my Javascript dynamic functions, like so:

javascript Code:
$('myElement').onclick = function()
{
    // Do something fancy.
}

How do you go about indenting your arrays? I'm curious
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 01-06-2008, 04:57 PM   #2 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

I too followed the first method for some time, but also changed to the latter for the exact same reason. However, now I've updated my monitor it doesn't really bother me either way.

The only downside to the second method is that the blocks kind of look like functions at a glance, but that is really a very minor hindrance. There is another alternative:

PHP Code:
$this->m_aAccounts[$szUsername] = array(
    
'username' => $szUsername,
    
'password' => $szPassword,
    
'level_name' => $pLevel->getLevelName(),
    
'level_flags' => $pLevel->getLevelFlags()); 
It's pretty much the same as your second method but without placing the opening and closing brackets on their own line. I think that removes the relation to the function and still provides a good solution. Thoughts?
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 01-06-2008, 05:09 PM   #3 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

I'm going with this style (extras from a piece of code of mine, I'm too lazy to come up with something right now :D):

PHP Code:
$sql->select( array( 'what' => 'p.project_name',
                     
'from' => 'projects p',
                     
'join' => array( array( 'type' => 'left',
                                             
'table' => 'ratings r',
                                             
'on' => 'r.project_id=p.project_id' ) ),
                     
'groupby' => 'p.project_id',
                     
'orderby' => 'p.project_name ASC',
                     
'where' => 'r.rating<>"NULL"' ) ); 
PS: what does "sz" in front of your vars stand for? I know a is for array, o for object and so on, but sz? - string perhaps?
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 01-06-2008, 05:58 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Yep. Technically a zero terminated string which doesn't apply in PHP. But to me it looks a lot nicer than $sVariable.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 01-06-2008, 06:40 PM   #5 (permalink)
The Visitor
 
short's Avatar
 
Join Date: Jan 2008
Posts: 3
Thanks: 0
short is on a distinguished road
Default

PHP Code:
$sql->select
    array( 
        
'what'    => 'p.project_name',
        
'from'    => 'projects p',
        
'join'    => array( 
            array( 
                
'type'  => 'left',
                
'table' => 'ratings r',
                
'on'    => 'r.project_id=p.project_id',
            ), 
        ),
        
'groupby' => 'p.project_id',
        
'orderby' => 'p.project_name ASC',
        
'where'   => 'r.rating<>"NULL"' 
    
)
); 
short is offline  
Reply With Quote
Old 01-06-2008, 08:28 PM   #6 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default

I do it like what Karl posted. I keep it to the left, however I add one tab, and keep the parentheses for array() on the line with something else, so as to not add too many unneeded lines.
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
Old 01-06-2008, 10:25 PM   #7 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
sjaq is on a distinguished road
Default

PHP Code:
Config::set('system', array(
        
'errorController' => 'error',
        
'defaultAction' => 'index',
        
'debug' => true,
        
'rootSafety' => true,
        
'fileCommand' => '/usr/bin/file',
        
'logs' => array(
            
'error' => 'error.log',
            
'default' => 'log.log'
        
),
        
'timezone' => 'Europe/Amsterdam'
)); 
sjaq is offline  
Reply With Quote
Old 01-06-2008, 11:01 PM   #8 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

I generally follow a similar approach to the last post, by sjaq, except that indent depths are always the same (one tab; don't hate me for using tabs) whereas the initial indent is double the length in the code posted above.

I know the following is not strictly about indentation, but I feel it applies here. I like to keep all of the 'key value' (=>) operators nicely lined up as well (using spaces as padding).

All in all, it's just personal preference and one that I've adopted over time.

PHP Code:
// If I were feeling really pernickety,
// I'd order the keys alphabetically ;-)
$aSample = array (
    
'errorController' => 'error',
    
'defaultAction'   => 'index',
    
'debug'           => true,
    
'rootSafety'      => true,
    
'fileCommand'     => 'usr/bin/file',
    
'logs'            => array (
        
'error'       => 'error.log',
        
'default'     => 'log.log'
    
),
    
'timezone'        => 'Europe/Amsterdam'
); 
Salathe is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:58 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design