How to implement for loop logic [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Is there a simple logic to implement for loop, demending on delimeter?
if $a>b for($i=a; $i>b; $i--)
else for($i=a; $i<b; $i++)
But I need it to be done in one loop I cant do something like
$start = $a<$b? $a : $b;
Because I need loop to always start with $a and go toward $b, maybe there another way?Can i have reason for downvoting? And what is unclear in my question, peoples if you dont understand question refrain from touching it.

Hope this helps
$inc = $a < $b ? 1: -1;
for ($i = $a; $i != $b; $i += $inc) {
/* some code here */
}
Explanation:
First step is estimation of the increment meaning whether to increment or decrement $a to reach $b.
Obviously if $a < $b then increment needed or else decrement. $i += $inc is the generic statement that adds +1 or -1. Adding +1 is increment and adding -1 is decrement.
Ultimately the loop exit condition is $i != $b, hoping this condition will be met atleast once in the increment/decrement.

Your question contains some logical error. Although syntax is correct.
if $a<b for($i=a; $i>b; $i--)
Here, $a is smaller than b and in for loop you give condition $i>b which will never meet because $i and $a are already smaller than b. So, this loop will not run even for a single time. This is also same for else because here also loop will not execute for a single time.
Is that you want..?

Related

How to use a sign plus in a PHP variable and get result? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to store a plus sign (or any other sign) in a PHP variable and make arithmetic operation.
I tried to store sign plus in variable $c. But PHP it's not accepting it.
$a = 3;
$b = 5;
$с = "+";
But when I echo it,
echo $a. $c. $b;
it gives me the result 35.
I tried enothoer one way, but it doesn't work:
if ($c === "+"){
echo $a + $b;}
else {
echo $a - $b;}
The result is -2.
How get result $a + $b?
You're using a combination of the Cyrillic and latin letter c in your code.
If I paste your code on this site to show each unicode char, the first c is an \u0441 (Cyrillic), how ever, the c inside the if shows 0x63 (latin), so PHP is unable to match those and trows:
PHP Notice: Undefined variable: c
Fixed:
<?php
$a = 3;
$b = 5;
$c = "+";
if ($c === "+"){
echo $a + $b;}
else {
echo $a - $b;}
Try it online!

PHP: Is there a difference between use of ++ or +=1 [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
When using increment on variables in Javascript, lint says that x += 1 is preferred over x ++.
But what about in PHP?
Is there any difference between the += and ++ or does it just not really matter?
Well whatever you might say about conventions, try running the following...
$i = 1; $s = 's';
$i++; $s++;
echo $i.'<br>'.$s.'<br>';
$i = 1; $s = 's';
$i += 1; $s += 1;
echo $i.'<br>'.$s.'<br>';
the output is somewhat unexpected...
2
t
2
1
so I would say it could matter very much which is chosen!
x += 1 is rather equivalent to ++x.
All those expressions (x += 1, x++ and ++x) increment the value of num by one, but the value of x++ is the value x had before it got incremented.

Logic to divide any integer number into same five parts [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 8 years ago.
Improve this question
Want to divide random integer number into equal five parts. And insert those values into array. Can any one tell me the logic for that.
Eg. I have 15 as my number. After dividing. It should generate the array as below.
$myArray = array('3','6','9','12','15');
Thanks in advance.
function getParts($number, $parts)
{
return array_map('round', array_slice(range(0, $number, $number / $parts), 1));
}
print_r(getParts(15, 5));
Explanation: range() generates the array of values starting with 0, ending when it reaches $number and using the step $number/$parts. It will get $parts+1 floating point numbers. array_slice() removes the first item (which is always 0). array_map() applies the function round() to each element to get the nearest integer.
Divide and create a loop to fill the array...
$total = 15;
$divide = 5;
$base = $total / $divide;
$arr = array();
for($i = 1; $i <= $divide; $i++) {
$arr[] = round($i * $base);
}

Define array with variable index [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
My code:
$rank_content = file('https://www.championsofregnum.com/index.php?l=1&ref=gmg&sec=42&world=2');
$line_count = 0;
//initializing only the first few keys because of no reason (the latter ones aren't in use yet)
$rankNameArr = array(0=>42,1=>42,2=>42,3=>42,4=>42,5=>42,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72);
$rankRlmpArr = array(0=>42,1=>42,2=>42,3=>42,4=>42,5=>42,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72);
while ($line = array_shift($rank_content)) { // retrieving line after line of the website, does work indeed
$line_count += 1;
if(strpos($line, "Warrior #") || strpos($line, "Archer #") || strpos($line, "Mage #"))
{
$rankNameArr[$line_count] = $line_count + 1; // HERE nothing happens
$rankRlmpArr[$line_count] = $line_count + 2; // nothing happens here, too
}
}
Why does
echo $rankNameArr[2];
echo $rankRlmpArr[2];
give me the value 42 instead of the correct value? If I replace $line_count with a real number, the script works properly.
My intention is to store the value $line_count + 1 into $rankNameArr at position $line_count. Not very complicated actually
EDIT -------------
Forget everything above, please. I finally reduced the script to the actual problem:
$arr = array(0=>42,1=>42,2=>42,3=>42,4=>42,5=>42,6=>42);
$counter=0;
for($i=0;$i<7;$i++) {
$arr[$counter]=$i;
}
echo $arr[5];
This sadly echoes 42. I have no idea how to have $arr[$counter] store the actual value of $i.
I have no idea how to have $arr[$counter] store the actual value of $i.
$counter is set to 0, so $arr[$counter] is the same as saying $arr[0]. If you echo $arr[0] you'll see that it is being changed while the rest are left alone, which means your code is working.
However if you want $counter to increment also, you just need to tell it to do so:
$arr = array(0=>42,1=>42,2=>42,3=>42,4=>42,5=>42,6=>42);
$counter=0;
for($i=0;$i<7;$i++) {
$counter++;
$arr[$counter]=$i;
}
echo $arr[5];

Where could I possibly be messing up in a for loop? [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 8 years ago.
Improve this question
Here is what I am doing (php)
<?php
for($i = 0; $i <= 30; $i+2)
{
echo $i;
}
?>
It drives me nuts,coz it does not work [prints nothing, browser keeps trying to load]. But if I change $i+2 to $i++, it works, and if I change it to $i+1, that does not work either.
I am out of my wits. What is going wrong?
It drives me nuts,coz it does not work [prints nothing, browser keeps trying to load]. But if I change $i+2 to $i++, it works, and if I change it to $i+1, that does not work either.
$i++ is equivalent to $i = $i + 1, note the assignment operator =, it isn't present here $i + 2 adds but doesn't update ...
since $i is never updated, you have an infinite loop, where the script will probably reach the allowable time for processing and terminate.
http://php.net/manual/en/function.set-time-limit.php
Set the number of seconds a script is allowed to run. If this is reached, the script returns a fatal error. The default limit is 30 seconds or, if it exists, the max_execution_time value defined in the php.ini.
You need to change $i+2 to $i = $i + 2 or $i += 2.
$i++ means $i += 1.
for($i = 0; $i <= 30; $i += 2)
{
echo $i;
}
You need to provide some increment /decrement value to the for loop after each iteration.
$i+2 does not change the value of i. So what is happening in your case is the for loop finds the same value of i for each successive iteration.Consequently the termination condition is never met.The for loop is hence stuck in an infinite loop.
Change: $i+1 to $i=$i+1 or $i+=1.
$i++ actually means $i=$i+1.
You have to change to $i=$i+2, because $i++ is equal to $i=$i+1.

Categories