javascript - 函数选项中的 PHP 变量
问题描述
我想使用以下 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."
输入方式与此完全相同。
我正在努力寻找正确的语法。
谢谢
亨利
解决方案
您不能将变量用作默认值。要达到相同效果,您可以做的最接近的事情是手动检查和替换参数变量,例如,
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;
}
...
}
推荐阅读
- javascript - 所有样式化的组件都返回任何 (@types/styled-components)
- javascript - 使用 Javascript 中的正则表达式正确拆分字符串
- php - 获取今天创建的关系项目?
- css - 链接的浏览器默认颜色是什么?
- firebase - Firebase 推送通知设备到设备
- laravel - laravel 作业中的“未定义属性”错误
- csv - 如何将制表符或逗号分隔的 .txt 文件转换为 .csv 文件?
- angular - 离子 SSR。使用 NgxAuthFirebaseUIModule 的页面未加载
- reactjs - FlatList 组件未呈现或更新
- javascript - 返回上一条指令js