0

我首先检查函数是否返回有效结果,然后,如果结果有效,我需要在我的脚本中使用它。但是,与其检查一次,然后再次返回函数查看其结果,有没有办法一次性完成所有操作?这$strVal是我稍后需要在查询字符串中使用的变量。

$conn = connect();

encrypt('HIs#$%.-','x');
decrypt('6507A27EB0521AFA0776F1A4F8033041','x');

//If the function returns a valid result, 
if (encrypt('Tom','x'))
{
 echo 'Success'.$strVal;//I'll use this in a querystring later.

}
else
{
 echo 'An error occurred';
}

//Encrypt string
function encrypt($strToEncrypt,$salt)
{
    global $conn;
    $elements = $conn->prepare("select hex(aes_encrypt(:what,:salt)) as encValue");
    $elements->bindParam(':what', $strToEncrypt);
    $elements->bindParam(':salt', $salt);
    $elements->execute();
    $row = $elements->fetch();
    $strVal = $row['encValue'];
    if(is_null($strVal)){return false;}else{return $strVal;}
}

//Decrypt string
function decrypt($strToDecrypt,$salt)
{
    global $conn;
    $elements = $conn->prepare("select aes_decrypt(unhex(:what),:salt) as decValue");
    $elements->bindParam(':what', $strToDecrypt);
    $elements->bindParam(':salt', $salt);
    $elements->execute();
    $row = $elements->fetch();

    if(is_null($row['decValue']))
    {echo "Null";}else{echo $row['decValue'];}
}
4

1 回答 1

2

是的,您可以在条件内进行变量赋值:

/* single 'equal' operator intended */
if( $strVal = encrypt( 'Tom', 'x' ) )
{
    echo 'Success' . $strVal;
}
于 2013-05-08T08:42:19.330 回答