Match strings with an if clause without checking capitals in php [duplicate] - php

This question already has answers here:
Case insensitive string comparison
(6 answers)
Closed 4 years ago.
I have this code:
$a = 'abc';
$b = 'AbC';
if ($a == $b)
{
echo 'abc == ABc!';
}
else
{
echo 'abc != ABc!';
}
Now it echoes abc != ABc! but i'd like it to match the strings regardless of the capitals.

Two options:
1) convert the casing and do a comparison.
strtolower($a) === strtolower($b)
One caveat of this is that for non-utf8 characters and non-english languages this does not work well.
2) use case insensitive comparison
if (strcasecmp($a, $b) == 0) {
strcasecmp docs

Related

Checking if a string contains " or ' [duplicate]

This question already has answers here:
Simple PHP strpos function not working, why?
(5 answers)
Closed 3 years ago.
I am trying to do a input check and to seeif the string contains " or '. I have the stringcheck method that looks like this
public static function stringcheck($search, $string)
{
$position = strpos($string, $search);
if ($position == true)
{
return true;
}
else{
return false;
}
}
And this is how i am trying to check it
if(H::stringcheck($search8, $string) === true || $string[0] === """)
{
$value++;
}
And the $search8 look like $search8 = htmlspecialchars('"');. I should also mention that the $string is already sanitized.
strpos returns the position of the match, which will be 0 if the match is at the beginning. But 0 is not == true.
I guess you are trying to work around that by extra checking $string[0] === """ which won't work, as I commented above: $string[0] is a single character, but " is 6 characters long.

Php do not make distinction in if statement with capital letters [duplicate]

This question already has answers here:
Is == in PHP a case-sensitive string comparison?
(7 answers)
Closed 4 years ago.
I have an if statement in my website. And what I want, is that he won't make distinction in capital letters. My if statement:
<?php
$nameOne = "JOHN";
$check = "john";
if($nameOne == $check){
echo 'No distinction';
} else {
echo 'distinction ';
}
?>
So, I want to echo 'No distinction'.
You can make the comparison using strtolower() on both strings
if (strtolower($nameOne) == strtolower($check))
You can use the solution suggested by #NicoHaase
<?php
$var1 = "Hello";
$var2 = "hello";
if (strcasecmp($var1, $var2) == 0) {
echo '$var1 is equal to $var2 in a case-insensitive string comparison';
}
?>
https://secure.php.net/manual/en/function.strcasecmp.php

PHP strpos with special characters [duplicate]

This question already has answers here:
php 5 strpos() difference between returning 0 and false?
(5 answers)
If string contains forward slash
(4 answers)
Closed 5 years ago.
i try to check a string with strpos().
$a = $_SERVER['REQUEST_URI'];
This works just fine
if (strpos($a, 'en' ) == true)
But this doesn't
if (strpos($a, '/en/' ) == true) - Doesn't work
I tried a lot of things to escape the character or format the string but as it seems I am too stupid...
The issue is that strpos returns the position or FALSE if not found.
So if the url is /en/something/some then you are hitting the situation where en is a position 1 and any non-zero number is true
When you do /en/ then the starting position is 0 and that is false.
you need to check with === in the end or more accurately !== example
<?php
$a= "/en/blo";
if (strpos($a, '/en/' ) !== false){
echo "TRUE";
} else {
echo "FALSE";
}

Difference between $a == 1 and 1 == $a [duplicate]

This question already has answers here:
PHP why (null === $variable) and not ($variable === null) in comparison? [duplicate]
(3 answers)
Closed 8 years ago.
I have seen multiple examples of such comparison, some other example ( from wordpress core):
if ( '' != $qv['subpost'] )
$qv['attachment'] = $qv['subpost'];
Is code above same as:
if ( $qv['subpost'] != '' )
$qv['attachment'] = $qv['subpost'];
or they are different in functionality?
Some people prefer the constant == variable option, as it'll cause fatal errors if you accidentally type a = and try to do assignment:
e.g.
$a = 'foo'; // assigns 'foo' to $a
$a == 'foo'; // tests for equality
'foo' == $a // tests for equality
'foo' = $a // syntax error - assigning value to a string constant
But functionally, otherwise, there's no difference between both versions. a == b is fully equivalent to b == a.
There is no difference.
(A == B) == (B == A)
The only thing that someone can put the value first is the readability, for example:
if ( 'APPLE' == $var ) {
} else if ('BANANA' == $var) {
}
There is no functional difference. You are comparing equality, and they will either be equal or not, no matter what side of the operator the values are on.
This question comes down to code style. Personally, when comparing against static values I prefer to always have the variable on the left. Others disagree. Use whatever style is in place in the project you're working on.
Yes, they do the same thing. It checks to see if $qv['subpost'] contains a value in both examples. No difference at all unless you're Yoda.

PHP false-positives [duplicate]

This question already has answers here:
The 3 different equals
(5 answers)
Closed 8 years ago.
So I have this function and I'm trying to understand how this is true and how it comes out false if I use === instead of == .
function is_equal($value1, $value2) {
$output = "{$value1} == {$value2}: ";
if ($value1 == $value2) {
$output = $output . "true<br />";
} else {
$output = $output . "false<br />";
}
return $output;
}
echo is_equal("123", " 123");
echo is_equal("123", "+0123");
?>
this code above comes out true because I'm testing for == how is that? and also if I use === it's false
When you compare equality using ==, PHP will juggle the types. I suspect your types are being juggled resulting in a numeric comparison.
When you compare equality using === the type is compared first, followed by the values.
Yes, this is right. === will compare the value and the typeinstead of == that is comparing if values are identical.
You can also try this one :
echo is_equal("123", 123);
=== tests whether the two variables are identical (same value, same type). == tests for equality and does type juggling for you.
Read up over here.

Categories