TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-23-2009, 10:54 PM   #1 (permalink)
The Wanderer
 
Join Date: Aug 2008
Posts: 8
Thanks: 0
smrtalex is on a distinguished road
Default 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?
smrtalex is offline  
Reply With Quote
Old 04-23-2009, 11:33 PM   #2 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Can you do a var_dump on $vb?
allworknoplay is offline  
Reply With Quote
Old 04-23-2009, 11:48 PM   #3 (permalink)
The Wanderer
 
Join Date: Aug 2008
Posts: 8
Thanks: 0
smrtalex is on a distinguished road
Default

Quote:
Originally Posted by allworknoplay View Post
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!
smrtalex is offline  
Reply With Quote
Old 04-24-2009, 12:10 AM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 04-24-2009, 12:27 AM   #5 (permalink)
The Wanderer
 
Join Date: Aug 2008
Posts: 8
Thanks: 0
smrtalex is on a distinguished road
Default

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.
smrtalex is offline  
Reply With Quote
Old 04-24-2009, 12:38 AM   #6 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by smrtalex View Post
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";
allworknoplay is offline  
Reply With Quote
Old 04-24-2009, 12:49 AM   #7 (permalink)
The Wanderer
 
Join Date: Aug 2008
Posts: 8
Thanks: 0
smrtalex is on a distinguished road
Default

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!
smrtalex is offline  
Reply With Quote
Old 04-24-2009, 12:57 AM   #8 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by smrtalex View Post
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";
allworknoplay is offline  
Reply With Quote
Old 04-24-2009, 01:31 AM   #9 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

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.
__________________

Village Idiot is offline  
Reply With Quote
Old 04-24-2009, 02:05 AM   #10 (permalink)
The Wanderer
 
Join Date: Aug 2008
Posts: 8
Thanks: 0
smrtalex is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
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?
smrtalex is offline  
Reply With Quote
Old 04-24-2009, 02:15 AM   #11 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by smrtalex View Post
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
allworknoplay is offline  
Reply With Quote
Old 04-24-2009, 04:24 AM   #12 (permalink)
The Wanderer
 
Join Date: Aug 2008
Posts: 8
Thanks: 0
smrtalex is on a distinguished road
Default

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:
Quote:
echo $arr["first_name"];
won't show the variable. It is almost like my array is not being setup.
smrtalex is offline  
Reply With Quote
Old 04-24-2009, 04:42 AM   #13 (permalink)
The Wanderer
 
Join Date: Aug 2008
Posts: 8
Thanks: 0
smrtalex is on a distinguished road
Default

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?
smrtalex is offline  
Reply With Quote
Old 04-24-2009, 02:58 PM   #14 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by smrtalex View Post
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....
allworknoplay is offline  
Reply With Quote
Old 04-25-2009, 11:00 PM   #15 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by smrtalex View Post
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.
__________________

Village Idiot is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Next class project? allworknoplay The Lounge 6 04-18-2009 08:33 PM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM
Array mess Killswitch Absolute Beginners 4 12-14-2008 07:35 AM
array elements into variables and values Dave Absolute Beginners 7 06-20-2008 01:56 PM
Part 1: Getting Started with Array Functions Wildhoney Absolute Beginners 6 10-01-2007 10:53 AM


All times are GMT. The time now is 03:55 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design