|
|
|
|
Thread title: Date, ifelse, include |
|
|
|
|
|
Thread tools
Search this thread
Display Modes
|
|
02-19-2008, 01:27 PM
|
#1
|
Status: Member
Join date: Feb 2008
Location: Norway
Expertise:
Software:
Posts: 101
|
Date, ifelse, include
Hi!
I'm rather new to PHP and are using some basic functions.
However I can't seem to get this date; ifelse; include -statment to work.
<?php
$d=date("w");
if ($d=="1" OR "4")
include("Div filer/Visdom/Visdom02.html");
elseif ($d=="6")
include("Div filer/Visdom/Visdom03.html");
else
include("Div filer/Visdom/Visdom01.html");
?>
|
What I want to it to do is to include a different file depending on the day of week.
What is wrong with my code?
Thanks!
|
|
02-19-2008, 02:04 PM
|
#2
|
Status: Request a custom title
Join date: Apr 2007
Location: UK
Expertise: Code & Programming
Software: Coda, TextMate, Sublime 2
Posts: 2,097
|
Code:
<?php
$d=date("w");
if ( $d == "1" OR "4" ){
include("Div filer/Visdom/Visdom02.html");
}
elseif ( $d == "6" ){
include("Div filer/Visdom/Visdom03.html");
}
else {
include("Div filer/Visdom/Visdom01.html");
}
?>
You forgot the parentheses ({})
Gareth.
|
|
02-19-2008, 02:27 PM
|
#3
|
Status: Junior Member
Join date: Sep 2007
Location: Lancashire
Expertise:
Software:
Posts: 46
|
Originally Posted by GarethP
You forgot the parentheses ({})
Gareth.
|
You don't need the curly brackets.
Code:
<?php
$d = date("w");
if ($d == 1 || $d == 4)
include("Div filer/Visdom/Visdom02.html");
elseif ($d == 6)
include("Div filer/Visdom/Visdom03.html");
else
include("Div filer/Visdom/Visdom01.html");
?>
|
|
02-19-2008, 02:33 PM
|
#4
|
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
Posts: 3,820
|
The curly braces are not a requirement if there is only one line of code within each condition block (as there is in G-Sun's code).
The problem appears to be the very first condition: if ($d=="1" OR "4")
First, it checks to see if $d is equal to "1". If that is true (if it's a Monday) then the rest of the condition is skipped (because of the OR) and the first include is processed. However if the day isn't Monday, we move onto the next check: "4". The way that PHP evaluates conditions in this case means that "4" actually means that the check returns TRUE! Because of this, whenever the code runs, the first condition will always return TRUE and thus always run the first include.
To fix the problem, you need to amend the condition to be:
if ($d == '1' OR $d == '4')
Note, the important change is to check that $d is equal to '4'. I've used single quotes around the string values because that's better practice but it would work fine with double quotes as you originally used.
|
|
02-19-2008, 04:23 PM
|
#5
|
Status: Member
Join date: Feb 2008
Location: Norway
Expertise:
Software:
Posts: 101
|
Thanks all!
The way that PHP evaluates conditions in this case means that "4" actually means that the check returns TRUE! Because of this, whenever the code runs, the first condition will always return TRUE and thus always run the first include.
|
Exactly Salathe, that was my problem. I did what you and Bradlc said and: Works!
Thanks!
..Can you explain why "4" returns true?
..the strange thing is that I believe my last code worked when I had another host..
|
|
02-19-2008, 06:35 PM
|
#6
|
Status: Request a custom title
Join date: Apr 2007
Location: UK
Expertise: Code & Programming
Software: Coda, TextMate, Sublime 2
Posts: 2,097
|
Ahh I never knew about the curly brackets not being needed! Thanks!
|
|
02-19-2008, 06:55 PM
|
#7
|
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
Posts: 3,820
|
Originally Posted by G-Sun
..Can you explain why "4" returns true?
|
For slightly fuller information, check out the PHP manual page on the Boolean type, particularly Converting to boolean. Basically, if you have a string and want to get its boolean value (TRUE/FALSE) then the only strings that will return FALSE are an empty string ("") or the string "0"; all other strings will be boolean TRUE. If you remember that only those two strings will ever be false then you'll be ok. Remember not to trip up on strings like "false" and "-1" which (if you've been reading) you'll know will actually result in TRUE.
|
|
02-20-2008, 08:59 AM
|
#8
|
Status: Member
Join date: Feb 2008
Location: Norway
Expertise:
Software:
Posts: 101
|
|
|
02-20-2008, 01:08 PM
|
#9
|
Status: I'm new around here
Join date: Feb 2008
Location:
Expertise:
Software:
Posts: 7
|
Curly brackets are not always needed as he mentioned, howvever it is good practice to get into a habit of always using them. It also makes your code appear much cleaner.
|
|
02-21-2008, 10:54 PM
|
#10
|
Status: Request a custom title
Join date: Feb 2006
Location: Nottingham
Expertise:
Software:
Posts: 1,648
|
Plus there's the other type of if statement syntax which may be useful for beginners, such as yourself. Seeing as how you're also using OR instead of ||.
PHP Code:
if($bIsTrue):
endif;
|
|
|
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
|