Extra spaces

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
User avatar
viraj
Forum Newbie
Posts: 11
Joined: Wed Nov 19, 2008 7:52 am
Location: Colombo - Sri Lanka

Extra spaces

Post by viraj »

Hi all,

First of all sorry If this is being discussed somewhere else in the form. But I really search for this and couldn't find any.

I just need to make my code more readable by adding extra spaces. So I need to know if the following cases ( numbered ) would the be legal ? if I put what will happen with PHP engine - what will the PHP do for these cases.

I have seen some places like Joomla use these styles;
eg. in Joomla, includes/database.php there are extra spaces just after start bracket and just before end bracket.

Code: Select all

 
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
 
require_once( dirname(__FILE__)  .'/../libraries/loader.php' );
 
jimport( 'joomla.database.database' );
 
1. extra spaces before ending and after starting the brackets, like this;

Code: Select all

function myFunction( $username = '', $password = '' )
instead of

Code: Select all

public function login($username='', $password='')
2. to align things - this is much much easy to read

Code: Select all

 
define( '__DBHOST'    , 'localhost'   );
define( '__DATABASE'  , 'table_name'  );
define( '__DBUSER'    , 'root'        );
define( '__DBPASS'    , 'pass'        );
 
instead of

Code: Select all

 
define('__DBHOST', 'localhost');
define('__DATABASE', 'table_name');
define('__DBUSER', 'root');
define('__DBPASS', 'pass');
 
3. and in Arrays

Code: Select all

 
$_SESSION[ 'username' ] = $user[ 0 ][ 'username' ];
$_SESSION[ 'userId'   ] = $user[ 0 ][ 'id'];
 
instead of

Code: Select all

 
$_SESSION['username'] = $user[0]['username'];
$_SESSION['userId'] = $user[0]['id'];
 
What do you guys think ? and PHP Gurus Please correct me in good way :wink:

Thanks
Viraj
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Extra spaces

Post by andyhoneycutt »

White-space is perfectly legal inside of syntax statements. I like to clean my code (making it more human-readable) using whitespace, lining up operators, etc. What you've proposed is perfectly acceptable syntax and in my opinion preferred.

-Andy
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Extra spaces

Post by califdon »

I agree with Andy, but don't be afraid to try these things. PHP won't bite you!
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Extra spaces

Post by Syntac »

That's all perfectly legal unless you try something like this:

Code: Select all

<?php echo "Value of index 1: $some_array[ 1 ];"; ?>
...in which case, PHP will throw an error.
User avatar
viraj
Forum Newbie
Posts: 11
Joined: Wed Nov 19, 2008 7:52 am
Location: Colombo - Sri Lanka

Re: Extra spaces

Post by viraj »

Thanks Andy and Willie Nelson for your suggestions. and yes indeed PHP won't bit. :D We have to make our code more and more human-readable.

Syntac, you raised a good point though.

As you said I wonder why this throw an error

Code: Select all

echo "Value of index 1: $some_array[ 1 ]";
and this is not

Code: Select all

echo "Value of index 1: " . $some_array[ 1 ];
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Extra spaces

Post by Syntac »

PHP's interpolation (in-string parsing) system is stricter than straight code.
User avatar
viraj
Forum Newbie
Posts: 11
Joined: Wed Nov 19, 2008 7:52 am
Location: Colombo - Sri Lanka

Re: Extra spaces

Post by viraj »

thanks Syntac.
jason.carter
Forum Commoner
Posts: 35
Joined: Sat Jan 10, 2009 10:05 am

Re: Extra spaces

Post by jason.carter »

viraj wrote: As you said I wonder why this throw an error

Code: Select all

echo "Value of index 1: $some_array[ 1 ]";
and this is not

Code: Select all

echo "Value of index 1: " . $some_array[ 1 ];

That throws an error as " is for text (as you know).

If you want to try and use that you can always attempt

Code: Select all

echo "Value of index 1: {$some_array[ 1 ]}";
Cover the variable with a curly bracket { }
User avatar
viraj
Forum Newbie
Posts: 11
Joined: Wed Nov 19, 2008 7:52 am
Location: Colombo - Sri Lanka

Re: Extra spaces

Post by viraj »

Thanks Carter,

I think its because whatever inside the curly bracket { } will execute first.

http://www.php.net/language.variables.variable
Post Reply