比较变量 PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6239766/
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 23:44:26  来源:igfitidea点击:

Compare variables PHP

phpstringvariablescompare

提问by Harigntka

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"

并检查 url 中是否存在 $_GET[] 变量,它会像这样"

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

回答by John

    $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:

供您参考,当我第一次启动 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

回答by Katsuke

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

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

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.

$_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';
}

Sources:

资料来源:

http://ca.php.net/isset

http://ca.php.net/isset

http://ca.php.net/array_key_exists

http://ca.php.net/array_key_exists

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

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

回答by Niklas

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

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

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

回答by Niklas

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:

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

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

回答by RobertPitt

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

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

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

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