why won't this work?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Todlerone
Forum Commoner
Posts: 96
Joined: Sun Oct 28, 2007 10:20 pm
Location: Hamilton, Ontario, Canada

why won't this work?

Post by Todlerone »

Hello everyone and again TY in advance for any help/suggestions. This code won't work for me, is the syntax incorrect? If I echo the variables before the if loop there are values stored in them.

Code: Select all

 
$dothis = "($Visitor == $team) || ($Home == $team)";
if ($dothis){
...
}
TY again
 
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: why won't this work?

Post by pickle »

Strings evaluate to false if they are equivalent to boolean false (pretty much just the string "0"). Anything else evaluates to TRUE. Your string, though it contains characters a human would view as boolean conditions, is just treated like any other string & will always return TRUE
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Todlerone
Forum Commoner
Posts: 96
Joined: Sun Oct 28, 2007 10:20 pm
Location: Hamilton, Ontario, Canada

Re: why won't this work?

Post by Todlerone »

What can I do to get around this? I want to enter the loop if either the $Home or $Visitor is equal to the $team variable.

TY Pickle
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: why won't this work?

Post by iankent »

Code: Select all

$dothis = ($Visitor == $team) || ($Home == $team) ? true : false;
if ($dothis){
...
}
if $Visitor == $team or if $Home == $team then $dothis becomes true, otherwise it becomes false.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: why won't this work?

Post by pickle »

Don't put it in a string

Code: Select all

if($Visitor == $team || $Home == $team)
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Todlerone
Forum Commoner
Posts: 96
Joined: Sun Oct 28, 2007 10:20 pm
Location: Hamilton, Ontario, Canada

Re: why won't this work?

Post by Todlerone »

The problem is the $dothis varies depending on the search result selected in a switch loop.

TY
Monotoko
Forum Commoner
Posts: 64
Joined: Fri Oct 26, 2007 4:24 pm

Re: why won't this work?

Post by Monotoko »

what are you after doing with the script?

You could do something like this:

Code: Select all

 
<?php
 
if ($Visitor==$team) // If the visitior is the team...
{
//do some code if the visitor is the team..
 }
elseif ($Home == $team) //or if the home is the team..
{
//do some code here if the home is the team...
}
?>
 
 
Todlerone
Forum Commoner
Posts: 96
Joined: Sun Oct 28, 2007 10:20 pm
Location: Hamilton, Ontario, Canada

Re: why won't this work?

Post by Todlerone »

I'm just trying to reuse the same code. If either home or visitor is true do the code otherwise loop to next element in array. I didn't think I would run into a problem with the $dothis"..." assignment. for the If loop.
Post Reply