tags:

views:

119

answers:

2

How can you get 1 as an output from #2?

#1

I ran this code

echo validate_email ( $email );
echo validate_password ( $password );
echo validate_username ( $username );

I get

111

which means that everything is ok.

#2

I run this code

function validate ( $email, $password, $username ) {
    if (  (validate_email ( $email ) == 1)
        AND (validate_password ( $password ) == 1)
        AND (validate_username ( $username ) == 1 ) )
            return 1;
}
echo validate ($email, $password, $username );

I get nothing as an ouput.

I tried to fix the problem by changing AND to && but the same output remains. The output should be 1.

+5  A: 

You get nothing as output because you are only returning 1, not actually echo-ing it. Things don't get echo-ed to the user unless you tell PHP to do so.

Example:

function test() {
    if (  (validate_email ( $email ) == 1)
        AND (validate_password ( $password ) == 1)
        AND (validate_username ( $username ) == 1 ) )
            return 1;
    else
            return 0;

echo test();

Also, here are a couple of points on if in general:

  • You should probably use && instead of AND as the former is more common.
  • You don't need the == 1 bit in there - 1 is always TRUE. Example:

    if ( validate_email($email) ) // correct email
    

    Instead of:

    if ( validate_email($email) == 1 ) // correct email
    
Lucas Jones
The missing `echo` was a bug in my question. I fixed it. I removed these `== 1` in my code, and the code started to work as expectde. -- This suggests me that two last lines of code are not equivalent. This is strange, since the two lines should be equivalent.
Masi
@Masi: your validate_*() functions are probably returning true instead of 1, thats why strict comparison (=== instead of ==) didn't work.
Alix Axel
+2  A: 

In the first example you echo the output of each function. In the second example you return 1 if they all equal 1.

Returning something doesn't output it to STDOUT. You need to echo it if you want that.

David Dorward