 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
04-23-2009, 10:54 PM
|
#1 (permalink)
|
|
The Wanderer
Join Date: Aug 2008
Posts: 8
Thanks: 0
|
Return array values
I have a script that creates an array that I want to validate. It creates the following array:
return array(
"first_name" => $vb["firstname"],
"last_name" => $vb["lastname"],
"email" => $vb["emailaddress"]
);
The variables:
$vb["firstname"]
$vb["lastname"]
$vb["emailaddress"]
are correct prior to the return array statement.
Any one know how I can validate/print the values in the newly created array to ensure that they are correct?
|
|
|
|
04-23-2009, 11:33 PM
|
#2 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
Can you do a var_dump on $vb?
|
|
|
|
04-23-2009, 11:48 PM
|
#3 (permalink)
|
|
The Wanderer
Join Date: Aug 2008
Posts: 8
Thanks: 0
|
Quote:
Originally Posted by allworknoplay
Can you do a var_dump on $vb?
|
Prior to the return array statement, I am running:
echo $vb["firstname"];
on each of the variables and it is returning the correct values.
Is there a way to do that with the array after the return array statement is run?
Thanks!
|
|
|
|
04-24-2009, 12:10 AM
|
#4 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
How about like this?
php Code:
function validate_these ($szFirstName, $szLastName, $szEmailAddress){ if (! $szFirstName || ! $szLastName || ! $szEmailAddress) { return false; } if (! preg_match('~.+?@.+?\..+?~i', $szEmailAddress)) { return false; } return true; }$vb[ "firstname"] = 'Adam'; $vb[ "lastname"] = '(Wildhoney)'; $vb[ 'emailaddress'] = 'adam@example.com'; $bValidate = validate_these ($vb[ "firstname"], $vb[ "lastname"], $vb[ "emailaddress"] ); echo $bValidate ? 'These Validate' : 'These Do Not Validate'; echo '<br />'; $vb[ 'emailaddress'] = 'adamexample.com'; $bValidate = validate_these ($vb[ "firstname"], $vb[ "lastname"], $vb[ "emailaddress"] ); echo $bValidate ? 'These Validate' : 'These Do Not Validate'; echo '<br />'; $vb[ 'firstname'] = ''; $vb[ 'emailaddress'] = 'adam@example.com'; $bValidate = validate_these ($vb[ "firstname"], $vb[ "lastname"], $vb[ "emailaddress"] ); echo $bValidate ? 'These Validate' : 'These Do Not Validate'; echo '<br />';
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
04-24-2009, 12:27 AM
|
#5 (permalink)
|
|
The Wanderer
Join Date: Aug 2008
Posts: 8
Thanks: 0
|
Nice, but I'm not sure that is telling what the values are in the new array? Or am I misreading the code.
Remember, I am creating an array with:
Quote:
return array(
"first_name" => $vb["firstname"],
"last_name" => $vb["lastname"],
"email" => $vb["emailaddress"]
);
|
I know the values are correct going in, and I just want to make sure my new array variables ("first_name", "last_name", and "email") have the correct values passed to them.
|
|
|
|
04-24-2009, 12:38 AM
|
#6 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
Quote:
Originally Posted by smrtalex
Nice, but I'm not sure that is telling what the values are in the new array? Or am I misreading the code.
Remember, I am creating an array with:
I know the values are correct going in, and I just want to make sure my new array variables ("first_name", "last_name", and "email") have the correct values passed to them.
|
Ok well say your function is called: myFunction.
You can try this...
list($first_name,$last_name,$email) = myFunction();
echo "$first_name $last_name $email";
|
|
|
|
04-24-2009, 12:49 AM
|
#7 (permalink)
|
|
The Wanderer
Join Date: Aug 2008
Posts: 8
Thanks: 0
|
Please forgive me if I don't know how to utilize your code. Here is the code I am using:
Quote:
function info($user) {
$esc = mysql_real_escape_string($user, $this->res);
$source = $this->source;
$tbl = $source["tableprefix"];
$rs = mysql_query("SELECT * FROM ".$tbl." WHERE customerEmailAddress = '$esc'",$this->res);
$vb = mysql_fetch_array($rs);
return array(
"first_name" => $vb["customerFirstName"],
"last_name" => $vb["customerLastName"],
"email" => $vb["customerEmailAddress"]
);
}
|
Again, I know the values of:
$vb["customerFirstName"]
$vb["customerLastName"]
$vb["customerEmailAddress"]
are correct right before the 'return array' statement. How do I view the contents of ("first_name", "last_name", and "email") after the 'return array' statement to ensure that they have the correct values passed to them? Or where can I input your code to see?
Thanks for your assistance!
|
|
|
|
04-24-2009, 12:57 AM
|
#8 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
Quote:
Originally Posted by smrtalex
Please forgive me if I don't know how to utilize your code. Here is the code I am using:
Again, I know the values of:
$vb["customerFirstName"]
$vb["customerLastName"]
$vb["customerEmailAddress"]
are correct right before the 'return array' statement. How do I view the contents of ("first_name", "last_name", and "email") after the 'return array' statement to ensure that they have the correct values passed to them? Or where can I input your code to see?
Thanks for your assistance!
|
Right, my code above should work, just do this:
list($first_name,$last_name,$email) = info($user);
echo "$first_name : $last_name : $email";
|
|
|
|
04-24-2009, 01:31 AM
|
#9 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
Opposed to returning an array by value, it is faster (although trivial on small systems) to pass by reference. This assigns the array to memory and passes the location of the variable to the script. This code should do it.
PHP Code:
function assignArr(&$param) { param = array("a","b","c"); }
$arr = array(); assignArr($arr);
echo $arr[0] . $arr[1] . $arr[2];
$arr will be modified in memory when you pass by reference.
Since I feel like explaining stuff tonight, I will explain how this works.
There are two ways to pass variables in programming, by reference and by value. By value is the most common and often the best way to go, if you dont know which you are doing you are probably passing by this method. This simply copies the value down to a new part of memory. This allows you to modify this copy without touching the original value.
Passing by reference uses special variables called pointers, if you work with C you are very familiar with this. Since all your variables are stored in the ram, they have an address, these variables do not actually contain the value of the variable but memory addresses. So when you need to access the variables, it points the script to the address in memory. This allows you to efficiently modify outside variables though parameters without violating good scope practices.
PHP tries to keep this fairly transparent to you, so you only need to know how to pass by reference. But this will be an unavoidable part of C or C++ if you start larning it.
|
|
|
|
04-24-2009, 02:05 AM
|
#10 (permalink)
|
|
The Wanderer
Join Date: Aug 2008
Posts: 8
Thanks: 0
|
Quote:
Originally Posted by Village Idiot
Opposed to returning an array by value, it is faster (although trivial on small systems) to pass by reference. This assigns the array to memory and passes the location of the variable to the script. This code should do it.
PHP Code:
function assignArr(&$param)
{
param = array("a","b","c");
}
$arr = array();
assignArr($arr);
echo $arr[0] . $arr[1] . $arr[2];
$arr will be modified in memory when you pass by reference.
Since I feel like explaining stuff tonight, I will explain how this works.
There are two ways to pass variables in programming, by reference and by value. By value is the most common and often the best way to go, if you dont know which you are doing you are probably passing by this method. This simply copies the value down to a new part of memory. This allows you to modify this copy without touching the original value.
Passing by reference uses special variables called pointers, if you work with C you are very familiar with this. Since all your variables are stored in the ram, they have an address, these variables do not actually contain the value of the variable but memory addresses. So when you need to access the variables, it points the script to the address in memory. This allows you to efficiently modify outside variables though parameters without violating good scope practices.
PHP tries to keep this fairly transparent to you, so you only need to know how to pass by reference. But this will be an unavoidable part of C or C++ if you start larning it.
|
Wow. Thanks! I have tried allworknoplay's suggestion to no avail (meaning it is not returning any values). And I'm not quite sure where to put your code and how to change it to work with my script. Can you help me there? 
|
|
|
|
04-24-2009, 02:15 AM
|
#11 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
Quote:
Originally Posted by smrtalex
Wow. Thanks! I have tried allworknoplay's suggestion to no avail (meaning it is not returning any values). And I'm not quite sure where to put your code and how to change it to work with my script. Can you help me there? 
|
I got it to work this way...
Code:
<?
function info($user) {
//simulate successful DB results
$vb["customerFirstName"] = "John";
$vb["customerLastName"] = "Smith";
$vb["customerEmailAddress"] = "none@none.com";
return array(
$vb["customerFirstName"],
$vb["customerLastName"],
$vb["customerEmailAddress"]
);
}
$user = "john";
list($first_name, $last_name, $email) = info($user);
echo "$first_name, $last_name, $email";
?>
I get the output:
John, Smith, none@none.com
|
|
|
|
04-24-2009, 04:24 AM
|
#12 (permalink)
|
|
The Wanderer
Join Date: Aug 2008
Posts: 8
Thanks: 0
|
Thanks! I think that might work if that is all that is running in the script. :(
Quote:
function info($user) {
$esc = mysql_real_escape_string($user, $this->res);
$source = $this->source;
$tbl = $source["tableprefix"];
$rs = mysql_query("SELECT * FROM ".$tbl." WHERE customerEmailAddress = '$esc'",$this->res);
$vb = mysql_fetch_array($rs);
echo $vb["customerFirstName"];
return array(
"first_name" => $vb["customerFirstName"],
"last_name" => $vb["customerLastName"],
"email" => $vb["customerEmailAddress"]
);
echo $arr["first_name"];
}
|
The line:
Quote:
|
echo $vb["customerFirstName"];
|
works right before the 'return array' statment. I'm just not understanding why this line:
won't show the variable. It is almost like my array is not being setup.
|
|
|
|
04-24-2009, 04:42 AM
|
#13 (permalink)
|
|
The Wanderer
Join Date: Aug 2008
Posts: 8
Thanks: 0
|
It has to be in my 'return' command. This works:
Quote:
function info($user) {
$esc = mysql_real_escape_string($user, $this->res);
$source = $this->source;
$tbl = $source["tableprefix"];
$rs = mysql_query("SELECT * FROM ".$tbl." WHERE customerEmailAddress = '$esc'",$this->res);
$vb = mysql_fetch_array($rs);
$a = array(
"first_name" => $vb["customerFirstName"],
"last_name" => $vb["customerLastName"],
"email" => $vb["customerEmailAddress"]
);
echo $a["first_name"];
}
|
Now I'm really lost.  Anyone have any thoughts on the 'return' statement?
|
|
|
|
04-24-2009, 02:58 PM
|
#14 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
Quote:
Originally Posted by smrtalex
It has to be in my 'return' command. This works:
Now I'm really lost.  Anyone have any thoughts on the 'return' statement?
|
I don't understand why you have to assign the variables in a key/value pair? You can just return the values..
This works..
Code:
<?
function info($user) {
//simulate successful DB results
$vb["customerFirstName"] = "John";
$vb["customerLastName"] = "Smith";
$vb["customerEmailAddress"] = "none@none.com";
return (array($vb["customerFirstName"], $vb["customerLastName"], $vb["customerEmailAddress"]) );
}
$user = "john";
list($first_name, $last_name, $email) = info($user);
echo "$first_name, $last_name, $email";
?>
Just return the values.
Then the list() function takes each item in the array and assigns them accordingly as first name, last name and email respectively.
You can then print them out....
|
|
|
|
04-25-2009, 11:00 PM
|
#15 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
Quote:
Originally Posted by smrtalex
Wow. Thanks! I have tried allworknoplay's suggestion to no avail (meaning it is not returning any values). And I'm not quite sure where to put your code and how to change it to work with my script. Can you help me there? 
|
You make the variable, run the function with said variable in the parameter and you are done.
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|