$my_row[blah] or $a_row[blah] or $row[blah] Which one?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
flash
Forum Newbie
Posts: 1
Joined: Tue Oct 08, 2002 10:13 am
Location: Cambria, CA ~ Where the Pines Meet the Sea
Contact:

$my_row[blah] or $a_row[blah] or $row[blah] Which one?

Post by flash »

Just curious, I'm guessing one could use $a_bannana[blah]? How do you prefer to proceed? Why the underscore?
rev
Forum Commoner
Posts: 52
Joined: Wed Oct 02, 2002 3:58 pm
Location: Atlanta, GA

Re: $my_row[blah] or $a_row[blah] or $row[blah] Which one?

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
f1nutter
Forum Contributor
Posts: 125
Joined: Wed Jun 05, 2002 12:08 pm
Location: London

Post by f1nutter »

Mac, great tip. :idea:

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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
Post Reply