用PHP打印关联数组的结果感到困惑 [英] Confused by result of printing associative array with PHP

查看:62
本文介绍了用PHP打印关联数组的结果感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 foreach 从关联数组中打印SQL查询的结果.我正在尝试不同的方法,其中一种有效,而另一种却给出错误.

I'm trying to use foreach to print the results of a SQL query from an associative array. I'm trying different things and one works while the other gives an error.

我特别在问为什么 $ row [first_name] 这部分给出不同的结果.

I'm specifically asking about why this portion $row[first_name] gives different results.

此代码有效:

<?php
 include 'connect_db.php';
 $stmt = $conn->query("SELECT people.first_name, people.last_name FROM people");
 $results = $stmt->fetchAll(PDO::FETCH_ASSOC);

 foreach ($results as $row) {
    echo "<form action='/test.php' method='post'>
   <input type='text' name='first_name' value=$row[first_name]>
  <input type='submit' value='Click'> <br>
 </form>"; 
 }  
?>

但是以下代码给出了错误提示:注意:使用未定义的常量first_name-假定为"first_name"

But the following code gives an error saying: Notice: Use of undefined constant first_name - assumed 'first_name'

<?php
 include 'connect_db.php';
 $stmt = $conn->query("SELECT people.first_name, people.last_name FROM people");
 $results = $stmt->fetchAll(PDO::FETCH_ASSOC);

 foreach ($results as $row) {
 echo $row[first_name]; 
 }  
?>

在第二个示例中,我显然错误地使用了 $ row [first_name] ,但我不知道该怎么做.

I'm obviously using $row[first_name] incorrectly in the 2nd example but I can't figure out how.

推荐答案

使用未定义的常量时,PHP将自动假定使用了预期的字符串. [原文]

When using an undefined constant, PHP will automatically assume a string was intended. [sic]

PHP假设您的意思是常量本身的名称,就像你把它叫做一个字符串

PHP assumes that you mean the name of the constant itself, just as if you called it as a string

示例: https://3v4l.org/C7PgT

如PHP 7.x中所述,它将导致错误,而不是未来版本中的警告,因此应避免使用它,而应使用带引号的字符串

As noted in the PHP 7.x, it will result in an ERROR as opposed to a warning in future versions, so its use should be avoided in favor of using quoted strings

为什么 $ foo [bar] 错误:

始终在字符串文字数组索引周围使用引号.例如, $ foo ['bar'] 是正确的,而 $ foo [bar] 是不正确的.原因是此代码具有未定义的常量( bar )而不是字符串('bar'-注意引号).如果没有定义的名为 bar 的常量,则PHP将替换字符串'bar'并使用它.

Always use quotes around a string literal array index. For example, $foo['bar'] is correct, while $foo[bar] is not. The reason is that this code has an undefined constant (bar) rather than a string ('bar' - notice the quotes). if there is no defined constant named bar, then PHP will substitute in the string 'bar' and use that.

这并不意味着总是引用密钥.不要用引号作为常量或变量的键,因为这将阻止PHP解释它们.

This does not mean to always quote the key. Do not quote keys which are constants or variables, as this will prevent PHP from interpreting them.

在双引号上下文("$ var" )中使用它时,PHP的词法分析器自动使用方括号( $ var [text] )中的文本作为字符串.

When you used it within a double-quoted context ("$var"), PHP's lexer automatically used the text within the brackets ($var[text]) as a string.

在使用双引号内的数组或对象时,PHP的词法分析器也存在问题.

Also PHP's lexer has issues when using array or objects within double quotes.

echo "value=$row['first_name']"; //results in a syntax error

示例: https://3v4l.org/K6fUd

要解决此问题,您需要使用大括号将数组包装起来

To resolve this issue you would need to wrap your array with curly-brackets

echo "value={$row['first_name']}";

示例: https://3v4l.org/aQ1sJ

相反,我强烈建议所有PHP输出都遵循单引号,以免您不得不在不同的语法和HTML语法统一性之间进行读取.

Instead I highly suggest conforming to single quotes for all PHP output to save you from having to read between different syntax and HTML syntax uniformity.

foreach ($results as $row) {
    echo '<form action="/test.php" method="post">
   <input type="text" name="first_name" value="' . $row['first_name'] . '">
  <input type="submit" value="Click"><br>
 </form>'; 
 }  

或者使用 HEREDOC 或NOWDOC取决于所需的输出.

Alternatively using HEREDOC or NOWDOC depending on desired output.

foreach ($rows as $row) {
    echo <<<EOT
value="{$row['first_name']}"
EOT;

}

示例(包括HEREDOC): https://3v4l.org/7K5ap

Example (including HEREDOC): https://3v4l.org/7K5ap

我的首选方法是确保所有HTML语法都直接输出或输出到输出缓冲区.由于它允许大多数IDE(例如PHPStorm)自动将其区分为HTML而不是PHP代码,以确保HTML语法有效并与其他标记匹配.

My preferred method is to ensure all HTML syntax is output directly or to the output buffer. As it allows for most IDE's, such as PHPStorm, to automatically distinguish it as HTML and not PHP code, to ensure HTML syntax is valid and matches with other tags.

<?php foreach ($results as $row) { ?>
<form action="/test.php" method="post">
    <input type="text" name="first_name" value="<?php echo $row['first_name']; ?>">
    <input type="submit" value="Click"><br>
</form>
<?php } ?>

这篇关于用PHP打印关联数组的结果感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