Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,472
There are 1527 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

Date, ifelse, include

Thread title: Date, ifelse, include
Closed Thread    
    Thread tools Search this thread Display Modes  
02-19-2008, 01:27 PM
#1
G-Sun is offline G-Sun
Status: Member
Join date: Feb 2008
Location: Norway
Expertise:
Software:
 
Posts: 101
iTrader: 0 / 0%
 

G-Sun is on a distinguished road

  Old  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
Gaz is offline Gaz
Gaz's Avatar
Status: Request a custom title
Join date: Apr 2007
Location: UK
Expertise: Code & Programming
Software: Coda, TextMate, Sublime 2
 
Posts: 2,097
iTrader: 26 / 100%
 

Gaz will become famous soon enough Gaz will become famous soon enough

Send a message via Skype™ to Gaz

  Old

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
Bradlc is offline Bradlc
Status: Junior Member
Join date: Sep 2007
Location: Lancashire
Expertise:
Software:
 
Posts: 46
iTrader: 10 / 100%
 

Bradlc is on a distinguished road

  Old

Originally Posted by GarethP View Post
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
Salathe is offline Salathe
Salathe's Avatar
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
 
Posts: 3,820
iTrader: 25 / 100%
 

Salathe will become famous soon enough

Send a message via MSN to Salathe

  Old

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
G-Sun is offline G-Sun
Status: Member
Join date: Feb 2008
Location: Norway
Expertise:
Software:
 
Posts: 101
iTrader: 0 / 0%
 

G-Sun is on a distinguished road

  Old

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
Gaz is offline Gaz
Gaz's Avatar
Status: Request a custom title
Join date: Apr 2007
Location: UK
Expertise: Code & Programming
Software: Coda, TextMate, Sublime 2
 
Posts: 2,097
iTrader: 26 / 100%
 

Gaz will become famous soon enough Gaz will become famous soon enough

Send a message via Skype™ to Gaz

  Old

Ahh I never knew about the curly brackets not being needed! Thanks!

02-19-2008, 06:55 PM
#7
Salathe is offline Salathe
Salathe's Avatar
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
 
Posts: 3,820
iTrader: 25 / 100%
 

Salathe will become famous soon enough

Send a message via MSN to Salathe

  Old

Originally Posted by G-Sun View Post
..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
G-Sun is offline G-Sun
Status: Member
Join date: Feb 2008
Location: Norway
Expertise:
Software:
 
Posts: 101
iTrader: 0 / 0%
 

G-Sun is on a distinguished road

  Old

Thanks Salathe!

02-20-2008, 01:08 PM
#9
Fun4MySpace is offline Fun4MySpace
Status: I'm new around here
Join date: Feb 2008
Location:
Expertise:
Software:
 
Posts: 7
iTrader: 0 / 0%
 

Fun4MySpace is on a distinguished road

  Old

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
Wildhoney is offline Wildhoney
Wildhoney's Avatar
Status: Request a custom title
Join date: Feb 2006
Location: Nottingham
Expertise:
Software:
 
Posts: 1,648
iTrader: 18 / 95%
 

Wildhoney is on a distinguished road

Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney

  Old

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; 

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed