ifsetor(), goto, 5.x, my two cents

php.internals

Noah Botimer

21 years ago
Hello all, Now that my PHP-DEV imap folder has cooled off a bit, I'd like to chime in briefly on ifsetor() and goto. As far as ifsetor() goes, I like the concept. I would, however, suggest a specific behavior and a name change. I do a lot of database code and use things like ISNULL() and COALESCE() to translate NULL values to 0's, placeholder strings, etc. ISNULL() typically provides a check of one value, and acts just as a ternary operation: value = check ? check : alternative; while COALESCE() usually returns the first non-null parameter, or NULL if all are parameters are NULL. Since PHP already supports arbitrary/optional parameters natively, I think a single coalesce() function would be a very reasonable extension that would behave in an understandable and desirable manner (with a name that matches at least some common usage). As for goto, we may be fighting over nothing. There is a lot of abuse that may be done with goto, as has been illustrated but, with a couple of sensible limitations, the horrific problems should be averted. It also seems to me that GOTO is far less often mentioned to beginners as a means of flow control since line-numbered BASIC has faded. People who get into trouble with it will likely have a good reason to be playing with fire. I say: put it in, label it dangerous, and let the developers decide if it's right for them. Thanks, -Noah Botimer

Benj Carson

21 years ago
On June 8, 2005 04:20 pm, Noah Botimer wrote:
> > Since PHP already supports arbitrary/optional parameters natively, I > think a single coalesce() function would be a very reasonable > extension that would behave in an understandable and desirable manner > (with a name that matches at least some common usage). >
Not that my opinion counts for anything--I'm just a lowly user, though I do have a T-shirt that claims I'm a 'PHP 5 Pioneer'--but I could not agree with you more on both counts. Many PHP developers are also SQL developers, so I think COALESCE() would be an excellent implementation of the idea behind ifsetor() with a name that, as you mention, has at least some overlap with concepts that should be familiar to many PHP developers. I believe this name was also floated the first time ifsetor() was introduced here.
> As for goto, we may be fighting over nothing. There is a lot of > abuse that may be done with goto, as has been illustrated but, with a > couple of sensible limitations, the horrific problems should be > averted. It also seems to me that GOTO is far less often mentioned > to beginners as a means of flow control since line-numbered BASIC has > faded. People who get into trouble with it will likely have a good > reason to be playing with fire. I say: put it in, label it > dangerous, and let the developers decide if it's right for them.
I've also wished at times that PHP had goto, and I think it would be a useful addition to the language. There are times when error handling *within* a function would be better served with a simple goto (or named break) than with an exception (e.g. if there is no need for an error to bubble up outside of a function, but the function still needs to clean up after itself). While PHP may 'not be the right language' for parsers, adding goto could perhaps change that. PHP's text handling is superb, but there are times when PCRE just won't cut it and a state machine is needed. Why avoid new territory and curtail the potential usefulness of the language? No need to make parsing the primary focus, but in the same way that the cli sapi was added with 4.3 in order to expand the usage of PHP outside of web environments, goto could further that goal. If the concern regarding goto is the potential for spaghetti, then I think that can be addressed with solid documentation. If big fat warnings are prominently displayed alongside good examples of proper usage, then most users will get the idea. Plus, given the length of the debate here on internals, I would imagine that there will be a generous helping of user comments to support proper usage idioms as well. Again, just my two cents, but as a developer, both of these constructs would be very useful, and I think they will only increase the utility of PHP. Benj Carson

Ron Korving

21 years ago
I must say, I like the coalesce() idea a lot. It gives more flexibility over ifsetor() which sounds to me like it only handles 1 variable that is or isn't set. coalesce() would handle any number of variables. Here's something else to consider though: Would anybody be interested in a parameter for ifsetor() or coalesce() that would use !empty() instead of isset() ? If coalesce() would become the function name of choice, you can't drop in a parameter to achieve what I just said, so an alternative function would be better in that case (or maybe it would be better in any case). I know I'd love to see some variant like this. I use empty() a lot more than isset(). Ron "Noah Botimer" <noah@botimer.net> wrote in message news:ECDCEE8D-2160-46B7-BF84-AB163E477707@botimer.net...

Noah Botimer

21 years ago
Ron, I was thinking about behavioral variants but left them out to keep my message shorter. I haven't really thought of a clean way to use one named function to provide both types of functionality. I was originally thinking of a constant parameter to indicate a "mode" but with the arbitrary length parameter list, it would have to come at the beginning, removing the possibility of optionality. For understandability of code, it might make sense to implement two functions with the same behavior and similar names, but a different underlying call, so the intent is obvious upon reading. Another possibility could be a variant where the first parameter would be a bool-returning callback and the return would be the first parameter that, when passed to that callback, returns a true (or non- zero/non-null) value. As a final note, I would say that both isset() and !empty() are valuable and to neglect either one for syntactical sugar-coating might be an oversight. Both are different and have applications to the concept, so both should supported in some fashion. Thanks, -Noah Botimer On Jun 9, 2005, at 2:28 AM, Ron Korving wrote:

Taco van den Broek

21 years ago
It will be difficult to define the check for such a coalesce function that everybody agrees on. Personnally I like the !empty test, but would want it to check on != 0 too. Most probably my favourite check would be something like: !empty($foo) || (string)$foo !== '' Someone else might want this function to check only with is_null or isset or any combination of is_null, isset, empty and (string)$foo !== ''. I do hope this kind of functionallity is added to PHP at some time but I'm pretty sceptical about the way such a function would behave. Ron Korving wrote: