SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Return from function

    I've trouble whit the return (I think) from a function.

    Here is what I have so far:
    PHP Code:
    function checkString($str$level) {
        if (
    $level == 1) {
            
    $str strip_tags($str);
            
    $str trim($str);
            return 
    $str;

        } elseif (
    $level == 2) {
            if (
    preg_match('/^[A-Za-z0-9]{6,15}$/'$str)) {
                return 
    true;
            } else {
                return 
    false;
            }
        }

    Here's the php-code where I use the function.

    See comments in the code
    PHP Code:
    $_POST["userid"] = checkString($_POST["userid"], 1); // This line works fine.
    echo "Userid: {$_POST["userid"]}<br>"
        
    if (
    checkString($_POST["userid"], 2)) { // This doesn't work
        // Userid is ok!
    } else {
        
    // Userid is NOT ok!

    Does anyone see what I'm doing wrong? I'm pretty new to function, so I'm on deep water.

    -Helge

  2. #2
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    same here but wouldn't this be a little better
    PHP Code:
    function checkString($str$level)
    {
        if (
    $level == 1)
        {
            
    $str trim(strip_tags($str));
            return 
    $str;

        }
        elseif (
    $level == 2)
        {
            if (
    preg_match('/^[A-Za-z0-9]{6,15}$/'$str))
            {
                return 
    true;
            }
            else
            {
                return 
    false;
                }
        }

    functions should look like this
    PHP Code:
    function foo ($arg_1$arg_2)
    {
        echo 
    "Eg function.\n";
        return 
    $return_value;


  3. #3
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply Andrew-J2000.
    Ok, I checked with the Pear standard and I was writing the function wrong but the if-statement correct .

    From the Pear Manual:
    PHP Code:
    function connect(&$dsn$persistent false)
    {
        if (
    is_array($dsn)) {
            
    statement1;
        } else {
            
    statement2;
        }
        return 
    true;

    But... I don't see how your changes affect my problem.

    I'll try to explain my problem better (please see first post again to see the code):
    In my function I have to levels (so far) of checking strings that comes from the user using a form.

    First I run the string trough level one to strip the html-tags. This works fine!

    In level two I try to check that the string only contains accepted variables and is between 6 and 15 caracters long. This is where I have the problems. I know that the preg_match is correct. (See my previous thread)

    I've tested the preg_match-if-statement outside the function and then I get it to work. But inside the function I don't get the desired result.

    Does it have something to do with the way I call the function in this if-statement?
    PHP Code:
    if (checkString($_POST["userid"], 2)) { // This doesn't work
        // Userid is ok!
    } else {
        
    // Userid is NOT ok!

    If you see what I'm doing worong, please enlighten me!

    -Helge

  4. #4
    SitePoint Member
    Join Date
    Mar 2002
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You want to create a variable that contains the function's return value. Then you can test it to see if the condition is true or false:

    PHP Code:
    <?php
    $userid 
    "moosefeathersardyhar"//should return false and print out "Userid is NOT ok!"
    $user_ok checkString($userid,2); // $user_ok contains whatever is returned by the function
    if ($user_ok == true) { 
        echo (
    "Userid is ok!");
    } else {
         echo(
    "Userid is NOT ok!");
    }

    function 
    checkString($str$level)
    {
        if (
    $level == 1)
        {
            
    $str trim(strip_tags($str));
            return 
    $str;

        }
        elseif (
    $level == 2)
        {
            if (
    preg_match('/^[A-Za-z0-9]{6,15}$/'$str))
            {
                return 
    true;
            }
            else
            {
                return 
    false;
                }
        }
    }
    ?>

  5. #5
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks moose, I finally got it to work.

    -Helge


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •