PHP数组$item[id] $item['id'] $item["id"]有什么区别呢?

WBOY
Release: 2016-06-06 20:32:29
Original
1595 people have browsed it

<code><?php echo anchor("admin/article/delete/$item[id]", '删除','删除');?>   //正确
<?php echo anchor("admin/article/delete/$item['id']", '删除','删除');?>  //报错
</code>
Copy after login
Copy after login
Copy after login
Copy after login

这是为什么?

<code><a href="Article/edit/<?php%20echo%20%24item%5B'id'%5D?>">修改</a>   //正确
<a href="Article/edit/<?php%20echo%20%24item%5Bid%5D?>">修改</a>    //错误
</code>
Copy after login
Copy after login
Copy after login
Copy after login

有人能解释下吗?

回复内容:

<code><?php echo anchor("admin/article/delete/$item[id]", '删除','删除');?>   //正确
<?php echo anchor("admin/article/delete/$item['id']", '删除','删除');?>  //报错
</code>
Copy after login
Copy after login
Copy after login
Copy after login

这是为什么?

<code><a href="Article/edit/<?php%20echo%20%24item%5B'id'%5D?>">修改</a>   //正确
<a href="Article/edit/<?php%20echo%20%24item%5Bid%5D?>">修改</a>    //错误
</code>
Copy after login
Copy after login
Copy after login
Copy after login

有人能解释下吗?

<code><a href="Article/edit/<?php%20echo%20%24item%5B'id'%5D?>">修改</a>   //正确
<a href="Article/edit/<?php%20echo%20%24item%5Bid%5D?>">修改</a>    //错误
</code>
Copy after login
Copy after login
Copy after login
Copy after login

这个$item是变量,而id是字符串,所以要引号。

<code><?php echo anchor("admin/article/delete/$item[id]", '删除','删除');?>   //正确
<?php echo anchor("admin/article/delete/$item['id']", '删除','删除');?>  //报错
</code>
Copy after login
Copy after login
Copy after login
Copy after login

这个$item是字符串,id也是字符串,组合起来就是$item[id],如果id加引号,就等于"{'id'}"。

<code>"$item[id]" == $item['id']
"item['id']" == $item["'id'"]
</code>
Copy after login

原因是字符串"$item"在引号会转义成变量$item,而$item里面的键名id是不会被转义的,所以不必要双重引号。

<code><a href="Article/edit/<?php%20echo%20%24item%5B'id'%5D?>">修改</a>   //正确
<a href="Article/edit/<?php%20echo%20%24item%5Bid%5D?>">修改</a>    //错误
</code>
Copy after login
Copy after login
Copy after login
Copy after login

下面这个错误没问题,不带引号的字符下标id会当成一个常量,而你应该没有定义这个常量

<code><?php echo anchor("admin/article/delete/$item[id]", '删除','删除');?>   //正确
<?php echo anchor("admin/article/delete/$item['id']", '删除','删除');?>  //报错
</code>
Copy after login
Copy after login
Copy after login
Copy after login

你可以这样试试:

<code><?php echo anchor("admin/article/delete/$item[id]", '删除','删除');?>   //正确
<?php echo anchor("admin/article/delete/{$item['id']}", '删除','删除');?>  //报错
</code>
Copy after login

<code><?php $arr['name'] = 'lamp';
echo $arr['name'];     //正确
echo "$arr['name']";   //报错
echo "$arr[name]";     //正确
echo "{$arr['name']}"; //正确
</code></code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template