无法使用 PHP 获取函数返回值


Can not get the function returning value using PHP

我需要一個幫助。我无法使用 PHP 获取函数返回的值。我在下面解释我的代码。

<?php
  function encrypt($id){
    $key=md5('onlinepharmacy', true);
    $id = base_convert($id, 10, 36); // Save some space
    $data = mcrypt_encrypt(MCRYPT_BLOWFISH, $key, $id, 'ecb');
    $data = bin2hex($data);
    return $data;
}
?>
<a href="health.php?h_i=<?php encrypt(12) ?>" class="detall">Go for Details</a>

从上面的代码中,我没有得到h_i value.it是空白的。我需要设置查询字符串值(h_i=)但无法获取。请帮助我解决此问题。

当你从函数返回值时,你需要使用 echo() 来显示它。 在您的情况下

如果你不想在HTML中使用echo,那么你必须从函数中echo()值而不是返回它。

function encrypt($id){
    $key=md5('onlinepharmacy', true);
    $id = base_convert($id, 10, 36); // Save some space
    $data = mcrypt_encrypt(MCRYPT_BLOWFISH, $key, $id, 'ecb');
    $data = bin2hex($data);`enter code here`
    echo $data;
}
<a href="health.php?h_i=<?php encrypt(12) ?>" class="detall">Go for Details</a>

end 语句语法缺失,应将返回值分配给变量,以防再次使用它。使更新和扩展变得容易的良好做法。

请参阅下面的工作代码;

function encrypt($id){
    $key=md5('onlinepharmacy', true);
    $id = base_convert($id, 10, 36); // Save some space
    $data = mcrypt_encrypt(MCRYPT_BLOWFISH, $key, $id, 'ecb');
    $data = bin2hex($data);`enter code here`
    return $data;
}
<a href="health.php?h_i=<?php echo encrypt(12); ?>" class="detall">Go for Details</a>