• (cs)

    tacostand.php

  • (cs)

    function ReturnFirst() { return "fist"; }

  • Chucara (unregistered)

    You're laughing now, but all your code will break when they change the syntax of '<', '>' etc. With this code, you'll only have to replace it in one function.

    Brillant!

  • (cs)

    I'm very impressed by the use of the explode() function.

    I'm not too familiar with PHP, but this appears to be the finest example of self-documenting code that I've seen for a long while...

  • (cs)

    function WTFPost($postType) { if $postType == "Wooden Table" return "WTF"; if $postType == "QuickBasic" return "WTF"; if $postType == "Enterprise" return "WTF"; if $postType == "Paula Bean" return "WTF"; return "The real WTF is..."; }

  • (cs)

    Forgive me as I am PHP challenged:

    so: truth('Absolutely No Fucking Way') and: truth('FALSE')

    both yield true?

  • (cs) in reply to snoofle
    snoofle:
    Forgive me as I am PHP challenged:

    so: truth('Absolutely No Fucking Way') and: truth('FALSE')

    both yield true?

    That is correct. Actually, his function only tests for FALSEHOOD. Of course, specific cases of falsehood ("no", "false", "off").
  • SchizoDuckie (unregistered) in reply to Chucara
    Chucara:
    You're laughing now, but all your code will break when they change the syntax of '<', '>' etc. With this code, you'll only have to replace it in one function.

    Brillant!

    Actually, this makes me think that those scripts were ported from some kind of other language, or generated by some 'multi language' tool. Now that is some feature! You just have to output the functions like MatchField instead of language native crap and you run everything through your own lib! Portability yay!

  • Anonymous Cow-herd (unregistered)

    TRWTF is that his truth test can't handly FileNotFound.

  • foo (unregistered)

    ... not because there's anything wrong with PHP, but because so many stupid people use it. When you lower the bar so low all kinds of garbage gets in.

  • (cs)

    Ok, so which widely used, open source PHP-based project is this from? phpBB? Wordpress? Drupal? ;-p

  • SunTzuWarmaster (unregistered)

    Other than the disgusting waste of resources/program flow in order to make a simple test that is executed on the hardware/ASM level in most architectures, yes that is a feature.

    If you wanted to make it a true feature, it would be done with a #define (or equivalent) to not waste program flow on it. If you hate #defines (and who doesn't?), then you could upgrade it to an inline function (or equivalent).

    As is, the compiler/assembler will make a label for the function. Then, the program will load all registers onto the stack, jump to the subroutine by loading the memory address, execute the FIVE assembly-level statements (load 2 values, compare, store 2 values back), jump back to the program, and then unload the registers. With an inline, you could cut this down to 5 assembly instructions (but more time compiling). With a #define, you can cut time in both directions.

    I wouldn't be surprised to see a memory leak in there too... Do people seriously think that computers are magic?

  • Renoir (unregistered)

    ...but you see, my new, improved, wheel's much better. It's only got four sides. And if you leave it on a hill, it doesn't roll away...

  • ambrosen (unregistered)

    Well, it's actually the best written code I've seen here in a while.

    Pointless and wasteful, but readable.

  • Nevermind me (unregistered) in reply to Chucara

    Too bad if want to use the same field name for POST & GET methods at the same time :(

    You'd have to do it the unportable, un-future safe 'normal' PHP way.

  • Ghengis Kode (unregistered)

    Sometimes I see code here that make me think it's about time we adopt procedure SDMBS-T Stupid Developer Must Be Shot - Twice

    Better to shoot once in the head and once in the butt. Just to play safe because we have no idea where his brain might be.

    </homicidal> </maniac>
  • Kuba (unregistered) in reply to SunTzuWarmaster
    SunTzuWarmaster:
    As is, the compiler/assembler will make a label for the function. Then, the program will load all registers onto the stack, jump to the subroutine by loading the memory address, execute the FIVE assembly-level statements (load 2 values, compare, store 2 values back), jump back to the program, and then unload the registers. With an inline, you could cut this down to 5 assembly instructions (but more time compiling). With a #define, you can cut time in both directions.

    I wouldn't be surprised to see a memory leak in there too... Do people seriously think that computers are magic?

    Disclaimer: I fail to see the sarcasm.

    So, um, this isn't C/C++. In php, there only way to get memory leaks is by doing known (hopefully) no-nos, such as circular references. php is a latent-typed, garbage collected language, you know...

    As for wastefulness, a good compiler can do compile-time evaluation of certain functions, and this one would be a good candidate. Although such optimizations are more common in languages where the compiler can interpret the code during compilation (Scheme, Lisp, ...), php (just as any other language system) could in principle optimize this function out as follows:

    1. the function has no side effects
    2. the function is stateless (another way of saying 1, perhaps)
    3. 1,2 ==> the function can be evaluated at compile time for constant arguments
    4. the function has constant arguments
    5. 3,4 ==> the function call can be replaced by the result of evaluating the function at compile time

    This can be thought of as compile-time macroization.

    Having written a hacked-up Lisp-like system where this optimization is always done bar compile-time cut-offs, I'd say that your point is kinda moot in principle, but valid for IMHO broken implementations of most languages, whether otherwise thought of as "cool" (say OCaml) or "uncool" (say C).

    Cheers!

  • I_Smoked_It (unregistered) in reply to SunTzuWarmaster
    SunTzuWarmaster:
    Do people seriously think that computers are magic?

    Yes, and the magic works until you let the smoke out.

  • (cs) in reply to snoofle
    snoofle:
    Forgive me as I am PHP challenged:

    so: truth('Absolutely No Fucking Way') and: truth('FALSE')

    both yield true?

    Actually it returns whatever you gave it.

  • (cs) in reply to ParkinT
    ParkinT:
    snoofle:
    Forgive me as I am PHP challenged:

    so: truth('Absolutely No Fucking Way') and: truth('FALSE')

    both yield true?

    That is correct. Actually, his function only tests for FALSEHOOD. Of course, specific cases of falsehood ("no", "false", "off").
    Actually, False (boolean), '0' (string), 0 (integer), 0.0 (float) and Null (Null) - and maybe others I can't recall - will be considered false too, if interpreted as booleans.

    Not that this contradicts anything you said.

  • Anon. (unregistered)

    Argh, my head

    explode()
    s.

  • mr-sk (unregistered)

    dumbest, shit, ever:

    function queryStringToArray($queryString) { global $GL_strlen; global $GL_strpos; if ($GL_strlen($queryString) == 0) { return array(); } $queryArray = array(); $args = explode("&", $queryString); for ($i = 0; $i < sizeof($args); $i++) { if ($GL_strpos($args[$i], "=")) { list($param, $value) = explode("=", $args[$i]); $queryArray[$param] = $value; } else { $queryArray[$args[$i]] = ""; } } return $queryArray; }

  • lyolik (unregistered) in reply to Chucara

    Why would they want to change syntax of ">" "<" etc...? - those are quite fundamental operands in most of programming languages(I used "most" just in case there are some I am not aware about).

  • Bob (unregistered) in reply to ParkinT

    They should have taken a picture of the monitor with the code in the IDE, using a film camera, then gotten it developed, then taken a picture of the photograph using a digital camera, (with the photograph on a wooden table)...

    Oh yeah, and the code is supposed to make it more like Lisp, making it more Enterprisey.

    Oh, and was the programmer's name Paula?

    Oh, and I wrote a CGI daemon in QuickBASIC which runs a forum:

    http://samanddeanus.no-ip.org/forum

    Sources: http://samanddeanus.no-ip.org/start.bas http://samanddeanus.no-ip.org/reply.bas

  • (cs) in reply to lyolik
    lyolik:
    Why would they want to change syntax of ">" "<" etc...? - those are quite fundamental operands in most of programming languages(I used "most" just in case there are some I am not aware about).

    You may want to have your sarcasm detector adjusted.

  • Guillaume (unregistered)

    This code has the truth !

    Ahhhh ! PHP... Producing noob programmers since the beginning.

    CAPTCHA was 'nobis'... no(o)bi(e)s

  • Bob (unregistered) in reply to Someone You Know
    Someone You Know:
    lyolik:
    Why would they want to change syntax of ">" "<" etc...? - those are quite fundamental operands in most of programming languages(I used "most" just in case there are some I am not aware about).

    You may want to have your sarcasm detector adjusted.

    Scheme/Lisp? Then they'd be built in FUNCTIONS so ha.

  • Munger (unregistered)
    function truth($fieldValue) { if ($fieldValue == "no" || $fieldValue == "false" || $fieldValue == "off") { return false; } else { return $fieldValue; } }

    Laugh if you will, but I use this particular construct when checking info which can originate from a Web page (check box/ radio button), a database ('t' or boolean), or a language construct (boolean).

    Why repeat the entire test all over the place when a simple function call will do? And if you need to add another test for true/false, then you simply adjust the one function.

    But I would return 'true' in the else.

  • Otto (unregistered)

    I find it amazing that nobody noticed that you could write the queryStringToArray function like this:

    function queryStringToArray($queryString) { parse_str($queryString, $newarray); return $newarray; }

    It's always a bad sign when people write functions to duplicate built in functionality.

  • Just Some Guy (unregistered)
    if (MatchField($login->Value("admin"), "==", "1")) {

    I'm betting that at one point there was a database involved somewhere, or that they planned to have one. The first value was supposed to be the name of a column, and the function's original name was something like CheckSessionValue.

    Or maybe they're just enterprisey.

  • aliquam (unregistered)

    that function

    MatchField($field, $cond, $value)
    would be soo much faster with a switch statement :/

    Also I guess it's good that ReturnTrue() doesn't return false...

  • (cs) in reply to Just Some Guy
    Just Some Guy:
    if (MatchField($login->Value("admin"), "==", "1")) {

    I'm betting that at one point there was a database involved somewhere, or that they planned to have one. The first value was supposed to be the name of a column, and the function's original name was something like CheckSessionValue.

    Or maybe they're just enterprisey.

    But that's hardly enterprisey at all, is it? I mean, the operator is a literal, for gawd's sake.

    Real Enterprisey (c) would be as follows:

    if (MatchField($login->Value("admin"),
                         $db->getOperator (__FILE__, __LINE__),
                         "1")) {

    This allows for instant configurability via a database, and has the added benefit that you can add distributed grid networking by ensuring that $db refers to an object encapsulating some sort of RPC.

    I'm still a little concerned about the "1" though.

  • (cs) in reply to SunTzuWarmaster
    SunTzuWarmaster:
    Other than the disgusting waste of resources/program flow in order to make a simple test that is executed on the hardware/ASM level in most architectures, yes that is a feature.

    If you wanted to make it a true feature, it would be done with a #define (or equivalent) to not waste program flow on it. If you hate #defines (and who doesn't?), then you could upgrade it to an inline function (or equivalent).

    As is, the compiler/assembler will make a label for the function. Then, the program will load all registers onto the stack, jump to the subroutine by loading the memory address, execute the FIVE assembly-level statements (load 2 values, compare, store 2 values back), jump back to the program, and then unload the registers. With an inline, you could cut this down to 5 assembly instructions (but more time compiling). With a #define, you can cut time in both directions.

    I wouldn't be surprised to see a memory leak in there too... Do people seriously think that computers are magic?

    Very impressive post. I'd almost believe that you had a clue about the subject, except for the fact that you're spouting all that stuff about assembly instructions, registers, and jumps, and totally missing the fact that this is PHP, which is interpreted. In other words, all you did was post non-applicable information.

    Nice try, though. If it hadn't been so far out of context, it might have been an impressive post.

  • (cs) in reply to Chucara
    Chucara:
    You're laughing now, but all your code will break when they change the syntax of '<', '>' etc. With this code, you'll only have to replace it in one function.

    Brillant!

    PHP being PHP, this is sadly not a remote possibility.

    Any language that can produce a syntactic wart such as "===" is quite capable of such behaviour.

    I look forwards to PHP6 with trepidation.

  • A Few Good Coders (unregistered)

    function truth($fieldValue) { return "You can't handle the truth!"; }

  • N. K. (unregistered) in reply to aliquam

    Hah, this is not really all that bad. On my previous job, people would most likely write it as:

    /**
     *
     *
     * @param $field
     * @param $cond
     * @param $value
     */
    function matchField($field, $cond, $value) {
        if ($cond == '<')
            return ($field < $value) ? true : false;
        if ($cond == '<=')
            return ($field <= $value) ? true : false;
        if ($cond == '==')
            return ($field == $value) ? true : false;
        if ($cond == '!=')
            return ($field != $value) ? true : false;
        if ($cond == '>=')
            return ($field >= $value) ? true : false;
        if ($cond == '>')
            return ($field > $value) ? true : false;
    
        if ($cond == 'like')
            return strpos($field, $value) === false ? false : true;
        if ($cond == 'in')
            return strpos($value, $field) === false ? false : true;
    
        return false;
    }
    
    /**
     *
     *
     * @param $expression
     */
    function isTrue($expression) {
        return $expression == true ? true : false;
    }
    aliquam:
    that function
    MatchField($field, $cond, $value)
    would be soo much faster with a switch statement :/

    I hope you are being sarcastic.

  • Alex (unregistered) in reply to Otto
    Otto:
    I find it amazing that nobody noticed that you could write the queryStringToArray function like this:

    function queryStringToArray($queryString) { parse_str($queryString, $newarray); return $newarray; }

    It's always a bad sign when people write functions to duplicate built in functionality.

    Yeah, PHP has so little built-ins anyway right ? You'd have to print a 500 page book only to list them all...

    This guy was bad for sure... Really... But please stop saying "Oh, but there's a function for that in PHP, loser". I'm pretty sure you've done a function or two that replicates a PHP built-in.

  • (cs) in reply to lyolik
    lyolik:
    Why would they want to change syntax of ">" "<" etc...? - those are quite fundamental operands in most of programming languages(I used "most" just in case there are some I am not aware about).

    FORTRAN uses .LT., .GT., .EQ. and so on

  • (cs)

    TRWTF is that he explodes the string (poor thing) instead of using his indexof function. To be useful it should be used more often.

  • (cs) in reply to real_aardvark
    real_aardvark:
    Chucara:
    You're laughing now, but all your code will break when they change the syntax of '<', '>' etc. With this code, you'll only have to replace it in one function.

    Brillant!

    PHP being PHP, this is sadly not a remote possibility.

    Any language that can produce a syntactic wart such as "===" is quite capable of such behaviour.

    I look forwards to PHP6 with trepidation.

    BTW, maybe this guy wrote all that stuff in PHP version 0.1 and all our wisdom wasn't applicable at that time???

  • (cs)

    So the obscure function checks for $plaintext, but if it's not found, adds urlencode($plaintext)?

  • Bob (unregistered) in reply to ClaudeSuck.de
    ClaudeSuck.de:
    FORTRAN uses .LT., .GT., .EQ. and so on

    Yes, but Fortran 90 and later can also use <, >, ==, /= (for "is not equal to"), >=, <=, etc.

  • (cs) in reply to Bob
    Bob:
    ClaudeSuck.de:
    FORTRAN uses .LT., .GT., .EQ. and so on

    Yes, but Fortran 90 and later can also use <, >, ==, /= (for "is not equal to"), >=, <=, etc.

    This means than that his function is really useful as he could compare then even in Klingon

  • (cs) in reply to Bob
    Bob:
    ClaudeSuck.de:
    FORTRAN uses .LT., .GT., .EQ. and so on

    Yes, but Fortran 90 and later can also use <, >, ==, /= (for "is not equal to"), >=, <=, etc.

    I'm beginning to lose the will to live.

    Perhaps it's time to give up programming in any language whatsoever and just express myself in unadulterated predicate logic.

  • (cs) in reply to real_aardvark
    real_aardvark:
    Bob:
    ClaudeSuck.de:
    FORTRAN uses .LT., .GT., .EQ. and so on

    Yes, but Fortran 90 and later can also use <, >, ==, /= (for "is not equal to"), >=, <=, etc.

    I'm beginning to lose the will to live.

    Perhaps it's time to give up programming in any language whatsoever and just express myself in unadulterated predicate logic.

    Don't worry, just keep going until Xmas 2012 and all your problems will vanish all of a sudden.

  • Thunder (unregistered) in reply to SunTzuWarmaster
    SunTzuWarmaster:
    Other than the disgusting waste of resources/program flow in order to make a simple test that is executed on the hardware/ASM level in most architectures, yes that is a feature.

    If you wanted to make it a true feature, it would be done with a #define (or equivalent) to not waste program flow on it. If you hate #defines (and who doesn't?), then you could upgrade it to an inline function (or equivalent).

    As is, the compiler/assembler will make a label for the function. Then, the program will load all registers onto the stack, jump to the subroutine by loading the memory address, execute the FIVE assembly-level statements (load 2 values, compare, store 2 values back), jump back to the program, and then unload the registers. With an inline, you could cut this down to 5 assembly instructions (but more time compiling). With a #define, you can cut time in both directions.

    I wouldn't be surprised to see a memory leak in there too... Do people seriously think that computers are magic?

    Heh, that was a nice attempt at sounding intelligent and knowledgeable. Failed, but nice attempt. What, exactly, are you talking about that's relevant to this article? The code would have generated far more than "FIVE" instructions in all cases, because it's not running natively to begin with.

  • (cs) in reply to real_aardvark

    It is unlikely that PHP would change the meaning of "<", ">", or "==".

    PHP being PHP though,it is easily conceivable that they would deprecate those operators in favor of (respectively) "lessThan", "is_greater_than", and "equals"

  • kenrick (unregistered)

    oh man, i just had to work on a site for a client and found that very MatchField function in the codebase!

    it must be listed on some crap php development site somewhere. sad how the interwebs corrupts completely.

  • (cs)

    A more robust version of IsTrue in c#:

    public bool IsTrueN(bool value, int times) {
    for(int i = 0;i<times;i++) { value = !!value; } return value; }

    making the wtf even wtfier.

  • (cs) in reply to Mhendren
    Mhendren:
    It is unlikely that PHP would change the meaning of "<", ">", or "==".

    PHP being PHP though,it is easily conceivable that they would deprecate those operators in favor of (respectively) "lessThan", "is_greater_than", and "equals"

    ... and thus my most excellent Real Enterprisey (c) solution to precisely this problem.

    In fairness to PHP, though, it's not really an Enterprisey language, is it? Java is so much better at this sort of thing.

Leave a comment on “wtflib.php”

Log In or post as a guest

Replying to comment #199020:

« Return to Article