[PHP] 'if' question

Glaanieboy

OSNN Veteran Addict
Joined
6 Mar 2002
Messages
2,628
I currently have a if statement like this:
Code:
if ($host2 == "co" || $host2 == "org" || $host2 == "net" || $host2 == "ac"){
As you can see, $host2 can be different values, if it is one of those values, then it should do something. It all works perfectly, but I have to add more '$host2 == "value" ' thingies there and I am afarid the list is going to be too long. I know that I can put all those values in a array, but how can I check those values with an 'if'?
 

Khayman

I'm sorry Hal...
Political Access
Joined
6 Jan 2002
Messages
5,518
You could do a switch statment

Code:
switch($host2)
{
case "co":
   //do blah
   break;
case "org":
 //more blah
   break;
case "net":
   //blah balh
   break;
}

can keep doing that forever
 

Khayman

I'm sorry Hal...
Political Access
Joined
6 Jan 2002
Messages
5,518
Or using an array

Code:
$valuestomatchon = array('co','org,'net','ac');

$count = array_count_values(($valuestomatchon);
for ($i = 0; $i < $count; $i++) 
{
 if ($host2 == valuestomatchon[count])
 //do somthing
}
 

Glaanieboy

OSNN Veteran Addict
Joined
6 Mar 2002
Messages
2,628
Let me give the part of the script I am talking about:
Code:
if ($host2 == "co" || $host2 == "org" || $host2 == "net" || $host2 == "ac"){
 $host = "$host1.$host2.$host3";
}else{
 $host = "$host2.$host3";
}
As you can see, when it has found of those values, it makes a different $host than when those values aren't found. It doesn't matter if it's "co" or "org", it does the same. Only until $host2 doesn't contain one of those values, the other part is being executed.
I hope I am making sense here.

edit: Yes, that array example you gave is good. I will use that.
 

Khayman

I'm sorry Hal...
Political Access
Joined
6 Jan 2002
Messages
5,518
i found a better way
Code:
$valuestomatchon = array('co','org,'net','ac');

if (in_array($host2, $valuestomatchon)) 
{
echo "Found it"
}
 

Glaanieboy

OSNN Veteran Addict
Joined
6 Mar 2002
Messages
2,628
Thanks ;) *must learn how to use the PHP functions list*

edit: Works perfectly ;)
 

Members online

No members online now.

Latest profile posts

Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.
Terrahertz wrote on Electronic Punk's profile.
Yo fellas!
Electronic Punk wrote on Sazar's profile.
Where are you buddy?

Forum statistics

Threads
62,007
Messages
673,466
Members
5,598
Latest member
Larry Chris