将 PHP 变量作为文本框值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15357125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:05:42  来源:igfitidea点击:

Putting a PHP variable as a text box value

phpvariablestextbox

提问by stark

I am trying to write code that passes a php variable as a textboxes value. Here is the following code I tried:

我正在尝试编写将 php 变量作为文本框值传递的代码。这是我尝试过的以下代码:

echo "<td>"."<input type='text' value='<?php echo $start_date; ?>'/>"."</td>"

This brings up an error : Parse error: syntax error, unexpected T_ECHO, expecting ',' or ';'

这带来了一个错误:解析错误:语法错误,意外的 T_ECHO,期待 ',' 或 ';'

I have tried various methods of re-wording:

我尝试了各种重新措辞的方法:

 echo "<input type='text' value='<?php echo $start_date?>'/>"; 

(this was purely to test as I would like the result in a table row)

(这纯粹是为了测试,因为我希望结果在表格行中)

but this shows:

但这表明:

<?php echo ?> 

in the textbox and I also get this error: Notice: Undefined variable: start_date...

在文本框中,我也收到此错误:注意:未定义变量:start_date...

Thanks in advance for any help.

在此先感谢您的帮助。

回答by Anirudh Ramanathan

You don't need tostart one echowithin another. The variable $start_datecan be within double quotes and hence interpolated.

不需要echo另一个开始。变量$start_date可以在双引号内,因此可以进行插值。

echo "<td><input type='text' value='$start_date'/></td>"; //no unnecessary concatentation

EDIT:

编辑:

In case of an associative array, for example, to echo $row['start_date']

关联数组的情况下,例如,回显$row['start_date']

echo "<td><input type='text' value='".$row['start_date']."/></td>";

回答by Suresh Kamrushi

You can try this-

你可以试试这个——

echo "<td>"."<input type='text' value='$start_date;'/>"."</td>";

回答by Suresh Kamrushi

you have to echo once, for example:

你必须回声一次,例如:

echo "<td>"."<input type='text' value='$start_date'/>"."</td>";

回答by Sgarz

Use this:

用这个:

echo '<td><input type="text" value="'.$start_date.'"/></td>';

回答by Rohit Kumar Choudhary

You should always took thin in your mind that if you enclose a variable inside "" double quotes than it can be echoed directly. You don't need use concatenation for this only. Eg

您应该始终牢记,如果将变量括在 "" 双引号内,则可以直接回显。您不需要仅为此使用连接。例如

echo "<td>$rohit</td>"; //here you dont have to echo this like "<td>".$rohit."</td>".

In this as @Cthulhu said use it like that.

正如@Cthulhu所说的那样使用它。