Anlamak &&

2 Cevap php

How can you get 1 # 2 çıkış olarak?

#1

Ben bu kodu koştum

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

Ben olsun

111

her şeyin tamam olduğu anlamına gelir.

#2

Ben bu kodu çalıştırın

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 );

Ben olsunnothing as an ouput.

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

2 Cevap

Sadece returning 1, aslında yankı-ing değil çünkü çıktı olarak bir şey olsun. Bunu yapmak için PHP söylemek sürece Şeyler kullanıcıya yankı-ed alamadım.

Örnek:

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

echo test();

Ayrıca, burada if genel puan üzerinde bir çift vardır:

  • Eski daha yaygın olduğu gibi muhtemelen && yerine AND kullanımı gerekir.
  • You don't need the == 1 bit in there - 1 is always TRUE. Örnek:

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

    Yerine:

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

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

Şey dönen STDOUT bu çıkışı değil. Sen o istiyorsanız bunu yankı gerekir.