我试图将变量放入FPDF的头函数中。我知道这是一个范围问题,但我不确定如何传递变量$ branch
$branch = $_POST['branch'];
class PDF extends FPDF
{
function Header()
{
$this->Cell(150);
$this->Cell(30,10,$branch,0,0,'C');
}
}

答案 0 :(得分:1)
$branch = "branch name";
$this->Cell(30,10,'.$branch.',0,0,'C');
使用PHP串联运算符(.
),然后传递变量。
答案 1 :(得分:0)
你有$branch
课外...
您可以在功能中加入global $branch;
来访问它。
function Header()
{
global $branch;
$this->Cell(150);
$this->Cell(30,10,$branch,0,0,'C');
}
但是,以$branch
作为参数调用函数会更好。
function Header($branch)
{
$this->Cell(150);
$this->Cell(30,10,$branch,0,0,'C');
}
// $pdf->Header($_POST['branch'])
答案 2 :(得分:0)
尝试:
$GLOBALS["branch"] = $_POST['branch'];
并在您的函数中:
$this->Cell(30,10,$GLOBALS["branch"],0,0,'C');
答案 3 :(得分:0)
$ branch ='XYZ';
$ GLOBALS [“ branch”] = $ branch;
函数头($ branch) {$ this-> Cell(30,10,$ branch,0,0,'C');}
答案 4 :(得分:0)
我用类似的方法解决它
class pdf extends FPDF {
public $custom;
public function __construct($custom) {
parent::__construct();
$this->custom = $custom;
}
function Header() {
$this->Cell(190, 10, $this->custom, 1,1,'C');
}
}
$pdf = new pdf("Hello World");
希望有帮助!