我首先检查函数是否返回有效结果,然后,如果结果有效,我需要在我的脚本中使用它。但是,不是一次检查,而是再次回到函数的结果,有没有办法一次性完成所有操作?这里,$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'];}
}
答案 0 :(得分:2)
是的,您可以在条件中执行变量赋值:
/* single 'equal' operator intended */
if( $strVal = encrypt( 'Tom', 'x' ) )
{
echo 'Success' . $strVal;
}