I have a for loop within a for loop and right before I enter an if statement, two values echo out and are equal. When I evaluate (with the if statement) whether or no they are equal, the values do not evaluate to even. Could there be something that I am not seeing that is wrong with this statement?
for($x = 0; $x < count($movies_total);$x++){
for($j = 0; $j < count($ask_array);$j++){
echo $movies_total[$x]->question_id.' '.$ask_array[$j].'<br>';
if($movies_total[$x]->question_id == $ask_array[$j]){
echo 'no';
}
}
}
You should dump both your variables ( $movies_total[$x] , $ask_array ) for us to know the problem but my guess would be you have white spaces. You can compare variables after trim:
if( trim($movies_total[$x]->question_id) == trim($ask_array[$j]) )
Related
I am new in PHP. I know a little bit about how for loops work. Using this format: (initial; conditions; increment) why doesn't this loop work? What kind of conditions are allowed?
Here is the code:
<?php
$name = "Biswajit";
for ($i = 1; $name[$i] == "w"; $i++) {
echo "hello";
}
?>
The condition can be any expression whatsoever that can be usefully evaluated to true or false.
The condition is tested once at the start of each iteration of the loop. The loop ends as soon as one of these tests gives false.
Your example condition $name[$i] == "w" is syntactically valid, but will end the loop immediately, because $name[1] is i, not w. (Note that string characters start from 0.) Maybe you meant to write $name[$i] != "w".
So a for loop in PHP is pretty much like any other for loop in any other language.
Basically, you'll be able to put in condition that results to a boolean (notably ==, >, <, >=, <=).
Here's a basic example of a for loop in PHP printing a number:
for($i = 0; $i < 5; $i++) {
echo $i;
}
Hope this simple example can help! :)
the following code should return only "5" once (for first ittiration)
instead returning "5" for each iteration. AS "5555555555555555555555555"
$col11=5;
for($x=1; $x<=5; $x++) {
for($y=1; $y<=5; $y++) {
if("$col{$x}{$y}") {
echo $col11;
}
}
}
Your if condition is simply checking whether the string provided is a truthy value. Yeah, you are interpolating the $x and $y values but it's still just a string. What you can do is put together the name of the variable as a string, then use $$ to access the variable name dynamically. You can also use isset() to determine if the variable has been set.
<?php
$col11=5;
for($x=1; $x<=5; $x++){
for($y=1; $y<=5; $y++){
// Store desired variable-name as string in a temp variable
$variable = "col{$x}{$y}";
// Check this temp variable's existance by using referece variable ($$)
if (isset($$variable)) {
echo $$variable;
}
}
}
?>
Check Output here
Change your if condition like this:
$col11=5;
for($x=1; $x<=5; $x++){
for($y=1; $y<=5; $y++){
$val = "col{$x}{$y}";
if($$val)
{
echo $col11;
}
}
}
I don't understand when you want to echo $col11 ? Maybe you want to echo col for x=1 and y=1 so you can make this condition :
if ($x==1 && $y==1)
I'm not sure what you research ?
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);
I have a variable like this :
$test = "002,003,004,005,012,032,045";
I'd like to see if any number in a range of numbers appears in this string.
eg : if ($test == [010-015]) echo "found";
this would check the $test variable for any number of 010,011,012,013,014,015 and if any is found then echo found.
How do I do this ?
Only way I can think of is to loop through each number .. but there must be a better way !
Thanks
hope this helps you
$test = "002,003,004,005,012,032,045";
foreach (explode(',',$test) as $i)
{
if($i > 10 && $i < 15)
{
echo 'found';
}
}
I'am working in CodeIgniter, and want to create a dynamic breadcrumbs by using $this->uri->segment($i) function.
How should I write correctly for() statement if I want to test the 2nd expression that is not equal to FALSE ? It gives me an infinite loop and I don't know why.
here is my code:
for($i = 1; $i !== FALSE; $i++){
var_dump($this->uri->segment($i));
}
For exemple, first 3 reccursion should output a different strings, starting from 4th reccursion, it gives me false but it's not working here, know someone why ?
Your $i variable is an integer, and will never be equal to FALSE.
Maybe you are looking for comparing $this->uri->segment($i)?
for($i = 1; $this->uri->segment($i) !== FALSE; $i++){
var_dump($this->uri->segment($i));
}