how to change the while loop condition depending on stuff? - php

by this question what i mean is that if, by example, someone's username is "bob" then the while loop condition will be ($i < 10), and if the username is something else then the while loop condition will be ($i > 10)
if($username == "bob")
{
//make this while loop condition: ($i < 10)
// it means: while($i <10){ so stuff}
}
else
{
//make the while loop condition: ($i >10)
}

Make do_stuff a function, then this is perfectly readable (although special-casing 'bob' seems doubtable at best).
if($username == "bob")
{
while($i<10) {
do_stuff();
}
}
else
{
while($i>10) {
do_stuff();
}
}

while( ($username == 'bob' && $i <10 ) XOR $i > 10){}
$username == 'bob' will be evaluated first if it comes out to be true then $i < 10 is evaluated.
$var1 XOR $var2 is true only when one of $var1, $var2 is true but not both.
But I myself will go with this.

Try making two different while loops within the if and else blocks.

Is this what you mean? You can just take a different path depending on the success or failure of the if. If the code inside is the same, just call a method to avoid duplicating it.
if($username == "bob")
{
//make this while loop condition: ($i < 10)
// it means: while($i <10){ so stuff}
while ($i < 10) {
call_method();
}
}
else
{
while ($i > 10) {
call_method();
}
}

To keep your code simple:
if($username == "bob")
{
while ($i<10){
}
}
else
{
while ($i>10){
}
}
There are other better ways to handle it but they may make your code difficult to understand like using eval which I dont like.

Related

use OR in if codition to print reverse results in php

I am trying echo result "OK" if ( 1 either both ) variable true, i did so far like this
<?php
$user_id = $_SESSION['user_id'];
$point= "select points from users where id = $user_id "; // in db right now points = 2000
$flag= "select m_boost from users where id = $user_id "; // in db right now flag = 1
?>
<div class="box border">
<div class="box-title">
<?php
if($point < 1000 || $flag = 0) {
echo "not ok";
} else { ?>
echo "ok";
<?php }?>
</div>
it's working if i do like this
if(($point == '2000') || ($flag == '0') ){
but i don't want == operator for $point i want less than < $point < '999'
The problem is :
Keep getting result " Not Ok " even one variable (flag = 1) is true in db
Expected Results:
i want to print "OK" if $point > 1000 or flag == 1,
try this
when you using OR condition you should careful about condition and login. you should implement logic in if condition instead of else.
if($point > 1000 || $flag ==1) {
echo "ok";
}else {
echo 'Not ok'
}
or AS YOU WANT
if($point < 1000 ) {
echo "Not ok";
}else if($flag ==0) {
echo 'Not ok'
}esle {
echo 'ok'
}
or you can use this way
$a=false;
if($point < 1000 ) {
$a=true;
}else if($flag ==0) {
$a=true;
}esle {
$a=false;
}
// you can use this variable in your condition.
if($a) {
echo "ok";
}else {
echo 'Not ok'
}
When you are using OR, if the first condition is met the second is disregarded.
Also, make sure you use double equals (==) for comparison, not single equals (=) which means assignment.
Therefore you want to replace this:
if($point < 1000 || $flag = 0) {
With one of these:
SWAPPED AROUND
if($flag == 0 || $point < 1000) {
or
USING && INSTEAD
if($point < 1000 && $flag == 0) {
Depending on what behaviour you're looking for. It's a little unclear - so any additional clarification from you would be helpful.
Once you get something that you think is working, try to test all possible combinations so that you can be confident it works how you wish.
$flag = 0
You're setting the variable's value to 0.
Example:
$flag = 1;
if($a < $b || $flag = 0){ //$flag's value is 0 now.
...
...
}
In conditions comparisons, the right operator is "==".
if($point < 1000 || $flag == 0) {
List of comparison operators
but i don't want == operator for $point i want less than < $point <
'999'
Didn't fully understood your goals, but maybe:
For checking a value in a range the right logical operator should be "and" (&&);
if($point > 0 && $point < 999){
List of logical operators
Update:
if $point > 1000 or flag == 1
if($point > 1000 || $flag == 1){
echo "ok"
}
if (!$flag && $point < 1000)
{
echo "Not OK";
} else {
echo "OK";
}
Writing this into a truth-table:
flag point result
0 < 1000 Not OK
1 < 1000 OK
0 >=1000 OK
1 >=1000 OK
Worked for me now as per my question
i think we are all here for some contribution reason, flagging down a question is not a way, if you got solution than respond otherwise my question was 100% clear.

if and else statement mixed up

I am doing the following if else statement below but number (//1) and number (//4) get executed at the same time, I am finding it abit hard to understand why.
<?php
//1
if($a == 1 && count($b) == 0) {
// do this
}
//2
elseif ($a == 1 && count($b) > 0) {
// do that
}
//3
if($a== 0 && count($b) == 0) {
// do a different thing
}
//4
else {
// do the last thing
}
?>
I have done this and it works but i think the should be a more suitable way for not using elseif for this.
else if($a== 0 && count($b) > 0) {
// do the last thing
}
but number (//1) and number (//4) get executed at the same time
It's because you don't have else before the if on //3
//3
if($a== 0 &&
Change to elseif($a== 0 &&
At the moment you have two separate IF conditions
You're missing a closing brace after your first if.
Also, you have a weird operator inside your first condition : $$. Maybe you intended to type &&?
$a = 10;
if ($a == 5) {
echo 'ok';
} elseif ($a == 10) { // $a is equal to 10, so it executes;
echo 'not_ok';
}
if ($a > 20) {
echo 'ok_ok';
} else { // $a is not >20 so else statement executes
echo 'not_not';
}
final result: not_oknot_not
If you are performing such tests on one and the same assignee, but different values, you might not want to execute more than one?
I guess you need elseif where third block is if
if ($a == 5) {
echo 'ok';
} elseif ($a == 10) { // $a is equal to 10, so it executes and stops the block;
echo 'not_ok';
} elseif ($a > 20) {
echo 'ok_ok';
} else { // $a is not >20, but the block was stopped on first elseif
echo 'not_not';
}
produces not_ok
Even if you move the else statement after the first elseif block as was suggested
if ($a > 20) {
echo 'ok_ok';
}
will execute, and if it's true, it will produce result, which again will result in double result
You might want to do this...
if ($a == 1 && count($b) == 0) {
// do this
}
//2
elseif ($a == 1 && count($b) > 0) {
// do that
}
elseif ($a== 0 && count($b) == 0) {
// do a different thing
}
else {
// do the last thing
}
The reason they get executed at the same time is that... Well technically they're not executed at the same time since it's procedural, but they both get executed because they are both different if else conditions. If you want only 1 execution, you should combine them :)

Algorithm to check if variable changed since start of the loop PHP

//Initialize
i=0;
foreach(runs 8 times) {
if(some condition that sometimes happens) {
$i = $i + 3;
} else if (some other condition that sometimes happens) {
//Do nothing with i
} else if(some condition that sometimes happens) {
$i = $i -4;
}
if(what condition do i put here to check if $i changed from the start of the loop?) {
//nothing changed, do action 1
} else {
//Else something changed and do action 2.
}
}
Hey guys, i'm sure this is obvious, but I'm having a hard time with this algorithm, and each time I need to make sure that $i isn't the same as it was at the start of the loop and do Action 1 or Action 2 based on that distinction.
For iteration 1, I could put if($i == 0) {, but then on iteration 2 and 3 that has the potential to fail.
You should be able to use another variable and save the value of $i in that one if it has changed. Then you can always compare that variable to $i when you check for a change.
I don't think you should have the $i inside the loop though.
$previousValue = null;
$i=0;
foreach(runs 8 times) {
//Initialize
if(some condition that sometimes happens) {
$i = $i + 3;
} else if (some other condition that sometimes happens) {
//Do nothing with i
} else if(some condition that sometimes happens) {
$i = $i -4;
}
if($previousValue == $i) {
//nothing changed, do action 1
} else {
//Else something changed and do action 2.
$previousValue = $i;
}
}
Just remember the state at the beginning and check it afterwards:
foreach(runs 8 times) {
//Initialize
i=0;
oldi = i;
if(some condition that sometimes happens) {
$i = $i + 3;
} else if (some other condition that sometimes happens) {
//Do nothing with i
} else if(some condition that sometimes happens) {
$i = $i -4;
}
if(i != oldi) {
//nothing changed, do action 1
} else {
//Else something changed and do action 2.
}
}
Try this:
$previous = 0;
foreach(runs 8 times) {
//Initialize
$i = 0;
if(some condition that sometimes happens) {
$i = $i + 3;
} else if (some other condition that sometimes happens) {
//Do nothing with i
} else if(some condition that sometimes happens) {
$i = $i -4;
}
if($i != $previous) {
//nothing changed, do action 1
} else {
//Else something changed and do action 2.
}
$previous = $i;
}
$ichanged = false;
i=0;
foreach(runs 8 times) {
//Initialize
if(some condition that sometimes happens) {
$ichanged = true;
$i = $i + 3;
} else if (some other condition that sometimes happens) {
//Do nothing with i
} else if(some condition that sometimes happens) {
$ichanged = true;
$i = $i -4;
}
if($ichanged == false) {
//nothing changed, do action 1
} else {
//Else something changed and do action 2.
$ichanged = false;//RESET for next iteration
}
}
its simple, just add one more variable inside the loop and initialise it to i value every time the loop iterates
try this code :
//Initialize
i=0;
j=0;
foreach(runs 8 times) {
j=i;
if(some condition that sometimes happens) {
$i = $i + 3;
} else if (some other condition that sometimes happens) {
//Do nothing with i
} else if(some condition that sometimes happens) {
$i = $i -4;
}
if(j==i) {
//nothing changed, do action 1
} else {
//Else something changed and do action 2.
}
}
Since only one of the cases will be evaluated, why not just call the function that does something when $i changes immediately?
$i = 0;
foreach(/*runs 8 times*/) {
if(/*some condition that sometimes happens*/) {
iChanged($i, $i + 3);
$i -= 3;
} elseif (/*some other condition that sometimes happens*/) {
iDidntChange($i);
} elseif(/*some condition that sometimes happens*/) {
iChanged($i, $i - 4);
$i -= 4;
} else {
iDidntChange($i);
}
}

Making many If statments into One statement. Php

function rawtransform{
if ($raw>=500 && $raw<=550){
$score= 1;
}
if ($raw>=550 && $raw<=600){
$score= 2;
}
if ($raw>=600 && $raw<=650){
$score= 3;
}
if ($raw>=700 && $raw<=750){
$score= 4;
}
if ($raw>=750 && $raw<=800){
$score= 5;
}
if ($raw>=800 && $raw<=850){
$score= 6;
}
if ($raw>=850 && $raw<=900){
$score= 7;
}
if ($raw>=900 && $raw<=950){
$score= 8;
}
if ($raw>=950 && $raw<=1000){
$score= 9;
}
}
This seems very basic and not very well coded. (I am only learning php )
Can anyone offer a better way of doing this? maybe a single if statement. I think there is a way just cant get my head round it.
Thanks
How about just using math?
function rawtransform($raw) {
$score = (int)($raw/50)-9;
}
You may want to add a range check for the input, though.
You can create a list of conditions, and loop through the and apply the if.
$conditions = array(
array(500, 550, 1), // greater than value, lesser than value, assignment value
array(550, 600, 2),
array(650, 700, 3) // add the rest of the conditions
);
foreach($conditions as $condition) {
if($raw >= $condition[0] && $raw <= $condition[1]) {
$score = $condition[2];
}
}
if ($raw >= 500 && $raw<= 1000){
$score = ceil(($raw-500)/50);
}
You can use if...else constructs.
if ($raw>=500 && $raw<=550){
$score= 1;
}
elseif ($raw>=550 && $raw<=600){
$score= 2;
}
elseif ($raw>=600 && $raw<=650){
$score= 3;
}
That way, if the script encounters a size of, say, 575 it won't even bother to go through the following conditions.
In your given example, the script can be simplified by:
function rawtransform($raw) { $score = floor(($raw - 450)/50); return $score; }
Your logic seems to be that you are subtracting 450 from raw, then dividing it by 50 and rounding down to the nearest whole number. (There are problems in your implementation however, as if raw is a factor of 50, it will meet the requirements for two of the if statements and there is a condition missing for when it falls between 650 and 700.)
You could do this as follows:
floor(($raw-450)/50)

PHP creating a while loop that is dependent on multiple variables that are independent of one another?

I'm stumped on this and my searches aren't turning up anything relevant.. I need to do a while loop that will continue if either of 2 variables are true... as far as I can tell you can't do a "while ($var = '' and $var2 = ''); so I tried this, basically I figured I could just set 2 different if statements so that it would change the variable "continue" if it went past 4 iterations (if $i >= 4), however this just gives an infinite loop:
function whiletest () {
$i = 1;
do {
echo 'output';
if ($status != 'true') {
$continue = 1 ;
}
if ($i >= 4) {
$continue = 2 ;
}
$i++ ;
} while ($continue = 1 );
}
Are you looking for a construct like this:
while($var1 == 'value1' OR $var2 == 'value2') {
...
}
That will continue to run while either condition is true.
Why wouldn't the following work?
while (($condition1) || ($condition2)) {
// loop stuff
}
As long as the expression within the while parens is true, the loop will execute.
The while statement evaluates a boolean expression. You should be able to write out:
while( ($status != true) && ($continue == 1) ) {}
Also in your code (if its a c/p), you have $continue = 1. This will always evaluate to true.
EDIT:
while (($status) && ($i < 4))
As for the last while, it just looks like an infinite loop to me.
You shouldn't need the $continue variable. This should do the trick:
$i = 1;
do {
//do other stuff here (possibly changing the value of $status)
echo 'output';
$i++;
} while ($status != 'true' && $i < 4);
Keep in mind that this will always run the loop at least once. If $status might start out as 'true' and you want the loop to run zero times if it is, you want:
$i = 1;
while ($status != 'true' && $i < 4) {
//do other stuff here (possibly changing the value of $status)
echo 'output';
$i++;
}

Categories