Page 1 of 1

Extra spaces

Posted: Wed Nov 19, 2008 9:02 am
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

Re: Extra spaces

Posted: Wed Nov 19, 2008 11:43 am
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

Re: Extra spaces

Posted: Wed Nov 19, 2008 2:10 pm
by califdon
I agree with Andy, but don't be afraid to try these things. PHP won't bite you!

Re: Extra spaces

Posted: Wed Nov 19, 2008 6:17 pm
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.

Re: Extra spaces

Posted: Thu Nov 20, 2008 3:14 am
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 ];

Re: Extra spaces

Posted: Thu Nov 20, 2008 3:03 pm
by Syntac
PHP's interpolation (in-string parsing) system is stricter than straight code.

Re: Extra spaces

Posted: Fri Nov 28, 2008 6:40 am
by viraj
thanks Syntac.

Re: Extra spaces

Posted: Sat Jan 10, 2009 2:37 pm
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 { }

Re: Extra spaces

Posted: Sat Jan 10, 2009 8:06 pm
by viraj
Thanks Carter,

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

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