I have a for loop and inside of it I have a if statement something like this:
$output = "";
$limit = 550;
for ($i = 1; $i <= $limit; $i++) {
if($i < 10){
$output .= my_function($i*1);
}elseif($i < 20){
$output .= my_function($i*2);
}elseif($i < 30){
$output .= my_function($i*3);
}
//elseif 30 => 550
}
Problem is I find it very tedious to have to continue this elseif statement down to 550. Is there any way to do this without writing 55 elseif statements.
Your thinking is exactly right. Having to repeat that much code is a sure sign that something is off.
In this case, the solution is pretty simple. You just want to use integer division to knock off the ones digit, which you don't care about. You also need to adjust by one since you're checking less than and not less than or equal.
$output = "";
$limit = 550;
for ($i = 1; $i <= $limit; $i++) {
$temp = (int) ($i / 10) + 1;
$output = my_function($i*$temp);
}
for ($i = 1;$i <= 550;$i++) {
$multiplier = (int) ($i / 10) + 1;
$output = my_function($i * $multiplier);
}
My first thought is that you could use:
$output = "";
$c = 0;
$limit = 550;
for ($i = 1; $i <= $limit; $i++) {
if (($i + 1) % 10 == 0) {
$c++;
$output = my_function($i*$c);
}
}
How about
$output = "";
$limit = 550;
for ($i = 1; $i <= $limit; $i++) {
$num = ($i / 10) + 1
$output = my_function($i*$num);
}
Try this:
$output = "";
$limit = 550;
for ($i = 1; $i <= $limit; $i++)
{
$output = my_function($i * (1 + (int)($i / 10)));
}
The solution should be as simple as the following:
$output = "";
$limit = 550;
for ($i = 1; $i <= $limit; $i++)
{
$num = ceil($i/10);
$output = my_function($num, $num-1);
}
hmm the original code seems to have changed since I started typing this. The my_function function now only has 1 input when it originally had 2.
for ($i = 1; $i <= $limit; $i++) {
$output = my_function(i*floor($i/10+1));
}
Point is not really about this question:
You reassign output on every loop iteration, but only last will be vailable after loop? Need you $output[]= or $output.= ?
Related
I'm trying to look for a number with maximum divisors in a range of 1 - 10000.
I succeeded, but then I wish to verify if there exist more than two max divisors and print them out. My array is really the problem. How can I clear an array and assign a new integer to it in an if else if statement?
Here is what I have tried:
function countDivisors(){
$input = 10000;
$maxNumOfDiv = -1;
$intWMaxDivs = -1;
$curNumOfDiv = 0;
$arr = array();
for($i=1; $i <= $input; $i++) {
$curNumOfDiv = 0;
for ($j = 1; $j < $i; $j++){
if ($i % $j == 0)
$curNumOfDiv++;
}
if($curNumOfDiv = $maxNumOfDiv){
$arr[] = $i;
$intWMaxDivs = $i;
$maxNumOfDiv = $curNumOfDiv;
} else if($curNumOfDiv > $maxNumOfDiv){
$arr = array();
$arr[] = $intWMaxDivs
$maxNumOfDiv = $curNumOfDiv;
}
}
for ($i; $i < count($arr); $i++){
echo $arr[$i]['intWMaxDivs'];
echo $arr[$i]['maxNumOfDiv'];
}
$div = [];
$maxDivKey = false;
$maxDiv = 0;
for($i = 1; $i <= 10000; $i++) {
for ($j = 1; $j < $i; $j++){
if ($i % $j == 0){
$div[$i][] = $i.'/'.$j.'='.$i/$j;
}
if($j == $i-1){
$count = count($div[$i]);
$div[$i]['count'] = $count;
if($maxDiv < $count){
$maxDiv = $count;
$maxDivKey = $i;
}
}
}
}
echo '<h1>Max divisors:</h1>';
print_r($div[$maxDivKey]);
//print_r($div);
I may be misunderstanding this question a little. If you are looking for a single number with maximum number of dividers, it should be something like this.
<?php
$max_num=10000;
$start_num=1;
$max_divs=-1;
$max_number=-1;
$numbers=array();
$max_divs_arr=array();
for($i=$start_num;$i<=$max_num;$i++)
{
$divs=0;
$div_array=array();
for($j=$start_num;$j<=$i;$j++)
{
if($i%$j==0)
{
$divs++;
$div_array[]=$j;
}
}
if($divs==$max_divs)
$max_divs_arr[$i]=$div_array;
if($divs>$max_divs)
{
$max_divs_arr=array();
$max_divs=$divs;
$max_divs_arr[$i]=$div_array;
}
}
foreach($max_divs_arr as $number=>$divisors)
echo "\nNumber with most divisors is $number\nIt has $max_divs divisors\nThose divisors are:".implode(',',$divisors);
I have an array containing a range of numbers 1-100:
$range = range(1, 100);
I want to loop through and assign each a value of 1-24. So 1=1, 2=2, 24=24, but then also 25=1, 26=2, 27=3, etc...
How can I loop through $range and apply said values to each number?
Note: I would preferably like to use a forloop, but will take any valid answer.
The modulo operator (%) is the answer
$range = range(1, 100);
$rangeValues = array();
for ($i = 0; $i < count($range); $i++){
// using modulo 25 returns values from 0-24, but you want 1-25 so I use ($i % 24) +1 instead which gives 1-24
$rangeValues[$range[$i]] = ($i % 24) +1;
}
Try : php modulo operator (%).
//example loop
$range = range(1, 100);
$yourIndex = array();
for ($i = 0; $i < count($range); $i++){
//$yourIndex will reset to 1 after each 25 counts in $range
$yourIndex[$range[$i]] = ($i + 1) % 25;
}
$range = range(1, 100);
$offset = 1;
$limit = 24;
for($i = 0; $i < count($range); $i++)
{
$range[$i] = $offset+($i%$limit);
}
var_dump($range);
For getting your required solution, you can also use the below code -
$range = range(1, 100);
for($i=0; $i<100; $i++){
if($i < 24){
echo $range[$i].' = '.($i+1);echo "<br>";
}else if($i < 48){
echo $range[$i].' = '.($i-23);echo "<br>";
}else if($i < 72){
echo $range[$i].' = '.($i-47);echo "<br>";
}else if($i < 96){
echo $range[$i].' = '.($i-71);echo "<br>";
}else{
echo $range[$i].' = '.($i-95);echo "<br>";
}
}
Is it possible to add or concatenate something into a variable name in a PHP variable? For example:
for($g = 7; $g <= 10; $g++){
for($i = 0; $i <= 4; $i++){
$counter = $g - 7;
if($i != $counter){
continue;
} else {
$grade.[$g] = $grades[$i];
}
}
}
I want this to happen:
$grade7 = 0
$grade8 = 1
$grade9 = 2
$grade10 = 3
Concatenates the $g with grade and make this value a variable by adding a $ sign at the starting line...
The example given below:
for($g = 7; $g <= 10; $g++){
for($i = 0; $i <= 4; $i++){
$counter = $g - 7;
if($i != $counter){
continue;
} else {
${"grade".$g} = $grades[$i];
}
}
}
echo $grade7; // 0
echo $grade8; // 1
echo $grade9; // 2
echo $grade10;// 3
One solution would be to create the variables dynamically:
for ($g = 7; $g <= 10; $g++) {
for ($i = 0; $i <= 4; $i++) {
$counter = $g - 7;
if ($i == $counter) {
${'grade' . $g} = $grades[$i];
}
}
}
You should use ARRAY instead of your method. :)
Try to look at variable named like "$$".
EDIT: Maybe something like
var $array = array();
for($g=7; $g<=10; $g++)
{
for($i=0; $i<=4; $i++)
{
$counter = $g - 7;
if($i != $counter) continue;
else $array[$grade.[$g]] = $grades[$i];
}
}
$data = new Array();
for($g = 7; $g <= 10; $g++){
for($i = 0; $i <= 4; $i++){
$counter = $g - 7;
if($i != $counter){
continue;
} else {
$data[$grade.[$g]] = $grades[$i]);
}
}
}
PHP NOOB, i have been working on this code all day, but cant find any good way to fix it.
here is my code:
$priv = explode(',', '0,1,1');
$m = 'me';
for($i = 0; $i <= 2; $i++)
{
if($priv[$i] == '0')
{
$m.$i = 20;
}
else
{
$m.$i = 10;
}
}
Am trying to join $m.$i together and set it to 20 or 10, but i end up having me20 or me10 instead of me1 = 20 or me1 = 10 when i echo $m.$i which is legit, is there anyways to make this work?
$m.$i = 20;
This will assign $i = 20 and then concatenate it with $m and hence you will see me20.
What you need is $m . $i .= 20; instead. which will concatenate them altogether.
Fixed:
<?php
$priv = explode(',', '0,1,1');
$m = 'me';
for($i = 0; $i <= 2; $i++)
{
if($priv[$i] == '0')
{
echo $m . $i .= 20;
}
else
{
echo $m.$i .= 10;
}
}
?>
EDIT:
The above answer was a total misunderstanding, I realised you intended to create the variables:
for($i = 0; $i <= 2; $i++)
{
if($priv[$i] == '0')
{
${$m.$i} = 20;
echo $me0;
}
else
{
${$m.$i} = 10;
}
}
Assign it like so.
${$m.$i} = 20;
You are trying to dynamically create variables, so you have to do something like this:
$priv = explode(',', '0,1,1');
$m = 'me';
for($i = 0; $i <= 2; $i++)
{
if($priv[$i] == '0')
{
${$m.$i} = 20;
}
else
{
${$m.$i} = 10;
}
}
then try to priny $me0, $me1
Would you write this code any differently? I don't mind tertiary operator by the way so any ideas that include it are also welcome
if ($stopYear < $startYear) {
for ($i = $startYear; $i >= $stopYear; $i--) {
$yearMultiOptions[$i] = $i;
}
} else {
for ($i = $startYear; $i <= $stopYear; $i++) {
$yearMultiOptions[$i] = $i;
}
}
$min = min($startYear, $stopYear);
$max = max($startYear, $stopYear);
for ($i = $min; $i <= $max; $i++) {
$yearMultiOptions[$i] = $i;
}
I don't know php, so min and max might have different syntax, but you get the idea.
You could do something like this:
$step = ($stopYear < $startYear) ? -1 : 1;
for ($i = $startYear; $i != $stopYear; $i += step) {
$yearMultiOptions[$i] = $i;
}