Javascript 函数选项中的PHP变量

Javascript 函数选项中的PHP变量,javascript,php,arrays,options,Javascript,Php,Arrays,Options,我想使用以下FPDF扩展来对一些PDF进行密码保护 function SetProtection($permissions=array(), $user_pass='ABC123', $owner_pass='ABC123!') 但是,我希望$user_pass是上面定义的变量,例如出生日期。 到目前为止,我尝试过的选项包括 function SetProtection($permissions=array(), $user_pass=''.$dateofbirth, $owner_pass=

我想使用以下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。”
的输入方式与此完全相同

我正在努力找到正确的语法

谢谢


Henry

您不能将变量用作默认值。要达到相同效果,最接近的方法是手动检查并替换参数变量,例如

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;
    }

    ...
}

这是php还是javascript?为什么它被标记为javascript?您不能在函数声明中计算表达式。您将参数定义与向函数中传递参数混为一谈。您应该将出生日期传递到函数调用中。也许要学习函数教程@Taplar从连接的角度来看,PHP.旁白:使用d.o.b.作为密码不是最好的主意。
$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;
        }

        ...
    }