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
Related
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
This question already has answers here:
checking if a number is float in PHP
(5 answers)
Shortest way to check if a variable contains positive integer using PHP?
(6 answers)
Closed 5 years ago.
I have a number that comes as a string from an input. It could be "450.021" or "30". I need to determine whether it is int or a float number. How do I do that?
I tried with intval but apparently floating numbers are floored and in my case it is not going to help me.
Very naiv approach
foreach(["342.23", "30"] as $a) {
if( (float)(int)$a === (float)$a ) {
echo "int";
} else {
echo "float";
}
}
But this won't catch cases where you parse "340.0".
Edit: Digging a bit more:
foreach(["342.23", "30"] as $a) {
$is_float = filter_var($a, FILTER_VALIDATE_FLOAT);
$is_int = filter_var($a, FILTER_VALIDATE_INT);
if( $is_float && $is_int ) {
echo $a . " is Int";
} else if( $is_float && !$is_int ) {
echo $a . " is Float";
}
}
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.
This question already has answers here:
PHP - if condition inside string
(5 answers)
Closed 8 years ago.
I have a string like this:
$str = "0 || 0 && 1";
actually this string is a condition.
if i do like this :
if($str) {
echo "done";
}
else {
echo "sfcsd";
}
it is always true since $str is string.
How can i evaluate this string with out eval().
check out sandboxing in PHP. here is a quick tutorial:
http://www.fieryprophet.com/blog/detail/sandboxing-untrusted-code-with-phpsandbox
This question already has answers here:
startsWith() and endsWith() functions in PHP
(34 answers)
Closed 9 years ago.
How do one determine,if the strings appears at the end of the other string. If they do then print true to standard out and false if they don’t. Would strpos will help?
Sample Input
S1: “staff”
S2: “FF”
how would i make a function to run this,
function matching_ends($s1,$s2){
}
<?php
$s1="testing";
$s2="ing";
echo matching_ends($s1, $s2);
function matching_ends($s1,$s2){
return substr($s1, -strlen($s2)) == $s2 ? "true" : "false";
}
?>
Demo
You can use this
if( substr($s1, strlen($s1)-1) === substr($s2, strlen($s2)-1))
{
// Do something when character at the last matches
}
else{
// Do something when doesn't match
}