php变量看起来是相同的,但它们并不完全相同


php variables seems to be the same but they are not identical

我有两个php变量:

$account_name
$my

如果我使用功能:

var_dump($account_name);

我得到:

string(192) "admin"

$account_name显示为类似于帐户名的链接

如果我使用功能:

var_dump($my);

我得到:

string(5) "admin"

如何更改返回我唯一"admin"字符串的变量$account_name

我有问题,因为如果我在SQL查询中使用$account_name,什么都不会发生,但如果我使用$my,它就可以工作。

您的$account_name可能包含html标记。由于您在浏览器中查看它,因此它被处理为html。您需要从这些标记中提取值。

看看这篇文章。

尝试:

$account_name = strip_tags($account_name);

strip_tag引用

您的字符串可能包含不可读的字符。尝试使用以下示例剥离字符串:

var_dump(preg_replace("/[^('x20-'x7F)]*/", '', $account_name));

如果它工作,它应该返回:string(5) "admin"

如何设置变量?

如果你是从这样的表格中得到的:

<form action="/" method="get">
    <input name="username" placeholder="Username">
    <button>click me</button>
</form>

您应该简单地收集输入的值,提取php中的值

<?php $account_name = $_GET["username"]; echo $account_name; ?>

在某些情况下,您可能希望去掉变量条($_GET["username"](;删除字符串前后的空格