如何在if条件下比较两个段落...使用任何语言[关闭]

function paraMatch($consumerid1, $consumerid2)
    {
    $para1=getpara1($consumerid1);
    $para2=getpara2($consumerid2);
    echo $para1;
    echo $para2;
    if($para1=$para2)   
      echo 1;
      return 1;
    else
       echo 0;
       return 0;
}

Your error is in the == and please don't use 0 and 1 try true and false or is better and missing {}

Here is the working code:

function paraMatch($consumerid1, $consumerid2)
    {
    $para1=getpara1($consumerid1);
    $para2=getpara2($consumerid2);
    echo $para1;
    echo $para2;
    if($para1 == $para2)   {
      echo true;
      return true;
    }
    else{
       echo false;
       return false;
    }
}

Call the function like this:

if(paraMarch($id1 , $id2) == true){
//true
}else{
//false
}

that would solve all possible problems

By any languages are you referring to the programming aspect or the language the paragraph is written in "aspect"? If you mean the latter, than that will be very hard. But if you mean the programming aspect and assuming you mean PHP since it's the only programming language you tagged then here my answer.


//You can use the strcmp function
function paraMatch($consumerid1, $consumerid2)
    {
    //Don't know what you are doing here
    $para1=getpara1($consumerid1);
    $para2=getpara2($consumerid2);
    echo $para1;
    echo $para2;

    //======================================

    if(strcmp($para1,$para2) == 0) {//strcasecmp($para1,$para2) //for case-insensitive 
      echo 1;
      return 1;
    } else {
       echo 0;
       return 0;
    }

}

Or you can use the method where you use 3 equal signs. The difference between 2 and 3 equal signs can cause problems since PHP is dynamic unless you are sure that both variables will be the same type or you know what you are doing. php == vs === operator.


function paraMatch($consumerid1, $consumerid2)
    {

    $para1=getpara1($consumerid1);
    $para2=getpara2($consumerid2);
    echo $para1;
    echo $para2;

    if($para1 === $para2) {// $para1 == $para2 //Can work as intended too in most situations 
      echo 1;
      return 1;
    } else {
       echo 0;
       return 0;
    }

}

I'd use the strcmp function for string to keep out of confusion but feel free to use any method. You can use 2 equal signs but use it with caution.

And just out of curiosity, why don't you just return boolean values?