[PHP]What do you think of my function?

Results 1 to 6 of 6
  1. #1
    Elite Member timmeh39 is offline
    Member +Rank
    Mar 2007 Join Date
    The NetherlandsLocation
    106Posts

    [PHP]What do you think of my function?

    Well this is a script you can use at register etc.

    what this does exactly:
    If you have a register and the username is max 20 chars long and min 4 chars long you typ it like this:
    var_between($username, int1,int1);

    If it is true(between your filled integers) it wil result in a 1 else in a 0
    PHP Code:
      //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\\
     //!!!!!!!!!!Written by TimmehXXL!!!!!!!!!!!!\\
    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\\
    function var_between($p_string$p_num1$p_num2){
        if(empty(
    $p_string) || empty($p_num1) || empty($p_num2)){
            echo
    "PARSE ERROR: You have to fill in every parameter!";
        } elseif(!
    is_numeric($p_num1) || !is_numeric($p_num2)){
            echo
    "PARSE ERROR: The second and last parameter should be numeric!";
        } elseif(
    $p_num1 == $p_num2){
            echo
    "PARSE ERROR: The last two parameters cant be the same!";
        } elseif(
    $p_num1 $p_num2){
            if(
    strlen($p_string) < $p_num2 || strlen($p_string) > $p_num1){
                return 
    0;
            } else {
                return 
    1;
            }
        } elseif(
    $p_num2 $p_num1) {
            if(
    strlen($p_string) < $p_num1 || strlen($p_string) > $p_num2 ) {
                return 
    0;
            } else {
                return 
    1;
            }
        }
    }

    $username    =    "TimmehXXL";
    echo 
    "Is this true? " var_between($username,4,20); 
    This will result in:
    Code:
    Is this true? 1
    Any comments?
    Last edited by timmeh39; 04-10-09 at 08:14 PM.


  2. #2
    Grand Master FragFrog is offline
    Grand MasterRank
    Aug 2004 Join Date
    The NetherlandsLocation
    5,629Posts

    Re: [PHP]What do you think of my function?

    PHP Code:
    function var_between ($string$min$max) {
      return (
    strlen($string) >= $min && strlen($string) <= $max);
    }

    var_dump(var_between('bla'520));    // false
    var_dump(var_between('blaaat'520)); // true 
    There you go. Does in 1 line the same as what you do in 20 (but returns a proper boolean instead of 1 / 0).

    Random note: it's not a parse error when the input doesn't follow the expected patterns, certainly not in a softtyped language like PHP. Parse errors occur when your source cannot be parsed and in fact already make sure your function has the valid amount of arguments. Just try calling your function with just 2 parameters: it never ever reaches even the first line, so your entire validation there will never run.

    Also, suppose someone just wants to know if a parameter is a max of 10 chars:
    PHP Code:
    var_between($string010); 
    Now your function echo's a silly parse error while it's a completely valid use of the function.

    And on a final note for now: BABO!

    (For those who have not had the pleasure of following a class by Frank Brokken: Blanks Around Binary Operators, ie, put some spaces between your conditions and echo's).
    Last edited by FragFrog; 04-10-09 at 11:33 PM.

  3. #3
    Elite Member timmeh39 is offline
    Member +Rank
    Mar 2007 Join Date
    The NetherlandsLocation
    106Posts

    Re: [PHP]What do you think of my function?

    Lol yes. but i have to check for wrong input

    And if you have 5,4 or 4,5 doesn't matter . in your script it has to be 4,5 else its almost always a fail
    Last edited by timmeh39; 05-10-09 at 10:40 AM.

  4. #4
    Watching from above Negata is offline
    LegendRank
    Apr 2004 Join Date
    FinlandLocation
    5,455Posts

    Re: [PHP]What do you think of my function?

    Quote Originally Posted by timmeh39 View Post
    And if you have 5,4 or 4,5 doesn't matter . in your script it has to be 4,5 else its almost always a fail
    That is the problem of the user, not the function. Of course, it's up to you to define the function so that either order will work, but it's a waste of time and resources. If you take the road of allowing different orders of parameters, why not take a step further and allow also the string to be any of the 3...

    My point, it's just unnecessary. It's OK to assume whoever uses your function, knows how it's meant to be used. This is where documentation comes in. If you want to know more: [ame]http://en.wikipedia.org/wiki/Design_by_contract[/ame]

  5. #5
    :-) s-p-n is offline
    DeveloperRank
    Jun 2007 Join Date
    Next DoorLocation
    2,097Posts

    Support Re: [PHP]What do you think of my function?

    You're function is too programmer-centric, and not enough user-centric. I'm sure that point has already been put to light. When a user puts an empty value, or one that's too small/large, they don't want to see an error. They just want a little guidance on how to improve their values to match the rules of your form. If they see a message that says "PARSE ERROR!!", they might not know what to do, or worse, they'll think they broke something and may run off to a competing web-site. If the programmer using your function does it wrong, that's either their fault for not reading documentation, or your fault for not writing it clearly enough; (Or not writing it at all).

    I'd do it procedurally. I don't see why you'd need a function for that anyhow.

    Fear the Frog. Besides, it's very nice of him to point you in the right direction.

    Anyway, why make a function when it's so easy to check?
    PHP Code:
    $errMsg = (string) null;

    //...

    if(strlen($user)<$min && strlen($user)>$max)
    {
        
    $errMsg .= '<p class="error">Your username must fall between "'.$min.'" &amp; "'.$max.'" characters.</p>';
    }

    //... 

    if(strlen($errMsg)<1)
    {
        
    mysql_query('...'); //Good
    } else
    {
        echo 
    $errMsg//Bad

    I could really care less how many lines it takes. That's not what measures CPU performance. I could overload any system with a single line if I wanted, so that's over-rated to a point. It's mostly knowing what functions do what, and how to cut back on heavier elements to reduce usage. Though line count does give something to work with when trying to simple-down code. Really, it's just for guidance. Most scripts can be improved by adding some spacing between the lines- Or at least it makes it easier to point out problems.

    White-space is our friend.

    Anyway, it's good you're experimenting with functions. Now try to make better use of them. ;)

    For starters, try to find operations that start to repeat themselves. Those are what need functions the most

  6. #6
    Elite Member timmeh39 is offline
    Member +Rank
    Mar 2007 Join Date
    The NetherlandsLocation
    106Posts

    Re: [PHP]What do you think of my function?

    That was realy helpfull :D thanks

    Well what you said about somethings that starts to repeat themselves :P well that is this i use it alot :)
    Last edited by timmeh39; 08-10-09 at 02:22 PM.



Advertisement