- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Alex, you have the best titles for these.
They always crack me up.
Admin
I can only assume that the developer of that function didn't look at the manual too hard, otherwise they would have spotted the built-in is_numeric() function in PHP.
That, or this codebase was developed for PHP version 3, which didn't have that function. But who uses PHP 3 these days?
Admin
Obviously, the function name we were looking for here was IsBetweenZeroAndNine().
Admin
Technically, they should use is_integer() instead of is_numeric(). There is probably an equally bad IsNumeric() somewhere that returns false for 5.4735e+001
Admin
Most technically, the name should probably be:
<FONT color=#0000ff>function</FONT> <FONT color=#0000ff>is_$Len=strlen($String);for($i=0;$i<$Len;$i++){if(($String[$i]>'9')||($String[$i]<'0')){return(false);}}return(true);_($String)</FONT>
[H]
Admin
I'm so avoiding-work-right-now that I actually tested this function. I can't figure out what it tells you. The param $String is presumably, well, a string, but then he references it as an array - $String[$i].
It always returns false unless you pass it a null or empty string, which of course returns true.
Admin
I'd prefer is_safely_convertible_to_integer_without_truncation_unless_it_has_to_convert_to_float_due_to_overflow_oh_well_it_can_be_made_some_kind_of_non_fractional_number_anyway()
Admin
$String[$i] is the old way of saying $String{$i}. Zend developers don't like overloading operators (they get confused easily), so it was changed sometime in PHP4. Presumably it was removed (or at least turned off by default) in 5.
Admin
Ok. this is intense.
Admin
And IsInteger("") returns true.
Admin
So it does -- I'll have to adjust the name to account for that.
Admin
I don't know anything about PHP, so could someone explain the WTF to an outsider?
With my lack of PHP-knowledge, it looks like it checks if a string is an integer () or the empty string..? And this is what one would expect from the name of the function (except maybe the empty-string thing - depends on how you define an "integer-string" (or whatever you wanna call it)).
What am i not getting?
() ofcause i know a string is not an integer, but you guys should know what i mean
Admin
(I'm assuming we're talking unsigned integer-strings ofcause - it it's supposed to work for signed, i get it)
Admin
I believe that the WTF here, as mentioned above in pieces, is that the intended purpose of the function was to walk through every character in the string input, and ensure it is a numeric digit (0-9). The function name is "isInteger". Integers can be signed (ie, be positive or negative), so if you call the function with "-2", you will get a false return value even though -2 is a valid integer.
Also, an empty string will cause the for loop to be bypassed, since the string length is zero, leading directly to the return true statement, which in effect is saying that an empty string is an integer also.
in pseudocode:
a. Get a string containing 0 or more characters.
b. loop through each of the characters in the string, and return false if any aren't between 0-9
c. return true otherwise.
Hope that helps!
Admin
Wish we could edit posts. What I meant was, the intended purpose of the function, given its' name, is to determine if the string input represents an integer. What it in fact does is attempt to validate that all characters in the string are digits, and does not correctly handle the negative sign or the empty string case.
Admin
Ah, ok - so it's a function-naming thing (or a really lame code error made by a tired/horny coder :)). Ok, thanx.
Admin
And should '00' return true or false? Leading zeroes are OK I suppose, but still...
Admin
"IsWholeNumber()" would pretty much capture it succinctly. Oh, except for that empty string thingy. “IsWholeNumberOrEmptyString()”. Or Null. “IsWholeNumberOrEmptyStringOrNull().” Okay. Still could have its uses.
Wait, 1.435e6 is a whole number.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
“IsWholeNumberOrEmptyStringOrNullOrNotExpressedInScientificNotation()”<o:p></o:p>
There.<o:p></o:p>
--Rank Amateur<o:p></o:p>
I’m not even a developer, but this is funniest site on the web for me.
Admin
Hmm... yeah... But for those who really likes descriptive function-names, maybe these names are better:
isTheParameterGivenToThisFunctionAnIntegerThatIsNotNegativeButMaybeEmpty ()
isTheParameterThatIsGivenToThisFunctionAString_WhereEachCharIsBetween0And9IncludingBoth0And9_WhereTheIntergerThatTheseCharsRepresentsAreNotNegative_ByTheWayTheStringCouldBeEmpty Aswell() [:)]
but other might argue that the name is too verbose. Maybe "isUInt()" or "a()" would be better?
Admin
honestly, it disturbs me to see an integer wrapped in ' delimiters. I'm still at a lost for words with this one, and I'm fairly glad I didn't have to learn php.
I'm still trying to convince my friend to give me a copy of the if statement that went like
if (true)
{
// Complete Action
/* insert other code here */
}
else
{
// There was an error.
/* there was actually code here. */
}
Admin
Oh, what about 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999?
This function would return true, but I can't see any language (I don't know about PHP) supporting such an integer.
Admin
Anonymous: Regarding the insanely long sequence of 9's.
Congrats on being the first to catch that! Totally didn't occur to me (or anyone else, it would seem.)
Admin
The funny part is that the discoverer was not calling this function. He was re-using a class (in production for a year and a half) written by someone else which called the above function He noticed a member function of the class did not work with negative numbers so he had to trace it back to the library function.
Next he looked like he might explode into a rant about the competency of his co-workers but instead he told someone else about it. His co-worker said "which one of them did this?" He replied with "I, um, it could be a couple of people." I think he started to cry at that point.
Admin
I feel your pain, I work with someone that does some interesting things sometimes, and has some interesting ideas.
This latest one was interesting...
C# has this cute little class called FileInfo, you create it by passing a filename or path to a file in its constructor, and it can tell you all sorts of neat little things about a file. It also has some useful methods to manipulate files. One of these methods is called CopyTo, which you guessed it, copies the file described by the FileInfo to the location you specify.
So, one of the things he has to implement is a way to send a file from the server to a client (see where this is going?). So he tells me what he wants to do, which is create a FileInfo class of the file on the server, send that class to the client, then call CopyTo from the client to send the file to the client. I told him it wouldn't work, but he didn't believe me. So I told him to go ahead and implement and show me. I'm still waiting to see it =)
Admin
this person can be lucky im not their boss, would have to throw them out a high window. anyway does php not have some sort of integer.parse function?????!??!??!?! wtf!
Admin
There are a few, python is one - it has native support for unbounded integers. I'm pretty certain matlab does and functional languages will.
But that doesn't make this function any less moronic.
Admin
So go to your Source Code Control system, review the history of the file, determine who's the responsible party and take the corrective action of <your code goes here>.
Admin
everyone forgets about "+2"...
Admin
Really? I can't see see any proper language not supporting something like that. In fact, transparent bignum support is one of my (several) criteria for judging whether or not a language is worth using.
Admin
Andrew, in php the integer parse function is intval()
Admin
Re: "transparent bignum support"
I would disagree with that - If I'm using the return value of this function to determine if I can store into a variable declared as a "regular" integer, I do not want that variable promoted automagically to a bigint (or whatever you want to call it). If code using that result tried to insert the value of my promoted variable into a database (none of which I am aware of can promote ints automagically), an exception would be thrown.
But, what the heck to I know? [:D]
And, to quote someone's sig that I see on here regularly, I've had a few beers before posting this, so excuse me if I'm way off base.
Admin
Cool, I created one of the microscopic font posts. Which I could remember how I managed to do that, so I could write a "proper bug report"[:P]
Admin
This is the "cowardly" anonymous poster who refuses to register (for fear of being "clowned"):
Microscopic font huh? Now who's the clown? Ha!
+1 for all the anonymous posters who need support groups!
Admin
While others may have, I for one have not accused anonymous posters of being cowardly. Again, it's just to maintain a coherent reply chain due to lack of threading.
Exactly how does the fact that the forum software occasionally causes posts to appear in tiny fonts cause me to be a clown?
-1 for not making any sense whatsoever.
This whole conversation is beginning to have the stench of trolling, so, as I stated in the other post, I'm done. Enjoy your anonymousness. My point has obviously been missed.
Admin
"Oh, what about 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999?
This function would return true, but I can't see any language (I don't know about PHP) supporting such an integer."
Python 2.4 (#1, Jan 12 2005, 11:15:47)
[GCC 3.4.3 20050104 (Red Hat 3.4.3-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> i = 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
>>> print i
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
>>> print i+1
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Admin
python can support such a long sequence of 9s and there are surely others.
Admin
Anonymous: the 99999999999999999... integer
There are many languages which support this. Python is one of the more popular one.
<font face="Courier New">
$ python
....
>>> i = 999999999999999999999999999999999999999
>>> print i
999999999999999999999999999999999999999
>>></font>
Admin
Blue is absolutely right when he says -1 for you not making sense. As a matter of fact I think it's pretty funny...because you were so fearful of being "clowned" and yet you completely clowned yourself with your last comment.
Enjoy the clownage! [;)]
Admin
In the interests of being a fug smucker, I shall post my version of this function (let's forget that the is_xxx functions exist, for didactic purposes):
<font face="Courier New"> // optional sign and any number of digits
// note: doesn't check for overflow
</font><font face="Courier New">
function is_integer($val)
{
return preg_match('/^[-+]?\d+$/',$val);
}
</font>
PHP's regexp handling is lifted straight from Perl, and is therefore some of the best-optimised string-handling code ever written. You can't in all honesty expect to write anything faster.
Oh... and the correct name for the original function would have been "is_cardinal" - a cardinal number is basically a non-negative integer. The only problem then is people passing the names of Catholic clergy to the function and being surprised at the answer...
Admin
So? The function isn't called Is32BitInteger(), it's called IsInteger().
Admin
Hmm, I wonder.. Does PHP require return values to be encased in parentheses? Or was the coder just rying to be sure the non-existant ands and ors would be resolved in the proper order?
Drak
Admin
Actually 99999999999999999999999999999999999999999... (without the ...). Is a valid mathematical integer although your compiler is not able to accept this in this way. [;)]
new functionname: TryToDeterminIfIsPositiveInteger_WithBugs($String);[^o)]
Admin
Nice one bat, made me laugh out loud. Could just call the function PopeJeanPaulII() and then it's something that is sort of like a cardinal but likely to keel over at any moment.
Admin
Not the only problem - "is_cardinal" would also have to recognise a songbird of the finch family and Arizona football players.
Admin
I don't think it's a big deal. Setting aside that he is re-inventing a function, which seems to be very common for newbies, who don't know s**t about the capabilities of the language and the libs they could use.
The quick solution would be to just search the entire project and replace the name of the function with IsPositiveInteger then create another IsInteger and it's ok - or start using the built in functions but this is just another search & replace - basically every text editor for developers can do a search and replace on an entire project...... and everyone managed to screw up royally at least once in his/her lifetime a project using this feature.
Admin
The thing is that he reinvented a function in place already, and did so incorrectly.
Relying on search and replace will lead you down the dark path. Once it consumes you, forever will it control your destiny, or something like that. Especially in a real-time interpreted language like PHP.
Admin
Um, asides from the fact that it may puke on negative numbers and the like, did y'all (other than the originator of the thread) not notice that the function return value is the opposite of what the function name seems to indicate?
IOW - it returns a a "TRUE" if the string is NOT an integer!
So it should be named IsNotInteger()
Admin
Umm....
<FONT color=green>// is a variable an integer?</FONT>
<FONT color=blue>function</FONT> IsInteger($String)
{
$Len = <FONT color=blue>strlen</FONT>($String);
<FONT color=blue>for</FONT> ( $i=0; $i< $Len; $i++ ) {
if ( ($String[$i] > <FONT color=red>'9'</FONT>) || ($String[$i] < <FONT color=red>'0'</FONT>) ) {
<FONT color=blue>return</FONT> (<FONT color=blue>false</FONT>);
}
}
<FONT color=blue>return</FONT>(<FONT color=blue>true</FONT>);
}
<FONT style="BACKGROUND-COLOR: #efefef">I have taken the liberty of highlighting a few key things in this function. Let see, the function returns false of the value of $string[$i] is less than 0 or greater than 9. Think about that for a few seconds. If after reading the entire string in, none of these conditions are met, and they will not be if it's a string of digits, then it returns true. </FONT>
<FONT style="BACKGROUND-COLOR: #efefef">I certainly hope you are not a programmer or if not, you do not enter into the field of programming, if you cannot follow simple boolean logic. </FONT>
You've been cl0wned. [:P]
Admin
Well, when booleans consist of true, false, or maybe...it gets kinda confusing.
Admin