Comparing strings

The primary function used to compare strings is strcmp (). As the only 2 parameters it accepts text strings which are to be compared. This function returns 0 if the strings are the same, value greater than zero if the first string is greater than the second value or less than zero if the first string is less than the second. This function is case characters.

If you need to compare two strings, you should use the strcasecmp () (parameters and return values such as the function strcmp). There is also a function strncmp (), which compares only the amount of characters from the beginning of the following strings, which was given as the third parameter of the function. Of course, there is also a version of the function strncmp () ignores the size of characters: strncasecmp ().

There is a rational explanation for why the strings should be compared using strcmp () or derivatives and not the usual comparison operator, == ‚. PHP interpreter tries to be smarter than the programmer automatically converting types of variables when comparing them to be the same.

Example

<?php
$a=0;
$b=”0papa”;
if($a == $b)
echo „Yes”;
else
echo „No”;

?>

The output is „Yes”, despite the fact that these variables are different. Why? PHP automatically converts the variable $ b to the numeric type, which is converted to a numeric value of 0 (which is actually in first place over the – the other characters are rejected because they are not numbers). You can theoretically use the operator „===” – a comparison not only value, but also the type.

Example

<?php
$a=0;
$b=”0″;
if ($a ===$b)
echo „Yes”;
else
echo „No”;

?>

The above example will display „No”, because despite the fact that both variables have a value of 0, the variable $ a is a number, and the variable $ b text string.
Strstr () function is used to check whether the specified string is a portion of another string. The function takes two parameters: the searched string (haystack ‚) and search string (needle’). If the string is a fragment specified, it returns the string – piece searched for from the first occurrence of the search string to the end.

Example

<?php

$email = „carmen@germany.de”;
$domain = strstr ($email, ‚@’);
echo $domain;

?>

The above example will display „@germany.de online project management tools.” If the string is not found, the return value is FALSE, so this function can be used in conditional statements.
Example

<?php

$email = „carmen@germany.de”;
if (strstr ($email, „germany”)! == False)
echo „Email has the word ‚germany’ ‚;

?>

When using this feature, you must remember that it is easy to mistake the return value „found at position 0” and   „no ‚because the Boolean value true comparisons can be converted to a numerical value 0. To avoid this, use the comparison operator checking type (, === ‚or’! == ‚).
There is also a version of this function ignores the size of characters: stristr () (accepted parameters and return values ​​of the functions is strstr).
Although strstr () can be used to check whether a string is one of the second part, it is ineffective. It is better to use the function strpos ().
To check whether the string matches the regular expression used in the function ereg () that the first parameter is a regular expression, and as a second string to be compared with the expression. There is also a version of this function ignores the size of characters: eregi (). The return value is true if the string matches the regular expression, and false if it does not fit.
Typically, the conditional statements that use the functions of checking a condition, this function returns TRUE if the condition is met, for example. Ereg function returns this value if the string matches the regular expression.

 

Example

<?php

if (ereg ( „. *”, $string))
echo „I found it!”

?>
In the case of the function strcmp () and derivatives it is a little different, because if the strings are identical to the return value is 0 regarded as a logical FALSE.

Example

<?php

if (! strcmp ( „something”, $string))
echo „$string is cośtam!”;

?>

 

Cookies help us deliver our services. By using our services, you agree to our use of cookies. More Info | Close