在HTML中使用带有PHP脚本的参数

时间:2014-10-17 21:05:11

标签: php

如何将参数传递给php脚本:

<?php require '../scripts/listPictures.php'; ?>

在最后添加我的参数会导致整个脚本中断,并且在argv上使用vardump只会返回null。

3 个答案:

答案 0 :(得分:0)

我认为这样做的好方法是使用一个类,然后像这样添加所有参数:

class View
{
    private $partial;
    private $data;

    function __construct($partial, $data = array())
    {
        $this->partial = $partial;
        $this->data = $data;
    }

    function __get($name)
    {
        return (isset($this->data[$name]))
            ? $this->data[$name]
            : null;
    }

    function render()
    {
        ob_start();
        include($this->partial);
        return ob_get_clean();
    }
}

然后你可以这样调用你的观点:

$view = new View("../scripts/listPictures.php", array("name" => "Ghilliedrone"));

以下是listPictures.php的内容:

<h1>Hello ! My name is <?php echo $this->name; ?> !</h1>

您还可以使用echo直接显示视图:

<?php echo new View("../scripts/listPictures.php", array("name" => "Ghilliedrone")); ?>

答案 1 :(得分:0)

您不会发送参数,因为include / require会运行脚本,就好像您只是在那个地方编写它一样。

那么只需检查脚本需要哪些参数(会话中的某些内容?请求中的某个变量?可能是变量?)并在需要之前设置它。

更好的方法是将listPictures.php中的任何内容转换为函数,以便实际发送参数

Alban的回复太多了。创建一个类,实例化它,调用它,仅用于一些简单的任务= p我要在其中创建一个函数,因为它更容易,更清晰,更快(要做和要运行php)并且使用更少存储器

答案 2 :(得分:0)

如果您没有在listPictures.php文件中声明类似函数和类的内容,则可以执行此操作:

function listPictures($arg1, $arg2) {
    return require '../scripts/listPictures.php';
}