This is driving me nuts. I've spent a couple hours trying to debug this but I'm getting nowhere. $total should increment by 1 everytime $result != 'REMAKE' but it's only incrementing whenever $result == 1 (and not when $result == 0). I cannot figure out why. I've tried $total += 1 but it makes no difference.
didAccountWinMatch() will return either 0, 1, or 'REMAKE'.
$total = 0;
$wins = 0;
if (isset($match)) {
foreach ($match as $key => $value) {
$result = didAccountWinMatch($accountId, $match[$key]);
if ($result != "REMAKE") {
$total ++;
$wins += $result;
}
}
} else {
$total = 0;
$wins = 0;
}
Use !== instead of !=. If you use the loose comparison operator between an integer and a string, the string is converted to an integer first. Converting the string REMAKE to an integer results in 0, so the code is equivalent to
if ($result != 0)
when $result is an integer.
The strict comparison operator !== doesn't perform type conversion; if the operands are different types it returns false.
Related
I made an iterative loop and created two matrices and I want when I find a specific name I put the corresponding value from the other row in the J variable but I gave me the wrong result which is the number 8 where the number was supposed to appear 7
// my code:
<?php
include 'DB.php';
$db=DB::getInstance();
//$posts = $db->table('posts')->get();
//echo json_encode($posts);
//$users = $db->table("posts")->Qget();
$rows = $db->table('posts')->get();
$filter_value1 = [];
$filter_value2 = [];
$i=0;
$j=0;
foreach($rows as $row){
$filter_value1[]=$row->name;
$filter_value2[]=$row->user_id;
if($filter_value1[$i]="gmal"){
$j= $filter_value2[$i];
}
// echo "$row->name <br>";
$i++;
}
echo($j);
= : This is an assignment operator in any Language suppose we want to assign some value to a variable we will use (equal to) = sign. It does not return anything. e.g.
$name= "gmal";
== : This is a comparison operator. If we want to compare two values or values hold by variables we should use ==. This operator returns True /False bases on comparison e.g.
if("22" == 22) it will return true
=== : Checks the values as well as the type of operands.
if("22" === 22) it will return false
if($name == "gmal"){
echo "Name is : {$name}";
}
so in your code change the following line
if($filter_value1[$i]="gmal")
to
if($filter_value1[$i] == "gmal")
i want to compare two values in one php array but the code stops when i compare even when the conditions is true i want to know how can i compare the two values here is my code :
$i=0;$cmpt=0;
foreach($newarray as $newarray1){
$j=0;
while ($newarray1[$i]!==$newarray1[$j]){ // the iteration dont get in here even when the condition is true
$j+1;
var_dump($j);
}
if ($i=$j){
$couleur[]=$Tcouleur[$cmpt];
$cmpt+1;
}else{
$couleur[]=$Tcouleur[$j];
}
$i+1;
}
var_dump($couleur);
This is probably because of the line
$j+1;
your both variables ($i and $j) are not being updated in the while loop, causing an infinite loop. ( checking the same values all the time, if the condition is true an infinite loop, else the code will never enter the loop and exit. )
change $j+1; with either $j++; or $j = $j + 1;
Moreover as #apomene shows,
if your array can have multiple types,
The !== operator checks both the type and the equality. If your array has same types ( for e.g int ) this would not create a problem tho. With the same types !== and != are the same thing practically. Else, it (!==) also checks for type equality. To elaborate,
$a = 1;
$b = '1';
$c = 2;
$d = 1;
$a == $b // TRUE ( different type, equal after conversion - char <-> int)
$a === $b // FALSE( different types - int vs char)
$a == $c // FALSE( same type not equal)
$a === $d // TRUE ( same type and equal)
Further reading available in this question.
Lastly, you seem to have a confusion between assignment and comparison of a variable. ( $i = $j vs $i == $j )
Check the php manual for assignment vs comparison of variables.
In your while loop, does $j+1 should not be $j++ or $j = $j + 1 ?
I know it's not the problem your asking...but same for your $i+1 at the end and your $cmpt
Now I think you want this :
$values = ['abc','def', 'hij','klm', 'def', 'klm','nop'];
$couleurs = ['rouge','vert','bleu','jaune','rose'];
$couleurPourValeur = [];
$increment = 0;
foreach($values as $value){
if(!isset($couleurPourValeur[$value])){
$couleurPourValeur[$value] = $couleurs[$increment];
$increment++;
}
}
print_r($couleurPourValeur);
Why does this always return true:
$s = '334rr';
$i = (int)$s;
if ($i == $s) {
echo true;
} else {
echo false;
}
If I echo $i it gives the value of 334, which is different from $s which is 334rr.
From the manual:
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.
So: ($i == $s) is the same as ($i == (int)$s) for the values you've given.
Use === to avoid type-juggling.
When compare string with integer using ==, string will try to case into integer.
Try this
$s = '334rr';
$i = intval($s);
if ($i == $s) {
echo true;
} else {
echo false;
}
Comparing strings to an int is not recommended. You should use the === instead which will verify the same data type as well as the same value.
PHP converts the $s string to an integer when comparing to another integer ($i).
It basically does this (well, i don't know what it does internally, but it boils down to this):
if($i == (int) $s)
Which makes the statement true
i'm practicing php at nowadays. i have one question.
i see one = on some while loops. often on mysql queries. so what does it stands for?
two == means equal. but single = ? can you tell me the logic behind this?
Single = means "assignment". An assignment always assigns to a variable and returns the result. So, for example:
while($row = mysql_fetch_array($results)) {
// ...
}
While mysql_fetch_array($results) returns a valid array, it loops. When it returns null (meaning that's the end of the results) the loop exits.
while ($row = mysql_fetch_row($query))
{
//do stuff
}
This keeps querying the result handle until a NULL value (end of result) is found. $row then contains the row fetched from the call so it can be used for processing.
It is also "shorthand" for checking for a NULL result:
while (($row = mysql_fetch_row($query)) != null)
{
//do stuff
}
It can also replace a for loop:
for ($i = 0; $i < mysql_num_rows($query); $i++)
{
//do stuff
}
All are equivalent.
single = is for assignment
$a = 9;
== is for comparison
if($a == 9) echo 'hello';
http://www.w3schools.com/PHP/php_operators.asp
That's the assignment operator.
While the current value of 'x' can be successfully assigned to variable 'y', continue on.
Here is an alternative:
$row = mysql_fetch_assoc($thing);
while (isset($row)) {
// do stuff
$row = mysql_fetch_assoc($thing);
}
You can see that it's a bit less typing (and the code is probably more efficient) here:
while (($row = mysql_fetch_assoc($thing)) != null) {
// do stuff
}
I have the following code
while($row = $usafisRSP->fetch_assoc())
{
$hidden_keys = array('Applicantid', 'unique_num', 'regs_time' ....);
$hidden_fields = array_intersect_key($row, array_fill_keys($hidden_keys, NULL));
$hidden_values = array();
foreach ($hidden_fields as $key => $value) {
// fill the values array using the values from fields array
$hidden_values[$value] = "$key = ".base64_decode($value)."";
if(base64_decode($value)== 0)
{
$hidden_values[$value] = "";
}
echo $hidden_values[$value];
The question is about "if($hidden_values[$value] == 0)" ... Basically I want to do not display/echo the $hidden_values[$value] if it's value of $value is 0. Sometimes $value is 0 or some words like (23 avenue).
I think you ran into three catches with PHP type comparisons and equalities:
Any string not beginning with a number will always loosely equal 0. So basically, if(base64_decode($value)== 0) will likely always resolve to true, even if decoded $value is "Adam".
Return value of base64_decode is a string, so if 0 is the result, it will be string 0, not integer 0. This means if(base64_decode($value) === 0) wouldn't even work if decoded $value is "0". Another catch is base64_decode may return false on errors, again failing this strict equality check.
A non-empty string (other than "0") will always loosely equal true. So this is the only comparison you really need for your case.
I think this is what you want, replacing the last 5 lines...
if(base64_decode($value)) echo $hidden_values[$value];
else $hidden_values[$value] = "";
} // closing your for loop
Is this what you're looking for?
foreach( $hidden_values as $value ) {
if( $value !== 0 ) {
echo $value;
}
}