Re: Passing an undetermined amount of arguments by reference

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jim Lucas wrote:
> Jay Moore wrote:
>> Shawn McKenzie wrote:
>>> Jay Moore wrote:
>>>> Jim Lucas wrote:
>>>>> Jay Moore wrote:
>>>>>> Greetings list.
>>>>>>
>>>>>> Say I have a function that escapes a string before being passed to
>>>>>> MySQL
>>>>>> like so:
>>>>>>
>>>>>> function escape($id, &$string)
>>>>>> {
>>>>>>     $string }
>>>>> Use an array as an alternate method of sending/returning data to the
>>>>> second argument.
>>>>>
>>>>> function escape($id, &$data) {
>>>>>     if ( is_array($data) ) {
>>>>>         foreach ( $data AS $k => $v ) {
>>>>>             escape($id, $v);
>>>>>             $data[$k] = $v;
>>>>>         }
>>>>>     } else {
>>>>>         $data = mysql_real_escape_string($data, $id);
>>>>>     }
>>>>> }
>>>>>
>>>>> This would handle any number of nested arrays/datasets.
>>>>>
>>>>> Hope it helps.
>>>>>
>>>> Will that work properly?
>>>>
>>>> $a = "'hello'";
>>>> $b = "sup";
>>>> $c = "\\hola'";
>>>>
>>>> $d = array($a, $b, $c);
>>>>
>>>> escape($id, $d);
>>>>
>>>> Jay
>>> I would try: $d = compact('a', 'b', 'c');
>>>
>> What is the difference?  Please excuse my naivety. :)
>>
>> Jay
>>
> 
> Good point.
> 
> http://us2.php.net/compact
> 
> Key phrase...
> 
> 	"it does the opposite of extract()"
> 
> So, using that I would do it like this...
> 
> 
> $a = "'hello'";
> $b = "sup";
> $c = "\\hola'";
> 
> $d = compact('a', 'b', 'c');
> escape($id, $d);
> extract($d);
> 
> with the above changes to the escape() function.
> 

Right, so: $d = array($a, $b, $c); would give you an array like this:

0 => "'hello'"
1 => "sup"
2 => "\\hola'"

Whereas: $d = compact('a', 'b', 'c'); would give you:

'a' => "'hello'"
'b' => "sup"
'c' => "\\hola'"

So the keys are your var names instead of numerical indexes.


Also, I can't test it now, but a shorter though cramped way might be
(assuming that you modify escape() to return instead of modifying the
reference):


extract(escape($id, compact('a', 'b', 'c')));

-- 
Thanks!
-Shawn
http://www.spidean.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux