tags:

views:

168

answers:

5

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

+7  A: 

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 } ?>
spoulson
Quick minor question. Any difference between "" and '' in PHP? Also, good use of arrays. :P
Zack
Double quoted strings ("") interpolate $variables, while single quoted ('') do not. In this example, all the strings can us single quotes and maybe possibly gain performance. You never know with PHP.
spoulson
Just need another ) after the array list, and it works fine, with single quotes, too. Thanks, Mark
songdogtech
Whoops. Fixed. I guess that disqualified me for the accepted answer, eventhough mine came first. Oh well.
spoulson
+4  A: 

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") {
Welbog
A: 

Maybe this:

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

also, php has two operators for equality.

== and ===

Jhonny D. Cano -Leftware-
+8  A: 

You're performing an assignment of $d when you say ($d="Mon"). What you want is the comparison operator (==):

if ($d == "Mon" || $d == "Tue")
John Rasch
+2  A: 

try:

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