简体   繁体   English

简单的PHP问题

[英]Simple PHP question

Yes, it's a simple question, but one that I can't find a answer for through the PHP documentation or Google. 是的,这是一个简单的问题,但我无法通过PHP文档或Google找到答案。 (I'm just learning PHP....) (我只是在学习PHP ....)

If this works: 如果这样做:

<?php $d=date("D"); if ($d="Mon") { ?>echo this text on Monday<?php endwhile; ?><?php } else { ?><?php } ?>

Why doesn't this? 为什么不呢?

<?php $d=date("D"); if ($d="Mon,Tue") { ?>echo this text on Monday and Tuesday<?php endwhile; ?><?php } else { ?><?php } ?>

Do I need different delimiters between Mon and Tue? 星期一和星期二之间我需要不同的分隔符吗? I've tried || 我试过了|| and && .... 和&& ....

Thanks, Mark 谢谢,马克

You're performing an assignment of $d when you say ($d="Mon") . 当你说($d="Mon")时,你正在执行$d的赋值。 What you want is the comparison operator ( == ): 你想要的是比较运算符( == ):

if ($d == "Mon" || $d == "Tue")

You're assuming that date("D") will return more than one value. 您假设date("D")将返回多个值。 It will only return the current day. 它只会返回当天。 Instead use this: 而是使用这个:

<?php $d=date("D"); if (in_array($d, array("Mon","Tue"))) { ?>echo this text on Monday and Tuesday<?php endwhile; ?><?php } else { ?><?php } ?>

The string $d is either going to contain "Mon" or "Tue", never "Mon,Tue". 字符串$d要么包含“Mon”或“Tue”,要么不包含“Mon,Tue”。 You can't compare strings this way. 你不能用这种方式比较字符串。 You need to use an expression like this: 你需要使用这样的表达式:

if ($d == "Mon" || $d == "Tue") { 

尝试:

<?php $d=date("D"); if (in_array($d,array('Mon','Tue'))) { ?>echo this text on Monday and Tuesday<?php endwhile; ?><?php } else { ?><?php } ?>

Maybe this: 也许这个:

   if ($d == "Mon" || $d == "Tue") { 

also, php has two operators for equality. 另外,php有两个运算符用于相等。

== and === ==和===

If you've string with value 'Mon,Tue', Then you can check 如果你的字符串值为'Mon,Tue',那么你可以查看

if($d=='Mon,Tue')

There is no chance for that, So you need to use OR condition. 没有机会,所以你需要使用OR条件。
ie,

if($d=='Mon' || $d=='Tue')

Try this code which is more user readable: 试试这个更易于用户阅读的代码:

<?php $d=date("D");
$days=array("Mon,Tue");
if ($d="Mon,Tue") if(in_array($a,$days)) { ?>echo this text on Monday and Tuesday<?php endwhile; ?><?php } else { ?><?php } ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM