简体   繁体   English

如何使用参数调用另一个函数中的函数-PHP类

[英]How to call a function in another function with arguments - PHP class

The following code works fine. 以下代码可以正常工作。 In the class file, there are four arguments for the function setRowCol($r,$c, $v, $pa) in which $pa is an argument for padding which is passed inside the function startCol($p). 在类文件中,函数setRowCol($ r,$ c,$ v,$ pa)有四个参数,其中$ pa是用于填充的参数,该参数在函数startCol($ p)内传递。 But it doesn't work when i set padding like the following 但是当我如下设置填充时它不起作用

public function startCol($p) {
            $tab= "<td Style='border:4px solid black; padding:'".$p."'px;>";                
            return $tab;
        }

and works fine if I give the value directly like 如果我直接给值,就像

$tab= "<td Style='border:4px solid black; padding:110px;>";             

But I would like to keep padding value as an argument. 但我想保留填充值作为参数。 Is there a solution for this? 有解决方案吗?

Full Class File 全班文件

class CreateTable {

    public $rows;
    public $cols;
    public $val = array();
    public $pad;

    public function setRowCol($r,$c, $v, $pa) {
        $this->rows = $r;
        $this->cols = $c;
        $this->val = $v;
        $this->pad = $pa;       
        echo $this->startTable();
        for($i=0; $i<$this->rows && $i<sizeof($this->val); $i++) {
            echo $this->startRow();         
            for($j=0; $j<$this->cols && $j<sizeof($this->val); $j++) {
                echo $this->startCol($this->pad);           
                echo $this->val[$j];
                echo $this->endCol();           
        }
        echo $this->endRow();
        }
        echo $this->endTable();
    }

    function startTable() {
        $tab = "<table Style='border:1px solid black';>";               
        return $tab;
    }

    function endTable() {       
        $tab= "</table>";
        return $tab;
    }

    function startRow() {       
        $tab= "<tr Style='border:1px solid black';>";               
        return $tab;
    }

    function endRow() {             
        $tab= "</tr>";      
        return $tab;
    }

    public function startCol($p) {                          
        $tab= "<td Style='border:4px solid black; padding:'".$p."'px;>";                
        return $tab;
    }
    function endCol() {                         
        $tab= "</td>";      
        return $tab;
    }
}
?>

File 文件

<?php

include "CreateTable.php";

$arr = array("Jan", "feb","mar", "apr");

$tab = new CreateTable();
echo $tab->setRowCol(3,10, $arr, 110);

?>

When you do this: 执行此操作时:

$tab= "<td Style='border:4px solid black; padding:110px;>"; 

The result is this: 结果是这样的:

<td Style='border:4px solid black; padding:110px;>

Which is technically invalid because it's missing a closing quote at the end of an attribute, but the browser is probably correcting that for you when rendering. 从技术上讲,这是无效的,因为它在属性的末尾缺少了右引号,但是浏览器在渲染时可能会为您更正该引号。

But when you do this: 但是,当您这样做时:

$tab= "<td Style='border:4px solid black; padding:'".$p."'px;>";

The result is this: 结果是这样的:

<td Style='border:4px solid black; padding:'100'px;>

Which is much more invalid and probably confusing the browser. 这更加无效,并且可能会使浏览器混乱。

I don't know why you put those single-quotes in there, but they produce invalid markup. 我不知道为什么将那些单引号放在其中,但是它们会产生无效的标记。 Don't just look at the rendered page when debugging, actually view the source which was returned by the server. 调试时不仅要查看呈现的页面,还要查看服务器返回的源。

Basically, don't add the extra quotes, and do add the closing quote: 基本上,不要添加多余的引号,而要添加结束引号:

$tab= "<td Style='border:4px solid black; padding:".$p."px;'>";

Or, even better, use double-quotes in the markup since that's what HTML technically calls for: 或者,甚至更好的是,在标记中使用双引号,因为这是HTML技术上的要求:

$tab= '<td Style="border:4px solid black; padding:'.$p.'px;">';

Consider the following approach: 考虑以下方法:

public function startCol($p = 0) {
        $pad = (!$p)? $this->pad : $p; 
        $tab= "<td Style='border:4px solid black; padding:".intval($pad)."px;'>";
        return $tab;
}

If the argument was not passed in then use the internal $this->pad value 如果未传递参数,则使用内部$this->pad

You have one too many ' and one ' is misplaced. 您有太多'和一个'被放错了位置。 Try like this: 尝试这样:

public function startCol($p) {
        $tab= "<td Style='border:4px solid black; padding:" . $p . "px;'>";                
        return $tab;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM