【问题标题】:PHP Variable in function options函数选项中的 PHP 变量
【发布时间】:2021-03-10 15:29:14
【问题描述】:

我想使用以下 FPDF 扩展来密码保护某些 PDF

function SetProtection($permissions=array(), $user_pass='ABC123', $owner_pass='ABC123!')

但是我希望 $user_pass 是上面定义的变量,例如出生日期。 到目前为止我尝试过的选项包括

function SetProtection($permissions=array(), $user_pass=''.$dateofbirth, $owner_pass='ABC123!')
function SetProtection($permissions=array(), $user_pass=$dateofbirth, $owner_pass='ABC123!')
function SetProtection($permissions=array(), $user_pass='".$dateofbirth."', $owner_pass='ABC123!')

但是我通常会遇到同样的问题,即密码基本上是 '' 中任何内容的纯文本,而不是变量,例如,在最后一种情况下,密码是 ".$dateofbirth." 输入的完全一样。

我正在努力寻找正确的语法。

谢谢

亨利

【问题讨论】:

  • 这是 php 还是 javascript?为什么它被标记为javascript?
  • 你不能在函数声明中计算表达式。您将参数定义与将参数传递到函数中混合在一起。您应该将该出生日期传递给函数调用。也许通过函数教程? @Taplar 从串联判断,PHP。
  • 旁白:有一个d.o.b。作为密码不是最好的主意。

标签: javascript php arrays options


【解决方案1】:

您不能使用变量作为默认值。您可以做的最接近的事情是手动检查和替换参数变量,例如,

function SetProtection($permissions=array(), $user_pass=null, $owner_pass='ABC123!')
{
    // You can also check if empty string if you have a use-case where empty string
    // would also mean replace with $dateofbirth
    if ($user_pass === null) {
        global $dateofbirth;
        $user_pass = $dateofbirth;
    }

    ...
}
$SetProtection = function ($permissions=array(), $user_pass=null, $owner_pass='ABC123!') use ($dateofbirth)
{
    // You can also check if empty string if you have a use-case where empty string
    // would also mean replace with $dateofbirth
    if ($user_pass === null) {
        $user_pass = $dateofbirth;
    }

    ...
}
class SomeClass()
{
    private $dateofbirth;

    ...

    function SetProtection($permissions=array(), $user_pass=null, $owner_pass='ABC123!')
    {
        // You can also check if empty string if you have a use-case where empty string
        // would also mean replace with $dateofbirth
        if ($user_pass === null) {
            $user_pass = $this->dateofbirth;
        }

        ...
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    • 2012-07-10
    相关资源
    最近更新 更多