Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

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

2 Answers 2

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.

share|improve this answer
    
Thanks! Having clean code is good. What will you suggest as alternatives to returning different types? –  shenge86 Nov 20 '11 at 20:41

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
}
share|improve this answer
    
What happened to skeet's answer lol –  Shredder Nov 18 '11 at 17:47
    
Thanks for the tip on using is_array to check. Now it works perfectly. –  shenge86 Nov 20 '11 at 20:41
    
@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. –  Rocket Hazmat Nov 21 '11 at 0:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.