[PHP] 'if' question

Glaanieboy

OSNN Veteran Addict
#1
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 User
#2
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 User
#3
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
#4
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 User
#5
i found a better way
Code:
$valuestomatchon = array('co','org,'net','ac');

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

Members online

No members online now.

Latest posts

Latest profile posts

Perris Calderon wrote on Electronic Punk's profile.
Ep, glad to see you come back and tidy up...did want to ask a one day favor, I want to enhance my resume , was hoping you could make me administrator for a day, if so, take me right off since I won't be here to do anything, and don't know the slightest about the board, but it would be nice putting "served administrator osnn", if can do, THANKS

Been running around Quora lately, luv it there https://tinyurl.com/ycpxl
Electronic Punk wrote on Perris Calderon's profile.
All good still mate?
Hello, is there anybody in there? Just nod if you can hear me ...
Xie
What a long strange trip it's been. =)

Forum statistics

Threads
61,971
Messages
673,300
Members
89,016
Latest member
Poseeut