Page 1 of 1
$my_row[blah] or $a_row[blah] or $row[blah] Which one?
Posted: Tue Oct 08, 2002 10:13 am
by flash
Just curious, I'm guessing one could use $a_bannana[blah]? How do you prefer to proceed? Why the underscore?
Re: $my_row[blah] or $a_row[blah] or $row[blah] Which one?
Posted: Tue Oct 08, 2002 10:38 am
by rev
flash wrote:Just curious, I'm guessing one could use $a_bannana[blah]? How do you prefer to proceed? Why the underscore?
The answer in short to your last question - the underscore represents a space where a literal space is an invalid character for a variable name to contain.
http://www.php.net/manual/en/language.variables.php
As for my preference, I tend to name all of my classes and functions using the following format: mySpecialClass or mySpecialFunction() -- Java influence I guess. I tend to use underscores in all variable names unless I am instantiating a class, then I tend to use a uniform variable string compared to the class name. e.g.
Code: Select all
<?php
$mySpecialClass = new MySpecialClass;
?>
For me, this just helps keep things straight in my head per what I am looking at within the script.
Posted: Tue Oct 08, 2002 11:06 am
by twigletmac
Whichever one you choose make sure that you quote your array's element names so $array['element'] not $array[element], not quoting them means that PHP does a bunch of extra work looking for a constant (in this example called element) which doesn't exist and then when it doesn't find it it assumes you meant 'element' not element.
Mac
Posted: Tue Oct 08, 2002 1:44 pm
by f1nutter
Mac, great tip.
I've wondered whats best. I thought that as we are dealing with arrays, $array[element] was best without the quotes, like C++ array[3], y'know an index rather than a string.
Time to search all my files and do some replacing, should save a load of server processing time.
PS. I use $row[blah], thats what the example said and seems to make perfect sense to me, like picking a row from the table.
Posted: Tue Oct 08, 2002 2:21 pm
by twigletmac
Forgot to say before, when you're using a numberical index, eg $array[0], $array[1] etc, you don't need to do any quoting.
Mac
Posted: Tue Oct 08, 2002 2:25 pm
by volka
php-arrays are a mixtures of perl-hashes and perl-arrays (where perl-arrays are more like those you know from C/C++)
you can use what ever you want as index (I think even ressources like database-connections) as long as you could assign it to a variable as well (and of course: could use an equal-comparison).
$a = element; would cause a parse-error (as long as element is not defined as constant i.e.) but you could do $a = 'element';
Since you can assign a numeric value without quotes ($i = 3) $arr[3] = ...; is correct