Jump to content

Recommended Posts

I have been learning php for like 2 days now, and I got a couple of things down, and I am just trying to write  simple script to display a different message depending on the day, so... heres my code -

[code]<?php
$d=Date("D");
$s="Have a good day sir on this cool Sunday!";
$m="Have a nice Monday dude!";
$t="Its Tuesday and have a nice day!";
$w="Wendsday! I'll bet your bored!?";
$th="Thursday's cool aint it?";
$f="Fridays rule!!!!";
$sa="Saturdays are cool too!!!!";

if ($d=="Sun")

{
echo $s;
}

elseif ($d=="Mon")

{
echo $m;
}

elseif ($d=="Tue")

{
echo $t;
}

elseif ($d=="Wen")

{
echo $w;
}

elseif ($d=="Thu")

{
echo $th;
}

elseif ($d=="Fri")

{
echo $f;
}

elseif ($d=="Sat")

{
echo $sa;
}

?>[/code]

So... can someone tell me whats wrong with this? I a have been following the tutorials on w3schools, and I am confused! lol.
Link to comment
https://forums.phpfreaks.com/topic/35080-help-a-php-newb-please/
Share on other sites

what's the issue you're having? the only thing i can see that's out of place (but not 100% sure its important but worth a try) is Date. Try and change the function name to ALL lowercase

[code]
$d=date("D");
[/code]

also you may want to look at the switch/case method, as it removes the need for so many ifs/elses and makes things easier to read:

[code]
<?php
switch ($d)
{
  case 'Sun':
     echo $s;
     break;
  case 'Mon':
     echo $m;
     break;
}
?>
[/code]

you can then even take things further by using an array (a group of variables). so:

[code]
<?php
$message['Sun'] = "This is Sunday";
$message['Mon'] = "This is Mondays message";
$message['Tue'] = "Tuesday, anyone?";
...etc...etc...

$d = date("D");

echo $message[$d];
?>
[/code]
[code]
<?php
$d=Date("D");
$s="Have a good day sir on this cool Sunday!";
$m="Have a nice Monday dude!";
$t="Its Tuesday and have a nice day!";
$w="Wendsday! I'll bet your bored!?";
$th="Thursday's cool aint it?";
$f="Fridays rule!!!!";
$sa="Saturdays are cool too!!!!";

switch($d)
{
case "Sun":
echo $s;
break;
case "Mon":
echo $m;
break;
case "Tue":
echo $t;
break;
case "Wed":
echo $w;
break;
case "Thu":
echo $th;
break;
case "Fri":
echo $f;
break;
case "Sat":
echo $sa;
break;
}
?>
[/code]
Avoid both big switch and/or ifelse statements.

[code]
<?php
$messages = array(
'Sun'=>"Have a good day sir on this cool Sunday!",
'Mon'=>"Have a nice Monday dude!",
'Tue'=>"Its Tuesday and have a nice day!",
'Wen'=>"Wendsday! I'll bet your bored!?",
'Thu'=>"Thursday's cool aint it?",
'Fri'=>"Fridays rule!!!!",
'Sat'=>"Saturdays are cool too!!!!");
echo $messages[Date("D")];
?>
[/code]

[b]Edit:[/b] Mark already said that.. ::)
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.