php中的连接与条件
我正在学习PHP,我试图在设置$autocollant\u total\u ht\u自定义时显示一个欧元 这是我写的: 但是,即使未设置$autocollant\u total\u ht\u custom,我的欧元也始终显示 我花了75分钟在这上面,尽管做了研究,还是一次又一次地尝试失败 我也试过了!是空的!是空的,结果相同 我相当肯定我的逻辑没有错,但方法是正确的 有人来营救吗 祝大家周六愉快 迈克 编辑1: 一点视觉辅助 我的目标是仅在单元格中确实存在某些内容时才显示单元格的内容。默认情况下,我可以在空单元格中看到0 我知道我的代码必须看起来很原始,但它是有效的,我不认为它与我们在最初问题中试图实现的目标有冲突 然后,如所问,这就是我在表行和表数据中所写的内容:php中的连接与条件,php,if-statement,conditional-statements,concatenation,Php,If Statement,Conditional Statements,Concatenation,我正在学习PHP,我试图在设置$autocollant\u total\u ht\u自定义时显示一个欧元 这是我写的: 但是,即使未设置$autocollant\u total\u ht\u custom,我的欧元也始终显示 我花了75分钟在这上面,尽管做了研究,还是一次又一次地尝试失败 我也试过了!是空的!是空的,结果相同 我相当肯定我的逻辑没有错,但方法是正确的 有人来营救吗 祝大家周六愉快 迈克 编辑1: 一点视觉辅助 我的目标是仅在单元格中确实存在某些内容时才显示单元格的内容。默认情况下
<tr>
<td class=table_align_left>A partir de 100</td>
<td><?php echo $autocollant_prix ?></td>
<td><?php echo $autocollant_custom?></td>
<td><?php echo $autocollant_total_ht_custom?> </td>
</tr>
简言之,我试图不显示任何东西,如果没有显示当前有效的值,然后在变量后添加一个欧元,有东西要显示
编辑2:
我的原始代码:
编辑3:
$autocollant\u total\u ht\u自定义已在本声明前面显示:
如果有帮助的话,我发布了我的代码
迈克。你好。看起来您缺少端部支架
if (isset($autocollant_total_ht_custom)) {
$autocollant_total_ht_custom = $autocollant_total_ht_custom . $euro;
} else {
echo " ";
}
也许是这样的?很难理解你的确切要求
IsSet检查变量是否设置为某个值如果它不为null,那么它通过了测试,如果你在这个变量上操作字符串,那么它将永远不会为null,这意味着欧元符号将始终显示
如果变量为null,则条件测试失败,点击else,不回显null字符串
如果您可以使用您希望设置的$autocollant\u total\u ht\u custom更新您的答案,我可以提供更好的帮助
编辑:
在我看来,你可以简化你所做的,基本上我们只关心在有设置的情况下回显字符串,否则做任何事情都没有意义,所以你的检查可以像
$autocollant_total_ht_lot10 = null;
$autocollant_total_ht_lot11 = "";
$autocollant_total_ht_custom = "1,000";
$euro = "€";
if (isset($autocollant_total_ht_custom))
{
echo 'ht custom';
echo TDFromString($autocollant_total_ht_custom, $euro);
}
//notice this doesnt output anything because it isnt set
if (isset($autocollant_total_ht_lot10, $euro))
{
echo 'lot 10';
echo TDFromString($autocollant_total_ht_lot10, $euro);
}
//notice this does because the string, while empty is something that isnt null
if (isset($autocollant_total_ht_lot11))
{
echo 'lot 11';
echo TDFromString($autocollant_total_ht_lot11, $euro);
}
//lets set it to null and see what happens
$autocollant_total_ht_lot11 = null;
if (isset($autocollant_total_ht_lot11))
{
echo 'lot 11 AGAIN';
echo TDFromString($autocollant_total_ht_lot11, $euro);
}
//it doesnt get printed!
//create a function that takes the string in question,
//and for the sake of your use case also the currency to output,
//that way you could change 'euro' to 'currency'
//and have the sign change based on what the value of the $currency
//string is, eg $currency = "£"
function TDFromString($string, $currency)
{
return '<td>' . $string . ' ' .$currency . '</td>';
}
实例:
一个更明确的例子:
我添加了一个额外的回音来指示和换行哪一个变量正在被打印出来,当然你不需要它
我要指出的是,函数名是坏函数名的一个很好的例子,因为它不仅在字符串周围返回一个td,而且还插入了货币,您可能希望将其命名得更好一些:
编辑:
最后一次编辑超出了您的问题范围,您应该考虑将数据保存在数组中,然后处理它们
使用前面的示例,我们可以将代码简化为这样
$autocollant_total_ht_lot10 = null;
$autocollant_total_ht_lot11 = "";
$autocollant_total_ht_lot12 = "2,0000000";
$autocollant_total_ht_custom = "1,000";
$euro = "€";
//create an array, and stick all our strings in it, from now, if we need to do something to one of the strings(or all!), we do it through the array
$arrayofLots = array($autocollant_total_ht_lot10, $autocollant_total_ht_lot11, $autocollant_total_ht_lot12, $autocollant_total_ht_custom);
//go over each array 'entry' so the first time is '$autocollant_total_ht_lot10', then '$autocollant_total_ht_lot11' etc
foreach ($arrayofLots as $lot)
{
//and we've been over this bit :)
//$lot is a variable we set so we have something to refer to for the individual array entry we are on, we could just as easily name it anything else
if (isset($lot))
{
echo TDFromString($lot, $euro);
}
}
function TDFromString($string, $currency)
{
return '<td>' . $string . ' ' .$currency . '</td>';
}
if语句很好,因此必须在代码的前面设置变量。虽然你没有分享你的结果。你能分享更多的phpSo吗?我更了解,如果Isset条件为真,你只想将$euro设置为€?不可复制你确定你要寻找的行为是由Isset提供的吗?如果一个变量被设置为null、false、zero或空字符串,它仍然被设置。如果值不是null,则isset返回true。不确定答案,请投赞成票,因为它似乎符合要求。切换顶部的$autocollant\u total\u ht\u custom以查看示例。您添加了两个大括号,在本例中不需要它们。我甚至不知道这是可能的。我无法想象在一个大型应用程序中,代码会变得多么不可读。不过很高兴知道。和平。你好,谢谢你的时间!我用更多的细节编辑了我的帖子。现在检查我的答案,希望我理解正确!我想我现在理解你的问题了,如果你检查一下实例,它可能会消除你的困惑!看起来真的很棒,但我如何将其插入我的?Mike.在同一行中,你会回显它的内容,但是如果你经常这样做,那么最好是创建一个函数,它接受一个字符串并返回一个封装在td中的字符串,我将更新帖子以显示一个简单的示例。
if (isset($autocollant_total_ht_custom)) {
$autocollant_total_ht_custom = $autocollant_total_ht_custom . $euro;
} else {
echo " ";
}
//$autocollant_total_ht_custom = null;
$autocollant_total_ht_custom = "something that isnt null";
//if you switch the variable assignment above you will see it behaves as expected.
$euro = "€";
if (isset($autocollant_total_ht_custom))
{
echo $autocollant_total_ht_custom = $autocollant_total_ht_custom . " " .$euro;
}
else
{
//$autocollant_total_ht_custom wouldn't be set at all if we reach this point, this is why im un-sure what your requirements are. Nothing would be echoed.
echo $autocollant_total_ht_custom;
}
$autocollant_total_ht_lot10 = null;
$autocollant_total_ht_lot11 = "";
$autocollant_total_ht_custom = "1,000";
$euro = "€";
if (isset($autocollant_total_ht_custom))
{
echo 'ht custom';
echo TDFromString($autocollant_total_ht_custom, $euro);
}
//notice this doesnt output anything because it isnt set
if (isset($autocollant_total_ht_lot10, $euro))
{
echo 'lot 10';
echo TDFromString($autocollant_total_ht_lot10, $euro);
}
//notice this does because the string, while empty is something that isnt null
if (isset($autocollant_total_ht_lot11))
{
echo 'lot 11';
echo TDFromString($autocollant_total_ht_lot11, $euro);
}
//lets set it to null and see what happens
$autocollant_total_ht_lot11 = null;
if (isset($autocollant_total_ht_lot11))
{
echo 'lot 11 AGAIN';
echo TDFromString($autocollant_total_ht_lot11, $euro);
}
//it doesnt get printed!
//create a function that takes the string in question,
//and for the sake of your use case also the currency to output,
//that way you could change 'euro' to 'currency'
//and have the sign change based on what the value of the $currency
//string is, eg $currency = "£"
function TDFromString($string, $currency)
{
return '<td>' . $string . ' ' .$currency . '</td>';
}
$autocollant_total_ht_lot10 = null;
$autocollant_total_ht_lot11 = "";
$autocollant_total_ht_lot12 = "2,0000000";
$autocollant_total_ht_custom = "1,000";
$euro = "€";
//create an array, and stick all our strings in it, from now, if we need to do something to one of the strings(or all!), we do it through the array
$arrayofLots = array($autocollant_total_ht_lot10, $autocollant_total_ht_lot11, $autocollant_total_ht_lot12, $autocollant_total_ht_custom);
//go over each array 'entry' so the first time is '$autocollant_total_ht_lot10', then '$autocollant_total_ht_lot11' etc
foreach ($arrayofLots as $lot)
{
//and we've been over this bit :)
//$lot is a variable we set so we have something to refer to for the individual array entry we are on, we could just as easily name it anything else
if (isset($lot))
{
echo TDFromString($lot, $euro);
}
}
function TDFromString($string, $currency)
{
return '<td>' . $string . ' ' .$currency . '</td>';
}