This is the code I used to have to check if $A doesn't match $B
if($A!=$B) {
$set = array();
echo $val= str_replace('\\/', '/', json_encode($set));
//echo print_r($_SERVER);
exit;
}
Now I need the opposite of this condition: ($A need to match one of these $B,$C or $D)
A simple shortcut to seeing if a value matches one of multiple values you can put the values to be compared against ($B, $C, and $D) into an array and then use in_array() to see if the original value ($A) matches any of them.
if (in_array($A, [$B, $C, $D])) {
// ...
}
If you don't want it to match any of $B, $C, or $D just use !:
if (!in_array($A, [$B, $C, $D])) {
// ...
}
You can use array_search
$B = 'B';
$C = 'C';
$D = 'D';
//match B
$A = 'B';
$options = [$B, $C, $D];
if (false !== ($index = array_search($A, $options ))) {
echo "Match: {$index} '{$options[$index]}'";
}
Output
Match: 0 'B'
Sandbox
The nice thing here is you can set the $index and use that to tell which one matched later.
Note you have to use false !== because array search returns the index where the match happened at, so it can happen on the first array element which is index 0. As we know PHP can treat 0 as false (in this case the condition would fail when it should pass). However, when we use the strict type check PHP also compares the type and INT 0 is not BOOL false (which passes the condition).
for reference.
http://php.net/manual/en/function.array-search.php
Another probably the most efficient way is to use isset, and use keys instead of values:
$options = [$B=>1,$C=>1,$D=>1]; //values don't matter
if(!isset($options[$A])){
//....
}
Related
I have this PHP if statement that I am trying to firgure out.
I know what the result is, but I have not seen this before (probably my bad).
Who can help me understand the code below? Why is $a showing and not $b? Because $a is first in line?
<?php
$a = 'has a value';
$b = 'this one too!';
if (($href = $a) || ($href = $b)) {
echo $href;
//Result is 'has a value'.
}
?>
|| uses "short-circuit" evaluation. If the first part of the expression is true, then the second part will not be evaluated. In PHP,
The value of an assignment expression is the value assigned.
(quoted from the = documentation)
so in this case the expression ($href = $a) has a value of the assigned value, 'has a value'. That string evaluates to true (see "converting to boolean"), so the second assignment will not be executed.
FYI, for another way to write this that's a little less repetitive, you can do this:
if ($href = $a ?: $b) {
echo $href;
}
I'm struggling with a small piece of code that doesn't want to evaluate itself :
$t = 5;
$s = "<=";
$r = 6;
var_dump($t.$s.$r);
Here the var_dump return "5<=6" which make sense but I just want it to tell me if 5 is inferior or equal to 6 with a boolean.
I wanted to know if there was an other way to get this boolean beside using an eval() or a switch throught all the possible operator
Thanks in advance.
If you want a safe and flexible solution, this allows you to define a method which is executed depending on the operator matching the key in an array, it only works with two operands, but the last one in the examples # just multiplies the first value by 4 and returns the value...
$operators = [ "<=" => function ($a, $b) { return $a <= $b;},
"<" => function ($a, $b) { return $a < $b;},
">=" => function ($a, $b) { return $a >= $b;},
">" => function ($a, $b) { return $a > $b;},
"#" => function ($a) { return $a * 4; }];
$t = 5;
$s = "<=";
$r = 6;
var_dump($operators[$s]($t,$r));
$s = "<";
var_dump($operators[$s]($t,$r));
$s = ">=";
var_dump($operators[$s]($t,$r));
$s = ">";
var_dump($operators[$s]($t,$r));
$s = "#";
var_dump($operators[$s]($t,$r));
gives...
/home/nigel/workspace2/Test/t1.php:14:
bool(true)
/home/nigel/workspace2/Test/t1.php:17:
bool(true)
/home/nigel/workspace2/Test/t1.php:20:
bool(false)
/home/nigel/workspace2/Test/t1.php:23:
bool(false)
/home/nigel/workspace2/Test/t1.php:26:
int(20)
It's a bit convoluted, but also extensible and safe.
while it is generally not a good idea to have code usch as this (evaluating code that is sstored as plaintext), there is a function for exactly this: eval().
eval() does what you expect PHP to do naturally: evaluate valid code stored in a string.
eval("var_dump(".$t.$s.$r.");"); will do the job - however, since any code inside those variables is executed without question, it can be a security risk, or at least introduce some hard-to-debug errors.
(the extra quoting and the ; are needed to make the code inside eval actually valid PHP code)
$c1 = $v1 = array();
$v1['key'] = 'value';
echo '$c1 === $v1: ' . (($c1 === $v1) ? 'true' : 'false'); // prints false
$c1 === $v1 is false. But why? It seems that $v1 is automatically set to a different array then it's origin array automatically. Why it is happening?
Initially $c and $v1 is set to the same array instance. So if I mutate $v1 should not $c reflects the changes as they are set to the same array instance.
These will not be the same, because you explicitly set them to hold different values. The first is empty, while the second holds values.
They are not set to the same reference though, so they are two different variables - when you do
$c1 = $v1 = array();
You create two different arrays. If you want the change of one to reflect in both arrays, you need to make it a reference, by using the & operator in front of the variable identifier, like so.
$v1 = array();
$c1 = &$v1; // $c1 is now a reference to $v1
$v1['key'] = 'value';
echo '$c1 === $v1: ' . (($c1 === $v1) ? 'true' : 'false'); // true!
Notice that you need to reference it after the variable you wish to refer to has been made.
When using a reference like this, it goes both ways - any change to $v1 would be applied to $c1, and the other way around. So they are different variables, but will always hold the same values.
The comparison in the example above holds true because the arrays are exactly the same - not just by reference, but because they hold the same values and keys. If you compare two non-referenced arrays with the exact same values and exact same, matching keys, you would get a true equality too.
var_dump(array('foo') === array('bar')); // false; same keys, different values
var_dump(array('bar') === array('bar')); // true; same keys, same values
var_dump(array('bar') === array('baz' => 'bar')); // false; different keys, same value
Live demo
Passing variables by reference
Because they are two different object, with two different references, with happening one does not affect the other. Just simple like that.
Those arrays will not be the same because the second array has a value. Please run the following code:
<?php
$a = $b = [];
print_r($a);
print_r($b);
$result = ($a === $b) ? 1 : 0;
// The reasult will be 1 because the arrays are both empty.
print_r($result);
$b[0] = 'Ravi';
print_r($a);
print_r($b);
$result = ($a === $b) ? 1 : 0;
// The result will be 0 because the arrays are different.
print_r($result);
I had the following in my code:
$mixed = array();
$mixed[0] = "It's a zero!";
$mixed['word'] = "It's a word!";
foreach ($mixed as $key => $value) {
if ($key == 'word') {
echo $value.'<br />';
}
}
The above would for some reason print both "It's a zero!" and "It's a word!". I was expecting it to print only "It's a word!". Why is that?? I feel like I am missing something important. When I was using === in the if statement, it worked as expected, in other words it printed only "It's a word!". I know there's a difference between the equal and identical operators, but the first example is not equal is it?
== does the type-conversion for you before comparison.
When you did an == with an integer 0, it converted 'word' into the appropriate integer value.
intval() returns 0 when supplied a pure-string, so 0 matched. The other was matched in string-context, and that matched as well.
=== does no such implicit conversion, so it returned true only in one case, when the strings were actually identical.
PHP variables have type.
== checkes equality after conversion to the same type, === also checks the type. Use var_dump to see what the real types are.
See #Cthulhu 's answer above which is much clear.
Apart from that, here is a different example.
strpos() function returns the position of the needle from haystack.
<?php
$pos_a = strpos('apple', 'a'); // a is in the first position.
$pos_b = strpos('apple', 'b'); // there is no b.
if ($pos_a){
echo 'we got a!'."\n";
}
if ($pos_b){
echo 'we got b!'."\n";
}
strpos return FALSE if the needle is not found. But you will see that php does not run any echo statement.
If you var_dumo()'d these 2 values, you will see that $pos_a and $pos_b contain 0 and FALSE.
if statement just failed because 0 and FALSE both are considered FALSE unless you use ===
Now try this:
<?php
$pos_a = strpos('apple', 'a'); // a is in the first position.
$pos_b = strpos('apple', 'b'); // there is no b.
if ($pos_a !== FALSE){
echo 'we got a!'."\n";
}
if ($pos_b !== FALSE){
echo 'we got b!'."\n";
}
Now you will see the desired result as it echos "we got a!".
$a == $b Equal TRUE if $a is equal to $b after type juggling.
$a === $b Identical TRUE if $a is equal to $b, and they are of the same type.
it looks that
if you check 0 against a string with == then PHP returns true:
php -r 'var_dump(0 == "statuses");'
-> returns TRUE
but not if your string has a number at the beginning:
php -r 'var_dump(0 == "2statuses");'
-> returns FALSE
from the specs I get it that it attempts a conversion - in this case the string to number.
so better use ===
http://php.net/manual/en/language.operators.comparison.php
Is there a built-in function for PHP for me to check whether two arrays contain the same values ( order not important?).
For example, I want a function that returns me true for the following two inputs:
array('4','5','2')
array('2','4','5')
Edit: I could have sorted the two arrays and compare them, but as I am such a lazy guy, I would still prefer a one-liner that I can pull out and use.
array_diff looks like an option:
function array_equal($a1, $a2) {
return !array_diff($a1, $a2) && !array_diff($a2, $a1);
}
or as an oneliner in your code:
if(!array_diff($a1, $a2) && !array_diff($a2, $a1)) doSomething();
The best solution is to sort both array and then compare them:
$a = array('4','5','2');
$b = array('2','4','5');
sort($a);
sort($b);
var_dump($a === $b);
As a function:
function array_equal($a, $b, $strict=false) {
if (count($a) !== count($b)) {
return false;
}
sort($a);
sort($b);
return ($strict && $a === $b) || $a == $b;
}
Here’s another algorithm looking for each element of A if it’s in B:
function array_equal($a, $b, $strict=false) {
if (count($a) !== count($b)) {
return false;
}
foreach ($a as $val) {
$key = array_search($val, $b, $strict);
if ($key === false) {
return false;
}
unset($b[$key]);
}
return true;
}
But that has a complexity of O(n^2). So you better use the sorting method.
The array_diff() method above won't work.
php.net's manual says that array_diff() does this:
"Returns an array containing all the entries from array1 that are not present in any of the other arrays."
So the actual array_diff() method would be:
function array_equal($array1, $array2)
{
$diff1 = array_diff($array1, $array2);
$diff2 = array_diff($array2, $array1);
return
(
(count($diff1) === 0) &&
(count($diff2) === 0)
);
}
However I go with the sort method :D
You can use array_diff.
$a = array('4','5','2');
$b = array('2','4','5');
if(count(array_diff($a, $b)) == 0) {
// arrays contain the same elements
} else {
// arrays contain different elements
}
However, a problem with this approach is that arrays can contain duplicate elements, and still match.
You can use array_intersect() instead of array_diff():
$a = array('4','5','2');
$b = array('2','4','5');
$ca = count($a);
$cb = count($b);
$array_equal = ( $ca == $cb && $ca == count(array_intersect($a, $b)) );
Performance wise. solution, where two factors are important:
the more often arrays are matching, the more array_intersect() is fast.
the more arrays are big (more than 10 values), the more array_intersect() is fast.
Depending on these factors, one method can be two or three time faster than the other. For big arrays with few (or no) matching combinations, or for little arrays with lots of matching, both methods are equivalent.
However, the sort method is always faster, except in the case with little arrays with few or no matching combinations. In this case the array_diff() method is 30% faster.
You only need to compare one-way using array_diff() and use count() for the inverted relationship.
if (count($a1) == count($a2) && !array_diff($a1, $a2)) {
// equal arrays
}
If the arrays being compared consist of only strings and/or integers, array_count_values allows you to compare the arrays quickly (in O(n) time vs O(n log n) for sorting) by verifying that both arrays contain the same values and that each value occurs the same # of times in both arrays.
if(array_count_values($a1) == array_count_values($a2)) {
//arrays are equal
}
As an addition to the accepted reply from #knittl
To cover the case, when one of the arrays has additional members:
function areEqualIfOrderIgnored(array $a1, array $a2): bool
{
if (
! array_diff($a1, $a2)
&& ! array_diff($a2, $a1)
&& count($a1) === count($a2)
) {
return true;
}
return false;
}
Or one line, as requested (but not nice)
if (! array_diff($a1, $a2) && ! array_diff($a2, $a1) && count($a1) === count($a2)) {do smth}