|
Posted by Jerry Stuckle on 11/18/59 11:45
Oli Filth wrote:
> Jerry Stuckle wrote:
>
>>Oli Filth wrote:
>>
>>>Oli Filth said the following on 09/04/2006 13:44:
>>>
>>>>Jerry Stuckle said the following on 09/04/2006 06:14:
>>>>
>>>>>Oli Filth wrote:
>>>>>
>>>>>
>>>>>> <?php
>>>>>>
>>>>>> function getValueWithDefault(&$array, $key, $default = NULL)
>>>>>> {
>>>>>> return (isset($array[$key])) ? $array[$key] : $default;
>>>>>> }
>>>>>>
>>>>>> $var = array();
>>>>>> echo getValueWithDefault($var, "Roger", "Dodger") . "\n";
>>>>>>
>>>>>> ?>
>>>>>
>>>>>Take out the $var=array() line and see what you get.
>>>>
>>>>But anyway, removing the $var = array() line does not result in any
>>>>error/warning. Because it's being passed by reference, PHP
>>>>automatically creates a variable $var in the global scope
>>>
>>>That should be *caller* scope...
>>
>>OK, you want to argue with everyone who supports the idea of macros. Fine.
>
>
> Woah, easy there tiger! Did I at any point say, or even imply, that
> macros were a bad idea? No.
>
> All I've done so far is correct your claim that one couldn't do the
> getValueWithDefault thing without a macro.
>
And as I've said before - you can't do it without multiple different functions.
> I'm a C/C++ programmer at heart, I use macros there, and I know the
> benefits (and otherwise) of them.
>
>
They why do you spend so much effort arguing AGAINST macros?
>
>>Let's see you to this as easily and as cleanly without macros:
>>
>> define('FOREACH(X, K, V)', 'if (isset(X) && isarray(X)) foreach(X as K=>V)
>>
>>
>> FOREACH($var,$key,$value) {
>>
>> // Process data in $key=>$value here.
>> }
>>
>> FOREACH($var2, $key2, $value2) {
>> // Different processing of $key2=>value2 here
>> }
>
>
> Again, that just implies bad practice. You should always know whether
> a *variable* exists, and as such, should have no reason to call isset()
> on it.
>
>
Not necessarily. It could be a list of checkboxes in your $_POST array, for
instance. It could be something in the $_SESSION array which may or may not
have been set.
Contrary to your belief, it is ALWAYS better to validate the existence of such
variables.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
[Back to original message]
|