PHP - Increment a value - php

I'd like to increment a value in a 3-digits format.
For example:
$start_value = 000;
while () {
do something;
$start_value++;
}
In this way I have this result:
$start_value = 000;
$start_value = 1;
$start_value = 2;
$start_value = 3;
and so on
instead of '001', '002', '003'
How I can accomplish this result?

Using sprintf you can accomplish this with:
echo sprintf("%03d", $start_value++) . "<br>";
Hopefully that is what you were after.
Implementing it with your code:
$start_value = 000;
while () {
do something;
$start_value = sprintf("%03d", $start_value++);
}

You are making a big mistake here. 000 in code is an octal number. Any literal number starting with a 0 in many programming languages is considered an octal literal. If you want to store 000, you need a string, not a number.
$start_value = "000";
while((int) $start_value /*--apply condition --*/) {
do something;
$start_value = str_pad((int) $start_value+1, 3 ,"0",STR_PAD_LEFT);
}

It goes like this. In PHP there is no concept of datatypes everything is determined in runtime based on where and how it is used.
<?php
$start_value = 000;
while ($start_value < 10) {
//your logic goes here
$start_value++;
printf("[%03s]\n",$start_value);
}
?>
Output : [001] [002] [003] [004] [005] [006] [007] [008] [009] [010]
So you can do all the calculation. Where ever you want to print the value, you can use printf with format specifiers.!! Hope it helps.

Related

php loop with letters without using an array of letters

I have an php code that writes an excel file with a variable number of columns. Each 4 rows, on the 5th I want to put the total of the four rows above, per column.
My issue is on how to write the formula in terms of columns.
My code is this one:
$col_index=20;
$i=20;
for($anno = $annoMin; $anno<=$annoMax; $anno++){
for($mese = 1; $mese <= 12; $mese++){
$string = "=$i$v+$i$q-$i$z-$i$t";
$ews->setCellValueByColumnAndRow($col_index,$k,$string);
$col_index=$col_index+1;
$i=$i+1;
}
}
In $string I need to put the column letter instead of $i. $v,$q,$z and $t are the reference to the rows and are ok. In other words $string should be evaluated to:
$string = "=U5+U3-U2-U4";
during the first for loop,
$string = "=V5+V3-V2-V4";
in the second and so on.
I know I can build an array of columns letter and use that $i reference to get my goal but I'm sure there is a better approach. I am using php 5.5 btw
The other option is to write the formula with the R1C1 notation but I'm pretty sure I cannot have the standard and the R1C1 notation in an excel sheet at the same time
You can increment a string variable. I think this is the most efficient way to do what you need (if I understood it right).
$ch = "a";
++$ch;
echo $ch; //prints "b"
Knowing this you can build a loop to update the value as you need.
This works very good for Excel because:
$ch = "z";
++$ch;
echo $ch; //prints aa
I'm just concentrating on the column letter as that seems to be what you're asking. Use the ASCII code of the letter and increment it, then convert it to the character:
$col_index = 20;
$i = ord('U'); // 85
for($anno = $annoMin; $anno<=$annoMax; $anno++){
for($mese = 1; $mese <= 12; $mese++){
$c = chr($i);
$string = "=$c$v+$c$q-$c$z-$c$t"; // $i will be U then V etc...
$ews->setCellValueByColumnAndRow($col_index,$k,$string);
$col_index = $col_index+1;
$i++;
}
}
If you need to keep $i starting at 20 and incrementing then just add 65:
$col_index = 20;
$i = 20;
for($anno = $annoMin; $anno<=$annoMax; $anno++){
for($mese = 1; $mese <= 12; $mese++){
$c = chr($i + 65);
$string = "=$c$v+$c$q-$c$z-$c$t"; // $i will be U then V etc...
$ews->setCellValueByColumnAndRow($col_index,$k,$string);
$col_index = $col_index+1;
$i++;
}
}

Optimal way of cycling through 1000's of values

