【问题标题】:FPDF Variable in header function标题函数中的 FPDF 变量
【发布时间】:2017-12-14 19:08:33
【问题描述】:

我正在尝试将变量放入 FPDF 的标头函数中。我知道这是一个范围界定问题,但我不确定如何传入变量 $branch

$branch = $_POST['branch'];

class PDF extends FPDF
{
  function Header()
  {
  $this->Cell(150);
  $this->Cell(30,10,$branch,0,0,'C');
  }
}

【问题讨论】:

    标签: php variables scope fpdf


    【解决方案1】:

    你有$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'])
    

    【讨论】:

    • 我在课外有我的全球 - 感谢您的回答 1+
    【解决方案2】:

    试试:

    $GLOBALS["branch"] = $_POST['branch'];
    

    在你的函数中:

    $this->Cell(30,10,$GLOBALS["branch"],0,0,'C');
    

    【讨论】:

      【解决方案3】:

      我用这样的方法解决它

      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");
      
      

      希望对你有帮助!

      【讨论】:

      • 最好的解决方案。谢谢!
      【解决方案4】:
      $branch = "branch name";
      
      $this->Cell(30,10,'.$branch.',0,0,'C');
      

      使用 PHP 连接运算符 (.),然后传递变量。

      【讨论】:

        【解决方案5】:
        $branch = 'XYZ';
        
        $GLOBALS["branch"] = $branch;
        
        function Header($branch) { $this->Cell(30,10,$branch,0,0,'C'); }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-06-13
          • 1970-01-01
          • 2012-03-17
          • 2011-08-12
          • 2012-03-11
          • 2021-11-03
          相关资源
          最近更新 更多