Re: [PHP-DEV] switch, equality and preg_match
If you look back to the original problem, switch was matching the 0 returned from preg_match to a string. However, if the return value is a boolean it would not match 0 to a string. However, I guess it would then match the true to a string. Brian Moon -- dealnews.com, Inc. Makers of dealnews dealmac http://dealnews.com/ | http://dealmac.com/ - Original Message - From: Jon Parise [EMAIL PROTECTED] To: Brian Moon [EMAIL PROTECTED] Cc: Andrei Zmievski [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Monday, July 02, 2001 7:21 PM Subject: Re: [PHP-DEV] switch, equality and preg_match On Mon, Jul 02, 2001 at 05:01:43PM -0500, Brian Moon wrote: IMO, I like preg_match to return a bool and preg_match_all return a count. But that is me. It may be thought of as inconsistent to some. I just see the functions has having a different purpose. That would make sense. preg_match_all() would return 1 if only one match is found, so there's no need for preg_match() to return the number of matches. However, even if preg_match() were to return a one or zero, it would still function the same in boolean evaluations. That makes the whole thing simply a matter of correctness. Returning an 'int' would be more consistent but returning a boolean would be specific. -- Jon Parise ([EMAIL PROTECTED]) . Rochester Inst. of Technology http://www.csh.rit.edu/~jon/ : Computer Science House Member -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DEV] switch, equality and preg_match
On Sun, 01 Jul 2001, Andrei Zmievski wrote: At 09:06 AM 7/1/01 -0500, Daniel Beckham wrote: Someone poke me when you decide what to do and I'll change the return type in the preg_match docs. I need to take a closer look at the code to see why it is that way. Okay, this is because both preg_match and preg_match_all use the same function internally, so the return type for both is an integer indicating the number of matches. Do you really think it should be changed? -Andrei Tomorrow the sun will rise. And who knows what the tide will bring? - Tom Hanks, in Cast Away -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DEV] switch, equality and preg_match
On Mon, 02 Jul 2001, Daniel Beckham wrote: No matter what, the documentation should be changed. It's incorrect whether you leave the function as is, or not. Although, IMO, I think that it would be more intuitive to make the function's return type boolean instead of int. Hell, for that matter, give us the m// syntax while we are requesting changes. =) Fsck that, I'm not getting into that cesspool again. :) -Andrei Man, if you gotta ask, you'll never know. - Louis Armstrong, when asked What is jazz? -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DEV] switch, equality and preg_match
On Mon, Jul 02, 2001 at 05:01:43PM -0500, Brian Moon wrote: IMO, I like preg_match to return a bool and preg_match_all return a count. But that is me. It may be thought of as inconsistent to some. I just see the functions has having a different purpose. That would make sense. preg_match_all() would return 1 if only one match is found, so there's no need for preg_match() to return the number of matches. However, even if preg_match() were to return a one or zero, it would still function the same in boolean evaluations. That makes the whole thing simply a matter of correctness. Returning an 'int' would be more consistent but returning a boolean would be specific. -- Jon Parise ([EMAIL PROTECTED]) . Rochester Inst. of Technology http://www.csh.rit.edu/~jon/ : Computer Science House Member -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DEV] switch, equality and preg_match
I can answer #2 for you... http://php.net/manual/en/language.types.type-juggling.php http://php.net/manual/en/language.types.string.php#language.types.string.conversion An excerpt from String Conversion: The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). It might be a good idea if switch didn't convert its types, but then that would be inconsistent with the rest of the language, so maybe not. -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DEV] switch, equality and preg_match
Here is your culprit: RETVAL_LONG(matched); The declaration in the docs says the function returns an int. However, the text of the doc says that the function returns True or False. Changing your call to: $match = (bool)preg_false(); /* prints alpha always */ gives you the results you want. Andrei, you are listed as the author on this file. What needs to change? The return type? That would make the most sense to me. Brian Moon -- dealnews.com, Inc. Makers of dealnews, dealmac http://dealnews.com/ | http://dealmac.com/ - Original Message - From: chrism [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Saturday, June 30, 2001 7:02 PM Subject: [PHP-DEV] switch, equality and preg_match I encountered a problem where the result of a preg_match was FALSE. This was fed into a switch statement - and I believe PHP executed the the wrong case section. I've tracked this down to preg_match not really returning FALSE combined with SWITCH executing the first case statement always on either a numeric 0 or BOOLEAN TRUE. As a consequence of auto type conversion, you can't mix STRING/NUMERIC/BOOLEAN within a switch. And with the preg_match issue, you can't assume all input into your switch will be of one TYPE (BOOLEAN). Aside from changing functions to return real booleans 1. Should type conversion be turned off inside switch() ? 2. If 0 == FALSE then why 0 == ANYTHING ? Chris ? /* auto type conversion is the root of my switch problem */ if (0 == ONE) echo one\n; /* TRUE */ if (1 == TWO) echo two\n; /* FALSE */ if (TRUE == THREE) echo three\n; /* TRUE */ if (FALSE == FOUR) echo four\n; /* FALSE */ if (TRUE == 0) echo hopefully not\n; function preg_false() { return(preg_match(/no/, dXiuehXX)); } /* note the difference between FALSE and preg_false() */ // $match = TRUE; /* prints alpha always */ // $match = 0; /* prints alpha always */ // $match = FALSE; /* prints gamma section */ $match = preg_false(); /* prints alpha always */ if(is_bool($match) == TRUE) echo match is boolean\n; switch($match) { case ALPHA: echo hmm.. alpha\n; break; case BETA: echo hmm.. beta\n; break; case 0: echo hmm.. gamma\n; break; case TRUE: echo hmm.. TRUE\n; break; case FALSE: echo hmm.. FALSE\n; break; } ? -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DEV] switch, equality and preg_match
Someone poke me when you decide what to do and I'll change the return type in the preg_match docs. Daniel - Original Message - From: Brian Moon [EMAIL PROTECTED] To: chrism [EMAIL PROTECTED]; [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] Sent: Sunday, July 01, 2001 3:16 AM Subject: Re: [PHP-DEV] switch, equality and preg_match Here is your culprit: RETVAL_LONG(matched); The declaration in the docs says the function returns an int. However, the text of the doc says that the function returns True or False. Changing your call to: $match = (bool)preg_false(); /* prints alpha always */ gives you the results you want. Andrei, you are listed as the author on this file. What needs to change? The return type? That would make the most sense to me. Brian Moon -- dealnews.com, Inc. Makers of dealnews, dealmac http://dealnews.com/ | http://dealmac.com/ - Original Message - From: chrism [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Saturday, June 30, 2001 7:02 PM Subject: [PHP-DEV] switch, equality and preg_match I encountered a problem where the result of a preg_match was FALSE. This was fed into a switch statement - and I believe PHP executed the the wrong case section. I've tracked this down to preg_match not really returning FALSE combined with SWITCH executing the first case statement always on either a numeric 0 or BOOLEAN TRUE. As a consequence of auto type conversion, you can't mix STRING/NUMERIC/BOOLEAN within a switch. And with the preg_match issue, you can't assume all input into your switch will be of one TYPE (BOOLEAN). Aside from changing functions to return real booleans 1. Should type conversion be turned off inside switch() ? 2. If 0 == FALSE then why 0 == ANYTHING ? Chris ? /* auto type conversion is the root of my switch problem */ if (0 == ONE) echo one\n; /* TRUE */ if (1 == TWO) echo two\n; /* FALSE */ if (TRUE == THREE) echo three\n; /* TRUE */ if (FALSE == FOUR) echo four\n; /* FALSE */ if (TRUE == 0) echo hopefully not\n; function preg_false() { return(preg_match(/no/, dXiuehXX)); } /* note the difference between FALSE and preg_false() */ // $match = TRUE; /* prints alpha always */ // $match = 0; /* prints alpha always */ // $match = FALSE; /* prints gamma section */ $match = preg_false(); /* prints alpha always */ if(is_bool($match) == TRUE) echo match is boolean\n; switch($match) { case ALPHA: echo hmm.. alpha\n; break; case BETA: echo hmm.. beta\n; break; case 0: echo hmm.. gamma\n; break; case TRUE: echo hmm.. TRUE\n; break; case FALSE: echo hmm.. FALSE\n; break; } ? -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DEV] switch, equality and preg_match
At 09:06 AM 7/1/01 -0500, Daniel Beckham wrote: Someone poke me when you decide what to do and I'll change the return type in the preg_match docs. I need to take a closer look at the code to see why it is that way. -Andrei -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DEV] switch, equality and preg_match
I wasn't intending to throw blame at preg_match - My main concern is what switch is doing - executing the first CASE when the input is either 0 or TRUE. I don't think switch should execute the first CASE whenever a numeric 0 is fed into it. Even TRUE gives me problems. And with 0 == FALSE, it all seems so messed up. Chris Here is your culprit: RETVAL_LONG(matched); The declaration in the docs says the function returns an int. However, the text of the doc says that the function returns True or False. Changing your call to: $match = (bool)preg_false(); /* prints alpha always */ gives you the results you want. Andrei, you are listed as the author on this file. What needs to change? The return type? That would make the most sense to me. Brian Moon -- dealnews.com, Inc. Makers of dealnews, dealmac http://dealnews.com/ | http://dealmac.com/ - Original Message - From: chrism [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Saturday, June 30, 2001 7:02 PM Subject: [PHP-DEV] switch, equality and preg_match I encountered a problem where the result of a preg_match was FALSE. This was fed into a switch statement - and I believe PHP executed the the wrong case section. I've tracked this down to preg_match not really returning FALSE combined with SWITCH executing the first case statement always on either a numeric 0 or BOOLEAN TRUE. As a consequence of auto type conversion, you can't mix STRING/NUMERIC/BOOLEAN within a switch. And with the preg_match issue, you can't assume all input into your switch will be of one TYPE (BOOLEAN). Aside from changing functions to return real booleans 1. Should type conversion be turned off inside switch() ? 2. If 0 == FALSE then why 0 == ANYTHING ? Chris ? /* auto type conversion is the root of my switch problem */ if (0 == ONE) echo one\n; /* TRUE */ if (1 == TWO) echo two\n; /* FALSE */ if (TRUE == THREE) echo three\n; /* TRUE */ if (FALSE == FOUR) echo four\n; /* FALSE */ if (TRUE == 0) echo hopefully not\n; function preg_false() { return(preg_match(/no/, dXiuehXX)); } /* note the difference between FALSE and preg_false() */ // $match = TRUE; /* prints alpha always */ // $match = 0; /* prints alpha always */ // $match = FALSE; /* prints gamma section */ $match = preg_false(); /* prints alpha always */ if(is_bool($match) == TRUE) echo match is boolean\n; switch($match) { case ALPHA: echo hmm.. alpha\n; break; case BETA: echo hmm.. beta\n; break; case 0: echo hmm.. gamma\n; break; case TRUE: echo hmm.. TRUE\n; break; case FALSE: echo hmm.. FALSE\n; break; } ? -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DEV] switch, equality and preg_match
It is not the switch's fault. That is just the way variable's work in PHP. If you need to know for sure about the type for this, you will need to do this: if($match===ALPHA) { echo hmm.. alpha\n; } elseif($match===BETA) { echo hmm.. beta\n; } elseif($match===0) { echo hmm.. gamma\n; } elseif($match===TRUE) { echo hmm.. TRUE\n; } elseif($match===FALSE) { echo hmm.. FALSE\n; } Brian Moon -- dealnews.com, Inc. Makers of dealnews, dealmac http://dealnews.com/ | http://dealmac.com/ - Original Message - From: chrism [EMAIL PROTECTED] To: Brian Moon [EMAIL PROTECTED] Cc: [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Sunday, July 01, 2001 11:02 AM Subject: Re: [PHP-DEV] switch, equality and preg_match I wasn't intending to throw blame at preg_match - My main concern is what switch is doing - executing the first CASE when the input is either 0 or TRUE. I don't think switch should execute the first CASE whenever a numeric 0 is fed into it. Even TRUE gives me problems. And with 0 == FALSE, it all seems so messed up. Chris Here is your culprit: RETVAL_LONG(matched); The declaration in the docs says the function returns an int. However, the text of the doc says that the function returns True or False. Changing your call to: $match = (bool)preg_false(); /* prints alpha always */ gives you the results you want. Andrei, you are listed as the author on this file. What needs to change? The return type? That would make the most sense to me. Brian Moon -- dealnews.com, Inc. Makers of dealnews, dealmac http://dealnews.com/ | http://dealmac.com/ - Original Message - From: chrism [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Saturday, June 30, 2001 7:02 PM Subject: [PHP-DEV] switch, equality and preg_match I encountered a problem where the result of a preg_match was FALSE. This was fed into a switch statement - and I believe PHP executed the the wrong case section. I've tracked this down to preg_match not really returning FALSE combined with SWITCH executing the first case statement always on either a numeric 0 or BOOLEAN TRUE. As a consequence of auto type conversion, you can't mix STRING/NUMERIC/BOOLEAN within a switch. And with the preg_match issue, you can't assume all input into your switch will be of one TYPE (BOOLEAN). Aside from changing functions to return real booleans 1. Should type conversion be turned off inside switch() ? 2. If 0 == FALSE then why 0 == ANYTHING ? Chris ? /* auto type conversion is the root of my switch problem */ if (0 == ONE) echo one\n; /* TRUE */ if (1 == TWO) echo two\n; /* FALSE */ if (TRUE == THREE) echo three\n; /* TRUE */ if (FALSE == FOUR) echo four\n; /* FALSE */ if (TRUE == 0) echo hopefully not\n; function preg_false() { return(preg_match(/no/, dXiuehXX)); } /* note the difference between FALSE and preg_false() */ // $match = TRUE; /* prints alpha always */ // $match = 0; /* prints alpha always */ // $match = FALSE; /* prints gamma section */ $match = preg_false(); /* prints alpha always */ if(is_bool($match) == TRUE) echo match is boolean\n; switch($match) { case ALPHA: echo hmm.. alpha\n; break; case BETA: echo hmm.. beta\n; break; case 0: echo hmm.. gamma\n; break; case TRUE: echo hmm.. TRUE\n; break; case FALSE: echo hmm.. FALSE\n; break; } ? -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP-DEV] switch, equality and preg_match
I encountered a problem where the result of a preg_match was FALSE. This was fed into a switch statement - and I believe PHP executed the the wrong case section. I've tracked this down to preg_match not really returning FALSE combined with SWITCH executing the first case statement always on either a numeric 0 or BOOLEAN TRUE. As a consequence of auto type conversion, you can't mix STRING/NUMERIC/BOOLEAN within a switch. And with the preg_match issue, you can't assume all input into your switch will be of one TYPE (BOOLEAN). Aside from changing functions to return real booleans 1. Should type conversion be turned off inside switch() ? 2. If 0 == FALSE then why 0 == ANYTHING ? Chris ? /* auto type conversion is the root of my switch problem */ if (0 == ONE) echo one\n; /* TRUE */ if (1 == TWO) echo two\n; /* FALSE */ if (TRUE == THREE) echo three\n;/* TRUE */ if (FALSE == FOUR) echo four\n; /* FALSE */ if (TRUE == 0) echo hopefully not\n; function preg_false() { return(preg_match(/no/, dXiuehXX)); } /* note the difference between FALSE and preg_false() */ // $match = TRUE; /* prints alpha always */ // $match = 0; /* prints alpha always */ // $match = FALSE; /* prints gamma section */ $match = preg_false(); /* prints alpha always */ if(is_bool($match) == TRUE) echo match is boolean\n; switch($match) { case ALPHA: echo hmm.. alpha\n; break; case BETA: echo hmm.. beta\n; break; case 0: echo hmm.. gamma\n; break; case TRUE: echo hmm.. TRUE\n; break; case FALSE: echo hmm.. FALSE\n; break; } ? -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]