PHP If Else withisset: else 部分不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11881944/
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
PHP If Else with isset: else part does not work
提问by progresser
PHP Part is hereSo when I post it, else part does not work at all (meaning: the text I set just disappear), while the other half works (meaning: I can assign variables, but can't leave it blank). I have been struggling with it all night...please help! So this is the second page, if you need to take a look at the code on the first page, please let me know!
PHP 部分在这里所以当我发布它时,else 部分根本不起作用(意思是:我设置的文本消失了),而另一半起作用(意思是:我可以分配变量,但不能留空)。我整晚都在为此苦苦挣扎……请帮忙!所以这是第二页,如果您需要查看第一页上的代码,请告诉我!
<?php
$lovers = $_POST['lovers'];
$quote = $_POST['quote'];
$color = $_POST['color'];
$font = $_POST['font'];
$imdblink = $_POST['imdblink'];
?>
<?php
if (isset($_POST['lovers']))
{
$lovers = $_POST['lovers'];
}else{
echo "P & M";
}
if (isset($_POST['quote']))
{
$quote = $_POST['quote'];
}else{
echo "I love you.";
}
if (isset($_POST['color']))
{
$color = $_POST['color'];
}else{
echo "yellow";
}
if (isset($_POST['font']))
{
$font = $_POST['font'];
}else{
echo "Futura";
}
if (isset($_POST['imdblink']))
{
$imdblink = $_POST['imdblink'];
}else{
echo "http://www.imdb.com/";
}
?>
Here is the HTML part:Is there anything wrong here? Please help!
这是 HTML 部分:这里有什么问题吗?请帮忙!
<div class="artGroup slide">
<div class="artwork"> <img src="../_images/M&P.png">
<div class="detail">
<div class="movie01c" style="font-family: <?php echo "{$font}"; ?>; font-size: 20px; color: <?php echo "{$color}";?>;">
<?php echo "{$quote}";?>
</div>
<div class="movie01t"><a href="<?php echo "{$imdblink}";?>">
<?php echo "{$lovers}";?>
</a></div>
</div>
</div>
</div>
回答by xdazz
Instead of using echo, use variable assignment too in the else part.
echo在 else 部分也不要使用,而是使用变量赋值。
if (isset($_POST['lovers'])) {
$lovers = $_POST['lovers'];
}else{
$lovers = "P & M";
}
回答by jeff
isset() tells you if the variable exists. If you have a form (which you didn't show the html for), and someone leaves the field blank, the field exists but is empty. There is no way that the form element will not exist after the form is submitted, so isset() will never return FALSE. So you want to use !empty() instead of isset().
isset() 告诉你变量是否存在。如果您有一个表单(您没有显示其 html),并且有人将该字段留空,则该字段存在但为空。表单提交后表单元素不可能不存在,因此isset()永远不会返回FALSE。所以你想使用 !empty() 而不是isset()。
回答by N4553R
ternary operator will make it really clearer.
三元运算符会让它更清晰。
for instance:
例如:
$imdblink = ((isset($_POST['imdblink'])) ? ($_POST['imdblink']) : ('http://www.imdb.com/'));
furthermore, you should define your default values, and use variable variable. (here an example not using define)
此外,您应该定义默认值,并使用可变变量。(这里是一个不使用定义的例子)
$vars = array('imdblink' => 'http://www.imdb.com/');
foreach ($vars as $varName => $defaultVal)
$$varName = ((isset($_POST[$varName])) ? ($_POST[$varName]) : ($defaultVal));
回答by Ashwini Agarwal
your statements should be like...
你的陈述应该像......
if (isset($_POST['lovers']) && ($_POST['lovers'] != ''))
{
$lovers = $_POST['lovers'];
}
else
{
$lovers = "P & M";
}
回答by Tac Tacelosky
You don't want to echo, you want to assign it, then you'll print it in your other PHP script.
你不想回显,你想分配它,然后你会在你的其他 PHP 脚本中打印它。
I think you're trying to set default values if it doesn't come in the $_POST array. Try
如果 $_POST 数组中没有它,我认为您正在尝试设置默认值。尝试
$color = isset($_POST['color']) ? $_POST['color'] : 'yellow';
If you never want a blank color, use
如果您从不想要空白颜色,请使用
$color = !empty($_POST['color']) ? $_POST['color'] : 'yellow';
回答by Moak
This should work
这应该工作
<?php
$lovers = isset($_POST['lovers']) ? $_POST['lovers'] : "P & M" ;
$quote = isset($_POST['quote'])? $_POST['quote'] : "I Love You";
$color = isset($_POST['color'])? $_POST['color'] : "yellow";
$font = isset($_POST['font']) ? $_POST['font'] : "Futura";
$imdblink = isset($_POST['imdblink'])? $_POST['imdblink']: "http://www.imdb.com/";
?>
you can replace issetwith !emptyif you want to make sure the field is not ... empty
您可以替换isset使用!empty,如果你想确保字段不为空...
回答by Ali Shikhiyev
All your isset $_postfunctions might works thats because it would not come to else part.
你所有的 isset$_post函数可能都有效,因为它不会出现在其他部分。
回答by srikanth wgl
issetreturns falseonly when the variable is not set or it is set to null. it will return truewhen the variable is ''(empty string) which will be the value you get when no value is entered in that field in the first page.
isset回报false只有当变量未设置或设置为null。它将true在变量为''(空字符串)时返回,这将是您在第一页的该字段中未输入任何值时获得的值。
You need to use emptyfor this to work properly which will return truefor unset and empty vlues. It returns for falsefor non-empty values. You need to prepend it with !(not operator) to use it here.
您需要使用empty它才能正常工作,这将返回未true设置和空的 vlues。它false为非空值返回。您需要在它前面加上!(不是运算符)才能在此处使用它。
And you are using variable assignment in ifand echoin else. I think that is what the cause of the problem. Change that echoto assignment.
并且您在 inif和echoin中使用变量赋值else。我认为这就是问题的原因。将其更改echo为分配。
if (!empty($_POST['lovers']))
{
$lovers = $_POST['lovers'];
}else{
$lovers = "P & M";
}
Don't forget !.
不要忘记!。
回答by user6765496
Here is an idea. Declare the variable and value first. If the $_POST['lovers']is not nullthen change the variable value:
这是一个想法。首先声明变量和值。如果$_POST['lovers']不是,null则更改变量值:
$lovers = "P & M";
if (isset($_POST['lovers']))
{
$lovers = $_POST['lovers'];
}
echo $lovers;

