Forum Moderators: coopster

Message Too Old, No Replies

Comparison of variables

What comparison function?

         

bethesda

7:11 pm on Dec 23, 2011 (gmt 0)

10+ Year Member



Hello again.
I want to compare the variable and its contents.
I wanted to ask you which function is best to use ?


if(stristr($var, 'my_string_to_compare') === TRUE) {
header("Location: ../scriptA.php?allok");
} else {
header("Location: ../scriptB.php?error");
exit();
}


I guess I chose the wrong function.
Generally, I am looking for a function that will check the contents (caseSensitive).

Then, if the contents match - redirect on page X, or if the contents do not agree, you are directed to Y.

In this case, even when the variable contains a valid string of characters - you are redirected to a page with an error.

bethesda

7:42 pm on Dec 23, 2011 (gmt 0)

10+ Year Member



Ok i use function (strstr) ..
And it works just fine ..

Topic closed ;)

penders

11:27 pm on Dec 23, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ok i use function (strstr) ..
And it works just fine ..


Are you sure? This function should not be used for string comparison since it is slower than the alternatives.

Unless you are wanting to check for a partial match, what is wrong with a simple equality comparison...
if ($var == 'my_string_to_compare') {


Or, if you need a partial match where 'my_string_to_compare' should be anywhere inside $var then use strpos() [uk.php.net]...
if (strpos($var,'my_string_to_compare') !== false) {

bethesda

11:06 am on Dec 24, 2011 (gmt 0)

10+ Year Member



Thanks. I switch it to
strpos


I wish you all a healthy and peaceful Christmas and all the best in the coming new year.

Dinkar

11:57 am on Dec 24, 2011 (gmt 0)

10+ Year Member



I would prefer switch [php.net], if there are lots of IFs.