comparing two strings and determining if they are equal - php

I have two strings that am trying to compare but it is displaying that they are not equal. What could be the problem? Here is the code that am running.
<?php
$x = "Come and enjoy the show.";
$y = "Come and enjoy the show.";
if (strcmp($x, $y)) {
echo "They are the same.";
} else {
echo "They are not the same.";
}
?>

strcmp - It returns zero on exact match & hence else condition will get executed in your case.
Defination: Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.
Change your condition with,
if (strcmp($x, $y) === 0) {
echo "They are the same.";
} else {
echo "They are not the same.";
}
DEMO.

strcmp can returns multiple values.
From the doc :
Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.
So, instead try this code :
if (strcmp($x, $y) === 0) {
echo "They are the same.";
} else {
echo "They are not the same.";
}
?>

try this...
strcmp function
Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.
if (strcmp($x, $y) === 0) {
echo "They are the same.";
} else {
echo "They are not the same.";
}

Related

just need the explanation

In the following code, without == 0 the result shows a wrong answer (A is an odd number). and with == 0 the result shows the correct answer. I need to know the explanation.
<?php
$A = 78;
if ($A % 2 == 0) {
echo "A is a even number";
} else {
echo "A is an odd number";
}
;
Without "==0" i.e.
if($A % 2)
evaluates to 0 becomes,
if(0)
as this is false the respective else is get executed gives you "A is an odd number".
The number 0 is evaluated as false in PHP. In this case $A % 0 is 0.
In that context:
if ($A%0) //this is false, because it is 0 and 0 is evaluated as false
On the other hand, A%2 == 0 is the same as (0 == 0) and that is a true statement.
Other things that are evaluated to false include, but are not limited to: null, an empty array, an empty string an object with no variables.

preg_match always returns 0

In the code below I control if the id in the database is a number. My problem is: the regular expression where I test the id always gives the nummber 0 back never 1 - even when the id is a number.
Can you help me to solve this?
This is the code with the problem:
$muster = "|^[0-9]+$|";
if(preg_match($muster, $_POST["Anzahl"]) == 0 || preg_match($muster, $_POST["id"]) == 0 || $_POST["Anzahl"] < 1) {
die("<a href='javascript:history.back()'>Eingabe überprüfen</a>");
}
I would use PHPValidate filters
Its easier and more convenient:
<?php
$str = "100";
if (filter_var($str, FILTER_VALIDATE_INT)) {
echo("Variable is an integer");
} else {
echo("Variable is not an integer");
}
?>
If filter_var(($str, FILTER_VALIDATE_INT)) is successful it will return 100 as integer, otherwise false
Btw there are many usefull filter: http://php.net/manual/de/filter.filters.validate.php

Compare 2 strings with different encoding

How to make the following strings equal:
$str1 = "Première équation";
$str2 = "Première équation";
I tried html_entity_decode($str1) but it doesn't work
I'd use a combination of strcmp and html_entity_decode for binary-safe comparison.
<?php
$str1 = "Première équation";
$str2 = "Première équation";
var_dump( strcmp(html_entity_decode($str1), $str2) );
https://eval.in/551298
For example;
// Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.
if( strcmp(html_entity_decode($str1), $str2) === 0 ) {
echo "They match";
}

How to check if a number (float or integer) is within a range (0 - 100)

I'm looking for the fastest way to get this test.
So functions, operands and everything else is allowed.
I tried with the following regex (I'm not an expert):
0\.[0-9]+|100\.0+|100|[1-9]\d{0,1}\.{0,1}[0-9]+
It works except that it erroneously accept 0.0 or 0.000000 and so on.
Also it's not the most appropriated and fastest way.
(if anybody wants to fix the regex to don't allow those 0.00 values it would be appreciated)`
No need for regex:
if (is_numeric($val) && $val > 0 && $val <= 100)
{
echo '$val is number (int or float) between 0 and 100';
}
Demo
Update
It turns out you're getting the numeric values from a string. In that case, it would be best to extract all of them using a regex, something like:
if (preg_match_all('/\d+\.?\d*/', $string, $allNumbers))
{
$valid = [];
foreach ($allNumbers[0] as $num)
{
if ($num > 0 && $num <= 100)
$valid[] = $num;
}
}
You can leave out the is_numeric check, because the matched strings are guaranteed to be numeric anyway...
Use bccomp
This is a perfect use case for BCMath functions.
function compare_numberic_strings($number) {
if (
is_numeric($number) &&
bccomp($number, '0') === 1 &&
bccomp($number, '100') === -1
) {
return true;
}
return false;
}
echo compare_numberic_strings('0.00001');
//returns true
echo compare_numberic_strings('50');
//returns true
echo compare_numeric_strings('100.1');
//returns false
echo compare_numeric_strings('-0.1');
//returns false
From the manual:
Returns 0 if the two operands are equal, 1 if the left_operand is
larger than the right_operand, -1 otherwise.
I think your regex pattern should look like this:
^\d{1,2}$|(100)
Demo

String comparison is not working in php

I'm very new to php. I have a json named json. When I try to do this:
echo $json->status;
I get :
CREATED
I try to compare this result with normal string CREATED like this:
if(strcasecmp("CREATED",$json->status))
{
print_r("Order created successfuly");
}
but for some reason the if condition is not evaluting to true. Even though I compare CREATED with CREATED!
Not sure where the error is.
Thanks in advance.
This function return zero if strings are equal
if (strcasecmp("CREATED",$json->status) == 0)
Look to the manual:
Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.
so strcasecmp('a','a') is 0, therefore you have to change your code into
if(strcasecmp("CREATED",$json->status) == 0)
{
print_r("Order created successfuly");
}
http://php.net/manual/en/function.strcasecmp.php
Quote from the page :
Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.
So strcasecmp('CREATED', 'CREATED') returns 0. And 0 is not equals to true.
You must do that :
if (strcasecmp("CREATED",$json->status) === 0) {
print_r("Order created successfuly");
}
if (strcasecmp( $json->status, "CREATED") == 0)
{
...
...
}
Why cant you just use a simpler if statement?
if( $json->status == "CREATED" ) {
print_r("Order created successfuly");
}
And check for whitespaces at the end or start of the status.
To compare strings, try to do the following :
if($json->status == "CREATED")
{
echo "Order created successfuly";
}

Categories