View Full Version : Doing if statements based on part of a string

05-23-2008, 10:11 AM
I am trying to do a bunch of if and elseif based on part of a variable.

All the variables I want to process in the same manner contain the same string:

submit_zzz etc..

So I would like to do something like this:

if ($_GET['var'] OPERATOR "submit_") {....

Does such an operator exists? Or do I have to do use a "==" for each variable I want to process? I guess what I am looking for is an operator that means "contain".

Thanks for any help.

05-23-2008, 10:31 AM
There are many functions for string manipulation.
See www.php.net/substr

05-23-2008, 11:10 PM
if(preg_match('/submit\_/i', $var))
//do stuff

That will catch all strings with submit_ in them. Since I'm not sure how your getting $var I didn't include any code. Have no idea if your looping through and array or testing variables hard coded.

05-23-2008, 11:14 PM
I don't know if it makes any difference to use digitalfiz's example or mine, but this is what I use to look for a value within a variable:

if (strpos($_GET['var'], 'submit_') === FALSE) {
//$_GET['var'] does NOT contain submit_
} else {
//$_GET['var'] DOES contain submit_


05-24-2008, 03:31 AM
strpos() (http://php.net/strpos) is much faster than preg_match() (http://php.net/preg_match).