Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Mar 2012
    Location
    Somewhere over the Rainbow
    Posts
    96
    Thanks
    7
    Thanked 5 Times in 5 Posts

    Question Opinion? Short Variable Names

    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.

    Thanks!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.
    PHP Code:
    $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.

  • Users who have thanked Fou-Lu for this post:

    achira (04-17-2012)

  • #3
    New Coder
    Join Date
    Mar 2012
    Location
    Somewhere over the Rainbow
    Posts
    96
    Thanks
    7
    Thanked 5 Times in 5 Posts
    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?!!

    Thank you!

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.

  • #5
    Regular Coder primefalcon's Avatar
    Join Date
    Aug 2008
    Location
    /home/primefalcon/
    Posts
    678
    Thanks
    8
    Thanked 39 Times in 39 Posts
    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

  • #6
    New Coder
    Join Date
    Mar 2012
    Location
    Somewhere over the Rainbow
    Posts
    96
    Thanks
    7
    Thanked 5 Times in 5 Posts
    I decided to go with names, no spaces/ands.

    some of them:

    PHP Code:
    $impactassessment=$_POST['Impact Assessment'];
      
    $humanresources=$_POST['Human Resources'];
      
    $itservices=$_POST['IT Services'];
      
    $finance=$_POST['Finance'];
      
    $emergencyresponse=$_POST['Emergency Response'];
      
    $training=$_POST['Training'];
      
    $researchdevelopment=$_POST['Research and Development'];
      
    $manufacturing=$_POST['Manufacturing and Sales'];
      
    $distribution=$_POST['Distribution and Sales'];
      
    $healthaspects=$_POST['Health Aspects'];
      
    $orgname=$_POST['Organization Name'];
      
    $firstname=$_POST['First Name'];
      
    $lastname=$_POST['Last Name'];
      
    $phonenumber=$_POST['Phone Number'];
      
    $emailaddress=$_POST['Email Address'];
      
    $website=$_POST['Website'];
      
    $address1=$_POST['Address1'];
      
    $address2=$_POST['Address2'];
      
    $city=$_POST['City'];
      
    $state=$_POST['State'];
      
    $zipcode=$_POST['Zip Code'];
      
    $missionstatement=$_POST['Mission Statement'];
      
    $corporationl=$_POST['Corporation Large'];
      
    $corporationm=$_POST['Corporation Medium'];
      
    $corporations=$_POST['Corporation Small'];
      
    $ngol=$_POST['NGO Large'];
      
    $ngom=$_POST['NGO Medium'];
      
    $ngos=$_POST['NGO Small'];
      
    $ngoadvocacyl=$_POST['NGO Advocacy Large'];
      
    $ngoadvocacym=$_POST['NGO Advocacy Medium'];
      
    $ngoadvocacys=$_POST['NGO Advocacy Small']; 

  • #7
    New Coder
    Join Date
    Mar 2012
    Location
    Somewhere over the Rainbow
    Posts
    96
    Thanks
    7
    Thanked 5 Times in 5 Posts
    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?

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.
    PHP Code:
    // 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.

  • #9
    New Coder
    Join Date
    Mar 2012
    Location
    Somewhere over the Rainbow
    Posts
    96
    Thanks
    7
    Thanked 5 Times in 5 Posts
    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.

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.

    Edit:
    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().

  • #11
    New Coder
    Join Date
    Mar 2012
    Location
    Somewhere over the Rainbow
    Posts
    96
    Thanks
    7
    Thanked 5 Times in 5 Posts
    Ahh okay.

    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •