How to add to a conditional variable for comparison [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
At this point in time, the below variables are set at the values shown and I need to compare:
(Pseudo code)
if $foo is > 0 AND that $i == $bar +1.
This is just a simplified example. In my code these variables are different at various times.
I'm not having any success figuring out how to do this comparison. Any help is appreciated.
$i = 4;
$foo = 4;
$bar = 3;
if($foo > 0 && ($i == ($bar + 1))) {
echo 'do something';
}
Thanks in advance for any help pointing me in the right direction.

Honestly think your code should work (after the parentheses fixes), but whatever your real logic is instead of the example code you gave, you can try wrapping all the logic areas.
$i = 4;
$foo = 4;
$bar = 3;
if(($foo > 0) && ($i == ($bar + 1))) {
echo 'do something';
}

Related

PHP: loop through an array give wrong result [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have following code:
$source_stat_array = array(0 => 'clicks', 1 => 'impr', 2 => 'spend');
$count = count($source_stat_array);
for ($i = 0; $i < count; $i++) {
echo $source_stat_array[$i++];
}
the result i get is following:
clicks spend
instead of i need
clicks impr spend
can you answer me what is wrong with code?
You are incrementing the value of $i twice.
Try the below code:
for ($i = 0; $i < $count; $i++) {
echo $source_stat_array[$i];
}
You can use foreach it will iterate through each key
foreach($source_stat_array as $key => $value){
echo $value;
}
Read more about foreach , manual

PHP for loop increment by 100000 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I want to loop through $i which starts from 790000000 and echo every 100000 up to 799999999:
790000000
790100000
790200000
790300000
790400000
...
I tried this code but it didn't work:
for ($i=790000000; $i<=800000000; $i+100000) {
echo $i . '<br>';
}
For loop wrong code, you are missing actual code update as $i+100000 does not update variable. Use $i += 100000 instead.
// Here is problem in your code
for ($i=790000000; $i<=800000000; $i += 100000) {
echo $i . '<br>';
}
Update your loop using += instead of i.
Only using + will not increment your $i variable value.
for ($i=790000000; $i<=800000000; $i+=100000) {
echo $i . '<br>';
}

for loop doesn't after looping throw the script [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
i have php script to do for loop, it supposed to loop over the code once and then exit,but theses not happening, it keep looping till i exited manually.
my code
<?php
session_start();
?>
<?php
ignore_user_abort(true);
for( $x=0; $x< 50; )
{
$_SESSION['timeout'] = time();
$st = $_SESSION['timeout'] + 1;
session_unset();
$_SESSION['timeout'] = time();
$st = $_SESSION['timeout'] + 1;
print_r("$st\n");
}
if(time() < $st)
{
file_put_contents('/tmp/phptest1234.txt', 'test');
}
?>
Below is an infite loop. $x always stays at 0.
for( $x=0; $x< 50; )
You need to do:
for( $x=0; $x< 50; $x++)
php man "for"

How can I jump out of a loop but carry on at the next iteration? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
In PHP, break exits a loop at a given point. But is it possible to force the loop to jump to the next iteration at a given point, rather than exiting it completely? Essentially:
for ($i = 0; $i < $foo; $i++){
if ($i == 1){
gotoNextIteration;
} else {
//do something else
}
}
Use continue for this purpose
for ($i = 0; $i < $foo; $i++){
if ($i == 1){
continue;
} else {
//do something else
}
}
Either use continue; or you can do something like this as well
for ($i = 0; $i < $foo; $i++){
if ($i != 1){
//do something
}
}
you don't need anything else.
Yes .. i have built many type of these loops for dynamic content:
for ($i = 1; $i <= 20; $i++) {
if ($i == 1) {
// write table header
} else if ($i == 20) {
// write the table footer
} else {
// fill the table columns
}
}
This is a basic example, but I use it for data iteration (like image galleries) and dynamic tables, and more. but I bow to Abhik Chakraborty's Answer as i was not familiar with "continue". I love learning new stuff.

PHP:How To Initialize Variable? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have one question to you, and I hope you will help me. I want to write calculator but i don't have chance tu change operations , I can only use addition operation. I don't know what happened. can anyone help me?
look at it, what happened?
<?php
$x = isset($_POST['field']);
if($x == 1){
echo $x + 5;
}
elseif($x == 2){
echo $x - 5;
}
?>
You code is wrong as this line will evaluate only as 0 (false) or 1 (true) if condition is met:
$x = isset($_POST['field']);
You need to do it that way:
$x = isset($_POST['field']) ? $_POST['field'] : 0;
(where 0 is default value, $x is assigned to in case there's no $_POST['field]` set.
$x = isset($_POST['field']) ? (int)$_POST['field'] : 0;
You want the value, not the boolean if it's set or not.

Categories