I need to find the value of x where the variance of two results (which take x into account) is the closest to 0. The problem is, the only way to do this is to cycle through all possible values of x. The equation uses currency, so I have to check in increments of 1 cent.
This might make it easier:
$previous_var = null;
$high_amount = 50;
for ($i = 0.01; $i <= $high_amount; $i += 0.01) {
$val1 = find_out_1($i);
$val2 = find_out_2();
$var = variance($val1, $val2);
if ($previous_var == null) {
$previous_var = $var;
}
// If this variance is larger, it means the previous one was the closest to
// 0 as the variance has now started increasing
if ($var > $previous_var) {
$l_s -= 0.01;
break;
}
}
$optimal_monetary_value = $i;
I feel like there is a mathematical formula that would make the "cycling through every cent" more optimal? It works fine for small values, but if you start using 1000's as the $high_amount it takes quite a few seconds to calculate.
Based on the comment in your code, it sounds like you want something similar to bisection search, but a little bit different:
function calculate_variance($i) {
$val1 = find_out_1($i);
$val2 = find_out_2();
return variance($val1, $val2);
}
function search($lo, $loVar, $hi, $hiVar) {
// find the midpoint between the hi and lo values
$mid = round($lo + ($hi - $lo) / 2, 2);
if ($mid == $hi || $mid == $lo) {
// we have converged, so pick the better value and be done
return ($hiVar > $loVar) ? $lo : $hi;
}
$midVar = calculate_variance($mid);
if ($midVar >= $loVar) {
// the optimal point must be in the lower interval
return search($lo, $loVar, $mid, $midVar);
} elseif ($midVar >= $hiVar) {
// the optimal point must be in the higher interval
return search($mid, $midVar, $hi, $hiVar);
} else {
// we don't know where the optimal point is for sure, so check
// the lower interval first
$loBest = search($lo, $loVar, $mid, $midVar);
if ($loBest == $mid) {
// we can't be sure this is the best answer, so check the hi
// interval to be sure
return search($mid, $midVar, $hi, $hiVar);
} else {
// we know this is the best answer
return $loBest;
}
}
}
$optimal_monetary_value = search(0.01, calculate_variance(0.01), 50.0, calculate_variance(50.0));
This assumes that the variance is monotonically increasing when moving away from the optimal point. In other words, if the optimal value is O, then for all X < Y < O, calculate_variance(X) >= calculate_variance(Y) >= calculate_variance(O) (and the same with all > and < flipped). The comment in your code and the way have you have it written make it seem like this is true. If this isn't true, then you can't really do much better than what you have.
Be aware that this is not as good as bisection search. There are some pathological inputs that will make it take linear time instead of logarithmic time (e.g., if the variance is the same for all values). If you can improve the requirement that calculate_variance(X) >= calculate_variance(Y) >= calculate_variance(O) to be calculate_variance(X) > calculate_variance(Y) > calculate_variance(O), you can improve this to be logarithmic in all cases by checking to see how the variance for $mid compares the the variance for $mid + 0.01 and using that to decide which interval to check.
Also, you may want to be careful about doing math with currency. You probably either want to use integers (i.e., do all math in cents instead of dollars) or use exact precision numbers.
If you known nothing at all about the behavior of the objective function, there is no other way than trying all possible values.
On the opposite if you have a guarantee that the minimum is unique, the Golden section method will converge very quickly. This is a variant of the Fibonacci search, which is known to be optimal (require the minimum number of function evaluations).
Your function may have different properties which call for other algorithms.
Why not implementing binary search ?
<?php
$high_amount = 50;
// computed val2 is placed outside the loop
// no need te recalculate it each time
$val2 = find_out_2();
$previous_var = variance(find_out_1(0.01), $val2);
$start = 0;
$end = $high_amount * 100;
$closest_variance = NULL;
while ($start <= $end) {
$section = intval(($start + $end)/2);
$cursor = $section / 100;
$val1 = find_out_1($cursor);
$variance = variance($val1, $val2);
if ($variance <= $previous_var) {
$start = $section;
}
else {
$closest_variance = $cursor;
$end = $section;
}
}
if (!is_null($closest_variance)) {
$closest_variance -= 0.01;
}

