If statement help


#1

I would really appreciate it if something could help with this:

I want to say if a variable begins with order196 or order197 or order198 or order199 order order200 then do something

Would anyone know some good code for doing this?


#2

Not sure exactly what info is coming in, what you want “to do” with it, if it’s only those values. Here’s a quickie for what info you gave.
[php]<?PHP
$goArr = array( “order196” => “page1.php”,
“order197” => “page2.php”,
“order198” => “page3.php”,
“order199” => “page4.php”,
“order200” => “page5.php”);
// incoming is your incoming data
$incoming = “order198_garbadedata_yadayada”;

// strip off first 8 chars
$inComp = substr($incoming, 0, 8);

// check if it’s in your array
if(array_key_exists($inComp, $goArr))
{
$location = $goArr[$inComp];

header("location: $location");

} else {
echo “Not found”;
}

?>[/php]


#3

Or you can use a switch statement. Assuming your variables holds either order196, order197, order198, order199, order 200 etc

[php]

$variable

switch ($variable)

{

case “order196”:
do something
break;

case “order197”:
do something
break;

case “order198”:
do something
break;

case “order199”:
do something
break;

case “order200”:
do something
break;

}

[/php]

and so on…


#4

You might try something like this. You really should be more specific with your requests for help. What exactly are you doing?

If this is anywhere close, look at variable variables (http://www.php.net/manual/en/language.v … riable.php)

[php]

<?php for ($i=196; $i<200; $i++){ //create the label of the variable; $var_label_string = "order$i"; //Now evaluate that string as if it were a variable !; $value_of_that_var = ${$var_label_string}; //now find out if that var is equal to what you're looking for; if ($value_of_that_var == "check"){ // or if (isset($$var_label_string)) { print "Requested variable found here: $var_label_string"; } } ?>[/php]

#5

He said “if a variable begins with order196…” so I assumed that there was more to the var. But it’s kind of funny to see 3 completely different ways to do the same thing. Kind of shows the flexability of PHP as well as the diversity of it’s users. Wow, that sounded like a bad PR statement…