简体   繁体   中英

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. (I'm just learning 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") . What you want is the comparison operator ( == ):

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

You're assuming that date("D") will return more than one value. 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". 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.

== and ===

If you've string with value 'Mon,Tue', Then you can check

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

There is no chance for that, So you need to use OR condition.
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 } ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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