[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'?
 
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
 
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
}
 
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.
 
i found a better way
Code:
$valuestomatchon = array('co','org,'net','ac');

if (in_array($host2, $valuestomatchon)) 
{
echo "Found it"
}
 
Thanks 😉 *must learn how to use the PHP functions list*

edit: Works perfectly 😉
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
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.

Forum statistics

Threads
62,016
Messages
673,499
Members
5,629
Latest member
bruno16757
Back