Jump to content

Help a php newb please!


joe48182

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.. ::)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.