1

Now, $num here is just the number of lines of data from a database search. Sometimes, it will be 0. $procrastinate is a function argument.

if ($num==0) {
    return "Do not appear to be registered. Please check your email input again.";
    break 1;
} else { // there is an entry. check for consistency
    $procrastinateDB = mysql_result($result,0,'procrastinate');
    if ($procrastinate != $procrastinateDB) {
        return "Answer to your procrastination question is incorrect. Please try again!";
        break 2;
    }
    else {
        $username = mysql_result($result,0,'usrname');
        $passwd = mysql_result($result,0,'passwd');
        return array($username, $passwd, $num);
        break 2;
    }
}

What I'm not understanding is that even when $num=0, an array is still returned with $usersname = 'D', $passwd = 'o', and $num = ' '. Clearly, it's taking the first returned statement and just assigning these variables to each character in the first returned sentence in sequential order.

How do I stop this? I don't want $username, $passwd, $num to contain any value if there is no database entry.

4
  • 2
    Just a note: the break statements are useless here. return already exits the function, so break is not needed. Commented Nov 18, 2011 at 17:17
  • @hakre: What are you talking about? Commented Nov 18, 2011 at 17:28
  • @Rocket: I was making jokes ;) Commented Nov 18, 2011 at 17:29
  • @hakre: Ah, guess I missed it, sorry. My brain is kinda scrambled today, got a lot of work to do. Commented Nov 18, 2011 at 18:22

2 Answers 2

3

Some comments about your code:

  • You don't need to break after return.
  • break does not work for if in PHP.
  • You don't need to do else if you're using return already.

I made some changes to your code to make this more visible and it reduces the cyclomatic complexity as well. Probably this helps that you find your error easier:

function unnamed(/* ... unknown parameters ... */)
{
    /* ... some code ... */
    if ($num==0)
    {
        return "Do not appear to be registered. Please check your email input again.";
    }

    // there is an entry. check for consistency
    $procrastinateDB = mysql_result($result,0,'procrastinate');
    if ($procrastinate != $procrastinateDB)
    {
        return "Answer to your procrastination question is incorrect. Please try again!";
    }

    $username = mysql_result($result,0,'usrname');
    $passwd = mysql_result($result,0,'passwd');
    return array($username, $passwd, $num);
}

Your function returns a string or an array btw. If you don't check the return type and assume it's an array, you will trigger substring access (see it documented on the string manual page):

$string = 'ABC';
echo $string[0]; // A
echo $string[2]; // C

It's often better to have a function return one type, not multiple. However this depends on your design and coding style, so I can only make suggestions, like return an object that contains a status message, a status code (success/failure) and - if available - the array of data you want to return. However that depends what you want / need. You can as well check the return type with is_array as well.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Having clean code is good. What will you suggest as alternatives to returning different types?
1

an array is still returned with $usersname = 'D', $passwd = 'o', and $num = ' '.

When $num is 0, you are returning the string:

Do not appear to be registered. Please check your email input again.

In PHP, you can access strings like arrays. So, my guess is you didn't check to see if you were returned a string or an array, and did something like this:

list($username, $passwd, $num) = yourFunction();

Since strings can be accesed like arrays, $username would be D, $password would be o, and $num would be a space, as those are the 1st three characters.

I suggest using is_array before accessing the value to make sure you know what you're getting.

$ret = yourFunction();
if(is_array($ret)){
   list($username, $passwd, $num) = $ret;
}
else{
   // Something else
}

2 Comments

Thanks for the tip on using is_array to check. Now it works perfectly.
@shenge86: No problem. Gotta be careful with PHP, variables can be of any type, so you need to make sure you know what each one is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.