简体   繁体   English

我们如何将输入类型分配给变量

[英]how can we assign input type in to an variable

Here I am having an doubt,i know it doesn't look relevant to you people but i need to get it. 在这里,我有一个疑问,我知道它看起来与你们没有关系,但我需要得到它。 The thing is can we assign an input type to an variable like this. 关键是我们可以将输入类型分配给这样的变量。

<?php  $option  = '<input type="text" name="project_id" id="pr_id"/>';
var_dump($option);?>

Here pr_id contains an value and a thinking to assign it to an variable pr_id包含一个值和将其分配给变量的想法

Sure, you can assign anything! 当然,您可以分配任何东西! Here's an Input object I just created, have a look at this: 这是我刚刚创建的Input对象,请看一下:

<?php

class Input
{
    /** @var array $attributes */
    private $attributes = [];

    /**
     * @param $key
     * @return mixed|string
     */
    public function getAttribute($key)
    {
        return isset($this->attributes[$key]) ? $this->attributes[$key] : null;
    }

    /**
     * @param $key
     * @param $value
     * @return $this
     */
    public function setAttribute($key, $value)
    {
        $this->attributes[$key] = $value;
        return $this;
    }

    /**
     * @param array $attributes
     * @return $this
     */
    public function setAttributes(array $attributes)
    {
        $this->attributes = $attributes;
        return $this;
    }

    /**
     * @return array
     */
    public function getAttributes()
    {
        return $this->attributes;
    }

    public function render()
    {
        $html =  '<input ';
        foreach ($this->getAttributes() as $key => $val) {
            $html .= $key.'="'.$val.'" ';
        }
        $html .= '/>';
        return $html;
    }
}

So you can now generate an Input with the following code: 因此,您现在可以使用以下代码生成输入:

$input = new Input();
$input->setAttribute('id', 'pr_id')
      ->setAttribute('name', 'project_id')
      ->setAttribute('type', 'text');

echo $input->render();

Which outputs: 哪个输出:

<input id="pr_id" name="project_id" type="text" />

Have a play with it here: https://3v4l.org/sAiWd 在这里玩一下: https : //3v4l.org/sAiWd

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

相关问题 我们如何将数组的单个值分配给javascript中的任何变量 - how we can assign single value of array to any variable in javascript 我们如何在没有提交或单击的情况下将输入类型的值传递到变量中 - how can we pass input type value in to an variable with out submit or click 如何将输入字符串分配为变量 - How to assign input string as a variable 无法检索数字输入并将其分配给变量 - Can't retrieve numeric input and assign it to a variable 将输入表单分配给变量 - Assign input form to a variable 我们如何将浮点值输入类型传递到另一个页面以进行检索 - How can we pass floating point values input type to another page to retrieve 我们如何使用 ajax 从 PHP 的数据库中获取数据并显示在输入类型的值中? - How can we fetch data from database in PHP using ajax and display in value of input type? 如何将PHP中的输入字段值存储到数据库中,然后将其分配给javascript中的变量 - How to store the input field value in PHP into database and then assign it to variable in javascript 如何通过javascript为输入类型日期分配默认值? - How to assign default value for input type date by javascript? 如何获取每个输入文本中的值并分配给变量 - how to get values in each input text and assign to a variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM