You are here: Re: Macros in php « PHP Programming Language « IT news, forums, messages
Re: Macros in php

Posted by Jerry Stuckle on 11/18/44 11:45

Oli Filth wrote:
> Jerry Stuckle wrote:
>
>>Oli Filth wrote:
>>
>>>Jerry Stuckle said the following on 09/04/2006 06:14:
>>>
>>>
>>>>Oli Filth wrote:
>>>>
>>>>
>>>>>I have error level set to E_ALL | E_STRICT. The following code
>>>>>executes fine:
>>>>> <?php
>>>>>
>>>>> function getValueWithDefault(&$array, $key, $default = NULL)
>>>>> {
>>>>> return (isset($array[$key])) ? $array[$key] : $default;
>>>>> }
>>>>>
>>>>> $var = array();
>>>>> echo getValueWithDefault($var, "Roger", "Dodger") . "\n";
>>>>>
>>>>> ?>
>>>>>
>>>>>I see no reason why an error/warning should get thrown. isset()
>>>>>simply looks for an element with the specified key name in the
>>>>>associative array. In an empty array, the key doesn't exist, so
>>>>>isset() returns false.
>>>>>
>>>>
>>>>That's not what I said. I said if $var is EMPTY.
>>>
>>>
>>> From the PHP manual for empty():
>>>
>>> "The following things are considered to be empty:
>>> ...
>>> array() (an empty array)
>>> ..."
>>>
>>
>>Yes, the array is empty. But $var is not! It contains an empty array.
>
>
> No, $var doesn't *contain* an empty array, $var *is* an empty array.
>

That's YOUR definition.

Most of us understand there is a difference between an empty array and an empty
variable.

$var = null is an empty variable.
$var = array() contains an empty array.


> <?php
> $var = array();
> echo empty($var) ? "TRUE" : "FALSE"; // echoes "TRUE"
> ?>
>
>
>>>>Take out the $var=array() line and see what you get.
>>>
>>>So you mean "undefined"? Going back to the original purpose of this
>>>function/macro, why would you ever want to call it on an undefined array?
>>>
>>
>>Because it may or may not exist, that's why.
>
>
> I thought the purpose was to check for the existence of an *entry* in
> an associative array, such as $_POST, etc.
>

Yes, and first you have to ensure the array itself exists.

> On the other hand, checking for the existence of a *variable* implies
> bad practice. There should be no need. You should always know a
> priori what variables exist at any given point in your code (assuming
> the wonders of register_globals are disabled).
>

You really don't understand PHP, do you?

Checking for the existence of a *variable* is required for good programming
practice in PHP. You never know what may have been passed in a GET or POST
operation, for instance. And you don't necessarily know what may have been
saved in a cookie or the session.

Checking to see if these exist is very important in PHP programming!

>
>
>>>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 (because for
>>>all it knows, this could be an output argument for the function).
>>
>>Yep, and now you have changed the variable, haven't you?
>
>
> Only if it was undefined in the first place, which would be bad
> practice.
>

No, it could be perfectly normal. What if the variable you're trying to check
is $_POST['cblist'], which itself may be an array of checkbox values?

If no checkboxes are checked, the array doesn't even exist in $_POST.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация