View Full Version : Opinion? Short Variable Names
04-17-2012, 03:50 PM
Is there a general consensus on using a combination of numbers for short variable names in php code; if so what?
I'm asking because I'm doing some insert php code for a form and I'm starting to think that I'd probably have better luck remembering numbers as opposed to these "short" variable names.
04-17-2012, 03:58 PM
You cannot use a variable name of just numbers. This is legal only when you've hacked up the stdclass object by exploiting the auto-member create, or when using pcre backreferences.
Sounds to me like you want to use an array. PHP doesn't have real arrays, but it uses hashtables which can use auto-incrementing numeric keys. Since arrays are traversable, you can use them in foreach loops, or use more traditional for or [do/]while loops.
Don't forget that during any type of iteration, if you don't need to remember the specifics of an iterated value you may reuse any variable within the loop.
$j = 1;
for ($i = 1; $i <= 10; ++$i)
$j *= $i; // $j has been overwritten here
printf('$j * $i = %d', $j);
Since we don't care what $j is here, it doesn't matter that we overwrite it during each iteration. $i is also overwritten, but that is normal since its the control for the for loop.
04-17-2012, 04:10 PM
Definitely helpful information but I think a bit too advanced for me at this point.
*files away for future use*
However, it's good to know that I need to stick with letter combinations. I think I will just do the full word because I don't want anyone else working on this in the future to be like WTF WHY DID SHE CHOOSE THESE AS VARIABLE NAMES?!!
04-17-2012, 04:16 PM
If you give more information we can probably give a better route of doing it.
As for names, you don't need to use letters, but a variable cannot start with a number. You have to start with a letter or underscore, so $_1 is valid.
PHP also has variable variables. Limited use IMO since you can do the exact same with the use of a temporary assignment.
04-17-2012, 04:23 PM
look as someone who has maintained a lot of php code, forget about using short variable names...
use descriptive ones... for example instead of using $pn actually do use something like $pageNumber, it makes updating the page in the future easier since you'll remember that exactly that variable is for when you need to updat the code in 1, 2 or maybe even 3 years time
04-17-2012, 04:28 PM
I decided to go with names, no spaces/ands.
some of them:
$researchdevelopment=$_POST['Research and Development'];
$manufacturing=$_POST['Manufacturing and Sales'];
$distribution=$_POST['Distribution and Sales'];
$ngoadvocacyl=$_POST['NGO Advocacy Large'];
$ngoadvocacym=$_POST['NGO Advocacy Medium'];
$ngoadvocacys=$_POST['NGO Advocacy Small'];
04-17-2012, 04:30 PM
Oh one more thing- everything I've been reading is about how to return it as an error and "you must fill out this value" if someone leaves an area blank- but how do I just indicate a null value in the table instead, in cases where I don't care if they've given me a value or not?
For those columns I've made them null as the default, is that enough?
04-17-2012, 04:41 PM
The easiest way is to simply iterate or filter that array down. The problem is that checkboxes in particular are only sent if one is checked, so you need to know which fields are important separately from the actual array.
// These must match the html input names.
$aRequiredFields = array(
'Impact Assessment', 'Human Resources', 'IT Services', 'Finance', '.........'
// There are a number of ways to now continue. I'll use just an iterative approach since its easiest to read
$aIncomplete = array();
foreach ($aRequiredFields AS $req)
if (!isset($_POST[$req]) || empty($_POST['req']))
$aIncomplete = $req;
printf("The following fields must be provided and were left empty: %s", implode(", ", $aIncomplete));
You can print out each empty one separately as well.
04-17-2012, 04:47 PM
I had considered that but I thought I wasn't supposed to use isset?
I think my biggest hurdle with check boxes is figuring out how to have one check box feed two different tables.
04-17-2012, 04:52 PM
Isset would be required to prevent errors. And as mentioned, checkboxes are only sent if one or more is checked.
Checkbox naming is simply using chkName to select multiple within the HTML name attribute. How its split up visually in a single form isn't relevant, but it must be done in a single form.
Actually, I'll need to double check but I believe nowadays that empty() also runs an implicit isset check. So if a variable does not exist, it will not trigger an error on an empty check, so you can probably just get away with the empty().
04-17-2012, 04:58 PM
I've seen that in reference to deleting and now it makes sense to be able to alter them to update as well. I might have to think about it for a few minutes though.
Thank you :)
Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.