比较变量 PHP

发布于 2024-11-13 10:13:22 字数 236 浏览 2 评论 0 原文

我如何比较两个变量字符串,是不是像这样:

$myVar = "hello";
if ($myVar == "hello") {
//do code
}

并检查 url 中是否存在 $_GET[] 变量,是不是像这样”

$myVars = $_GET['param'];
if ($myVars == NULL) {
//do code
}

How can I compare two variable strings, would it be like so:

$myVar = "hello";
if ($myVar == "hello") {
//do code
}

And to check to see if a $_GET[] variable is present in the url would it be like this"

$myVars = $_GET['param'];
if ($myVars == NULL) {
//do code
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

恏ㄋ傷疤忘ㄋ疼 2024-11-20 10:13:22
$myVar = "hello";
if ($myVar == "hello") {
    //do code
}

$myVar = $_GET['param'];
if (isset($myVar)) {
    //IF THE VARIABLE IS SET do code
}


if (!isset($myVar)) {
    //IF THE VARIABLE IS NOT SET do code
}

供您参考,我第一次启动 PHP 时困扰了我好几天的事情:

$_GET["var1"] // these are set from the header location so www.site.com/?var1=something
$_POST["var1"] //these are sent by forms from other pages to the php page
$myVar = "hello";
if ($myVar == "hello") {
    //do code
}

$myVar = $_GET['param'];
if (isset($myVar)) {
    //IF THE VARIABLE IS SET do code
}


if (!isset($myVar)) {
    //IF THE VARIABLE IS NOT SET do code
}

For your reference, something that stomped me for days when first starting PHP:

$_GET["var1"] // these are set from the header location so www.site.com/?var1=something
$_POST["var1"] //these are sent by forms from other pages to the php page
一笑百媚生 2024-11-20 10:13:22

为了比较字符串,我建议使用三等号运算符而不是双等号。

// This evaluates to true (this can be a surprise if you really want 0)
if ("0" == false) {
    // do stuff
}

// While this evaluates to false
if ("0" === false) {
    // do stuff
}

为了检查 $_GET 变量,我宁愿使用 array_key_exists,如果键存在但内容为 null,isset 可以返回 false,

例如:

$_GET['param'] = null;

// This evaluates to false
if (isset($_GET['param'])) {
    // do stuff
}

// While this evaluates to true
if (array_key_exits('param', $_GET)) {
    // do stuff
}

尽可能避免进行诸如

$myVar = $_GET['param'];

$_GET 之类的分配,取决于用户。所以预期的密钥可能可用,也可能不可用。如果您访问该密钥时不可用,则会触发运行时通知。如果启用了通知,这可能会填满您的错误日志,或者在最坏的情况下向您的用户发送垃圾邮件。只需执行一个简单的 array_key_exists 即可在引用 $_GET 上的键之前检查它。

if (array_key_exists('subject', $_GET) === true) {
    $subject = $_GET['subject'];
} else {
    // now you can report that the variable was not found
    echo 'Please select a subject!';
    // or simply set a default for it
    $subject = 'unknown';
}

来源:

http://ca.php.net/isset

http://ca.php.net/array_key_exists

http://php.net/manual/en/language.types.array.php

For comparing strings I'd recommend using the triple equals operator over double equals.

// This evaluates to true (this can be a surprise if you really want 0)
if ("0" == false) {
    // do stuff
}

// While this evaluates to false
if ("0" === false) {
    // do stuff
}

For checking the $_GET variable I rather use array_key_exists, isset can return false if the key exists but the content is null

something like:

$_GET['param'] = null;

// This evaluates to false
if (isset($_GET['param'])) {
    // do stuff
}

// While this evaluates to true
if (array_key_exits('param', $_GET)) {
    // do stuff
}

When possible avoid doing assignments such as:

$myVar = $_GET['param'];

$_GET, is user dependant. So the expected key could be available or not. If the key is not available when you access it, a run-time notice will be triggered. This could fill your error log if notices are enabled, or spam your users in the worst case. Just do a simple array_key_exists to check $_GET before referencing the key on it.

if (array_key_exists('subject', $_GET) === true) {
    $subject = $_GET['subject'];
} else {
    // now you can report that the variable was not found
    echo 'Please select a subject!';
    // or simply set a default for it
    $subject = 'unknown';
}

Sources:

http://ca.php.net/isset

http://ca.php.net/array_key_exists

http://php.net/manual/en/language.types.array.php

你的往事 2024-11-20 10:13:22

如果您想检查变量是否已设置,请使用isset()

if (isset($_GET['param'])){
// your code
}

If you wanna check if a variable is set, use isset()

if (isset($_GET['param'])){
// your code
}
樱桃奶球 2024-11-20 10:13:22

要将变量与字符串进行比较,请使用以下命令:

if ($myVar == 'hello') {
    // do stuff
}

要查看变量是否已设置,请使用 isset(),如下所示:

if (isset($_GET['param'])) {
    // do stuff
}

To compare a variable to a string, use this:

if ($myVar == 'hello') {
    // do stuff
}

To see if a variable is set, use isset(), like this:

if (isset($_GET['param'])) {
    // do stuff
}
半世蒼涼 2024-11-20 10:13:22

所有这些信息都列在 PHP 网站的 Operators

http://php.net/manual 下/en/language.operators.comparison.php

All this information is listed on PHP's website under Operators

http://php.net/manual/en/language.operators.comparison.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文