Numbers to letters with logical sequence

I have this array which links numbers to letters at the moment like this:
1-26 = A-Z
But there is more, 27=AA and 28=AB etc...
so basically when I do this:
var_dump($array[2]); //shows B
var_dump($array[29]); //shows AC
Now this array I made myself but it's becoming way too long. Is there a way to actually get this going on till lets say 32? I know there is chr but I dont think I can use this.
Is there an easier way to actually get this without using this way too long of an array?
It's slower calculating it this way, but you can take advantage of the fact that PHP lets you increment letters in the same way as numbers, Perl style:
function excelColumnRange($number) {
$character = 'A';
while ($number > 1) {
++$character;
--$number;
}
return $character;
}
var_dump(excelColumnRange(2));
var_dump(excelColumnRange(29));
here is the code which you are looking for :
<?php
$start = "A";
$max = 50;
$result = array();
for($i=1; $i<=$max; $i++) {
$result[$i] = $start++;
}
print_r($result);
?>
Ref: http://www.xpertdeveloper.com/2011/01/php-strings-unusual-behaviour/
This should work for you:
Even without any loops. First I calculate how many times the alphabet (26) goes into the number. With this I define how many times it has to str_repleat() A. Then I simply subtract this number and calculate the number in the alphabet with the number which is left.
<?php
function numberToLetter($number) {
$fullSets = (($num = floor(($number-1) / 26)) < 0 ? 0 : $num);
return str_repeat("A", $fullSets) . (($v = ($number-$fullSets*26)) > 0 ? chr($v+64) : "");
}
echo numberToLetter(53);
?>
output:
AAA

Comparison Opeartor seemingly not working

I am trying to compare two values but when I do it does not appear to work. I know what the values are so it should be reporting true. Even worse, if I take either one of the variables out and put the number in it works.
$data = simplexml_load_file('xml/heroes/hero.xml')
or die("Error: Cannot create object");
$hme = $data->hes->he->maxen;
$hce = $data->hes->he->curen;
$hac = $data->hes->he->lastac;
echo $hce . ' should not be greater than ' . $hme;
if($hce > $hme){
echo 'should be working';
}
Outputs:
773 should not be greater than 20
I think your variable are like this
$hce = "773";
$hme = "20";
Before comparing them do intval
if(intval($hme)>intval($hce))
Cast your strings to integers:
$hme = (int)$data->hes->he->maxen;
$hce = (int)$data->hes->he->curen;
$hac = (int)$data->hes->he->lastac;
I think you took them as strings.I think you need to convert them to integer.
Simple function to do that:
int atoi(char *s)
{
int val = 0;
while (*s)
{
val *= 10;
val += (*s) - '0';
s++;
}
return val;
}

How can I increase 0000 by 1 and keep formatting?

$i=0000;
while($i<=1231)
{
print "$i";
$i++;
}
I want it to display 0001,
0002,
0003,
0004,
but instead it prints:
0,
1,
2
Does anyone know why this isn't working?
Thank you in advance.
Try using printf("%04s",$i);
print str_pad($i,4,'0',STR_PAD_LEFT);
is only one way... you could also use sprintf.
PHP treats the string 0000 as number 0 when considering incrementation. The ++ incrementation operator can actually work on normal strings too, but due to PHP's type handling, it doesn't work as you expect in this case. However, if you had started with string like a0000, then incrementing it would result in a0001. For example:
<?php
$var = 'a0000';
for ($i = 0; $i < 100; $i++)
{
$var++;
}
echo $var; // Outputs a0100
?>
Although, since this method of using the incrementation operator is a bit unorthodox, I would recommend using printf("%04d", $var) (or sprtinf()) in this case instead, when outputting. For example:
<?php
$var = 0;
for ($i = 0; $i < 100; $i++)
{
printf('%04d ', $var);
$var++;
}
?>
sprintf(), str_pad()
try this $x = str_pad($z + 1, 5, 0, STR_PAD_LEFT);

Categories