Jump to content

[SOLVED] Variable Declaration help


Richter12x2

Recommended Posts

First of all, I wouldn't officially consider myself a programmer so much as a code hacker - I can usually look at how code is put together and change it to get the results I want, but I couldn't start and write a program from scratch.  I just know that with a little understanding I can change my ignorant ways though. :D

 

I'm working through the code in a pre-packaged software solution, and it wouldn't get the mail address correct.  I looked up the syntax for  the mail command, and it helped me to get a CC: as a workaround, but I eventually tracked the main problem down to these lines of code.

	$fname=$recUser[0]['f_name'];
	$to_email=$recUser['user_name'];

 

It's declaring two variables that are ultimately pulled from the same SQL table, one has the [0] and the other does not.  I added the [0] to the 'user_name' field (which is where it gets the To for the e-mail) and it works fine.  When I read through the PHP manual on this site pertaining to declaring variables, I didn't see anything referencing the [0] portion. 

 

What's the significance of the [0]?  It doesn't declare it as a set value, because it's immediately picking up the correct value from the table once I add it.  Without the 0, the To: field of the e-mail stays blank, and $to_email=''. 

 

In short, my code is fixed, but I'm just trying to bridge the gap from knowing the answer to understanding the answer. :D 

 

Thanks in advance!

Link to comment
Share on other sites

$recUser is a multidimensional array. The first dimension has keys as numbers (I assume it has more than just 0), the second one then has the user_name and f_name. There is no way for me to tell if there is a key called user_name in the first dimension, but it is possible, not likely, though. It was probably just a typo by whoever wrote it.

 

If you look here, the first example uses a two-dimensional array: http://us3.php.net/manual/en/function.array.php

Link to comment
Share on other sites

Square brackets appended to a variable name signal that variable as an array.

 

For example:

<?php
  $var = "Hello, World!"; // Declares a normal variable
  $var = Array(); // Declares an empty array
?>

 

Each "level" of square brackets indicates a new dimension of the array.  If you see $var[] that is an array with a single dimension; you can conceptualize this as a stack of items (like plates).  If you see $var[][], this is an array with two dimensions; you can conceptualize it as an x-y graph, grid, chart, or anything with columns and rows.  Arrays can have any number of dimensions.

 

You use an index into a dimension to access a particular value out of it.  For example, let's say there is a single dimensioned array with 10 items in it.

<?php
  // dimensions start counting at zero
  echo $single[0]; // prints the first item
  echo $single[1]; // prints second item
  echo $single[2]; // third item
  // ...
  echo $single[9]; // prints 10th, aka last, item
?>

 

Using the same concept with a double dimensioned array, it is common for one index to represent columns and the other rows, although the order is not important (All that is important is that you use it consistently throughout your program).

<?php
  // Assume the dimensions are $double[rows][columns]
  echo $double[0][5]; // Row 1, Column 6
  echo $double[4][9]; // Row 5, column 10

  // Now switch the dimensions: $double[columns][rows]
  echo $double[0][5]; // Row 6, column 1
  echo $double[4][9]; // Row 10, column 5
?>

 

Creating arrays is easy:

<?php
  $arr = Array();
  $arr[] = "First"; // [] means 'append to the array this value'
  $arr[] = "Second";
  $arr[] = "Third";
  // $arr now holds 3 values, $arr[0] holds the value "First"
  // $arr[1] holds the value "Second" and so on

  // We can declare the whole thing in one go
  $arr = Array( "First", "Second", "Third" );
  // Above, PHP is very kind and automatically creates the array and inserts
  // the items in that order
?>

 

So far, all of these arrays are numerically index.  This means we place integer values inside of the square brackets to grab items.  You may have wondered why we bother with loops in programs; combined with arrays they are very powerful.  Consider the following:

<?php
  // The following is a trivial example, but let's pretend you have a collection of items
  // you have to manipulate.
  $var1 = "Hello";
  $var2 = "I know";
  $var3 = "This is stupid";
  echo $var1;
  echo $var2;
  echo $var3;

  // Above, combining the echo statements into a single statement is trivial to do.  But
  // what if you aren't echo'ing the values?  What if you're performing complex operations
  // on each item.  You don't really want to repeat that code do you?

  // use an array and a loop
  $arr = Array( "val1", "val2", "val3" );
  foreach($arr as $value){
    echo $value;
  }
?>

 

Note that you can use any type of loop to loop over an array:

<?php
  $arr = Array( "val1", "val2", "val3" );
  
  $number_of_items_in_array = count($arr); // Will hold 3

  // Using a for loop
  for($i = 0; $i < $number_of_items_in_array; $i++){
    echo $arr[$i];
  }

  // Using a while
  $i = 0;
  while($i++ < $number_of_items_in_array){
    echo $arr[$i];
  }

  // Using a do while
  $i = 0;
  do{
    echo $arr[$i++];
  }while($i < $number_of_items_in_array)
?>

 

Up until now, all of these arrays are numerically indexed, which means we use integer values between the square brackets to get individual values.  Arrays can be associatively indexed as well, which means we use strings instead of integers to get at the values.

<?php
  $arr = Array();
  $arr["Bob"] = "Smith";
  $arr["Mary"] = "Jones";
  echo $arr["Bob"];
  echo $arr["Mary"];
?>

 

The last thing you need to know is foreach, which I used earlier without explaining.  foreach loops over each item in an array and stores it within a variable.  You can tell foreach to store the index in a variable as well.

<?php
  foreach($array as $value){
    // Each item in the array will be stored in $value
  }

  foreach($array as $index => $value){
    // Each index will be stored in $index, be it numeric or associative
    // Each value will be stored in $value
  }
?>

 

This should get you started; if you need further assistance I'd read the PHP manual.

Link to comment
Share on other sites

Aha, I see - that would explain why this guy had data from two seperate databases going into the same variable.  He had a second To: coming in after the first that would send a copy to the site administrator to notify that there was a new customer - the end result is that it killed out the person to whom the message was originally addressed.  I could probably change it back now, but I just added a 2 to the end of that variable and called it as a CC: header later on. 

 

Thanks for the help guys!

 

By the way Roopert, that was a pretty awesome explanation of arrays - I remember learning a little bit about them back in my Visual Basic days, but getting confused.  Your explanation helped a lot.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.