How can I increase 0000 by 1 and keep formatting? - php

$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);

Related

Large number multiplication and division

I am trying the below mentioned code(PHP) to find probability. The calculation includes combination calculation of large numbers, using BCmaths function but not getting results. Please suggest, how this can be done.
function combin($n, $r)
{
$C = 1;
for ($i=0; $i < $n-$r; $i++)
{
$C = bcdiv(bcmul($C, $n-$i), $i+1);
}
return $C;
}
$dv = (combin(68, 17))*(combin((7866-68),(177-17)))/combin(7866, 177);
echo $dv;
?>```
Once you start using the bc* functions, you should continue to use them throughout the code. You however, are taking the results of these functions and then using the standard PHP operators on them. You should be able to change your calling code to:
$dv = bcdiv(combin(7866, 177), bcmul(combin(68, 17), (combin(7866 - 68, 177 - 17))));

PHP string of zeros of random length?

How do I insert a random string of zeros (length between x and x+y) before each of the four digits for the code snipet below?
an example would be:
$quotes=array("000350.00155.062.00000044");
<?php
$quotes=array("$random+00350.$random2+0155.$random3+062.$random4+044");
Something like this would work, I don't completely understand your array declaration however and may have missed the point.
$quotes = array("00350","0155","062","044");
foreach($quotes as $i => $v) {
$a = rand($x, $x + $y);
$zeros = "";
for($j = 0; $j < $a; $j++) $zeros .= "0";
$quotes[$i] = $zeros . $v;
}
I would run a pseudo random over an uniform distribution [0-1]. Then, ceil of the number would be my number of zeros.
Done! :)
if you only have the $quotes variable I would you suggest you to do this:
-> explode $quotes (using the "explode" function and puting the "." as the delimiter)
-> get a random integer from x to x+y using rand(x, x+y)
-> concatenate the parts using a loop

0's Not being Stripped from the front of a numeric value

I am working on a project for a friend's website which is suppose to generate completely random phone numbers to be displayed on a "fake" review board. I figured the best way to do with would be for me to to generate out each section separably. So 3-3-4, but no matter what I do, every time there is a 0 in front the code cuts it off. Here's an example of what I mean:
http://www.shiningashes.net/Test.php
yet this is what I have for the code:
<?php
for ($i = 0000; $i <= 9999; $i++) {
echo $i;
echo "<br>";
}
?>
How do I get the 0's to stop being cropped out so the 0's display? 0001, 0021, 0123, etc?
You can use str_pad
for ($i = 0; $i <= 9999; $i++) {
echo str_pad($i, 4, '0', STR_PAD_LEFT);
echo "<br>";
}
You can use printf to format your output:
<?php
for ($i = 0; $i <= 9999; $i++) {
printf("%04d<br>\n",$i);
}
?>
You need to make your variable a string if you want to keep the zeros. That would mean using quotes, and never using numeric operators on it. But since you depend on using ++ on it, I suggest the following hack:
<?php
for ($i = 10000; $i <= 19999; $i++) {
$str=substr ( $i , 0 , 4 );
echo $i;
echo $str;
echo "<br>";
}
?>
You will need to convert your integer to a string when printing it.
<?php
for ($i = 0; $i <= 9999; $i++) {
printf("%04d<br />", $i);
}
?>
Check the documentation for printf/sprintf for more information.
Kind regards,
Stefan

PHP loop thru hex numbers from a form POST always gives the first number as 0. why?

i have a form with two text fields, "from" and "to."
when i enter values such as "100000" and "10000F" and do a for loop, it always comes out like:
0 100001 100002 100003 100004 100005 100006 100007 100008 100009 10000a 10000b 10000c 10000d 10000e 10000f
if i plug in the range for the loop manually, i get:
100000 100001 100002 100003 100004 100005 100006 100007 100008 100009 10000a 10000b 10000c 10000d 10000e 10000f
using:
for ($i = '0x'.$_POST['from']; $i <= '0x'.$_POST['to']; $i++) { print dechex($i)."\n"; }
versus:
for ($i = 0x100000; $i <= 0x10000F; $i++) { print dechex($i)."\n"; }
if anyone knows what i am doing wrong here, please let me know.
i have also tried tried adding the "0x" to the numbers via the form with the same results.
thanks!
While $i++ is converting $i to a number, this doesn't run until after the first loop. Apparently dechex doesn't try to interpret strings as numbers and just barfs.
To force conversion, prefix the string expression with a +:
for ($i = +('0x'.$_POST['from']); $i <= '0x'.$_POST['to']; $i++) {
print dechex($i)."\n";
}
Tested and working on PHP 5.2.6.
(Note that casting does not work! (int)('0x100000') returns 0.)
You should use intval() to convert a hex string to a number. dechex() will take the number and turn it back into a hex string:
$from = intval('100001', 16);
$to = intval('10000f', 16);
for ($i = $from; $i <= $to; $i++) {
print dechex($i) . "\n";
}
'0x'.$_POST['from']; is a string $i = 0x100000 is a number
Your first iteration will cast the string value to a number using standard PHP casting (as an integer value to the first non-numeric character, which is the 'x' giving a start value of 0)
You can also do
for ($i = intval('0x'.$_POST['from'], 0); $i <= intval('0x'.$_POST['to'], 0); $i++) { print dechex($i)."\n"; }
You need to convert your hex string to an actual integer first. Something like this function does.
Otherwise, PHP will handle it as a string.

number increment with customized style

I've tried to do a number(decimal) increment which looks like 001 002 003...123,124 in a loop and couldn't find a simple solution.What I've thought now is to check out whether the number is long enough ,if not prefix it some "0".But it seems not good.Any better ideas?
Thanks.
$x = 6
$y = sprintf("%03d",$x);
http://php.net/manual/en/function.sprintf.php
for($i=1;$i<1000;$i++){
$number = sprintf("%03d",$i);
echo "$number <br />";
}
Two options come immediately to mind. First, try str_pad(). It does exactly what you seem to describe.
Second, you could use sprintf() as another has suggested.
If you are not sure how long the various numbers will turn out to be (e.g., they are determined dynamically and no way of knowing what they will be until afterwards), you can use the following code:
<?php
$numbers = array();
for ($i = 0; $i < 2000; $i++)
{
$numbers[] = $i;
}
array_walk($numbers, function(&$item, $key, $len) { $item = sprintf('%0'.$len.'d', $item); }, strlen(max($numbers)));
print_r($numbers);
?>

Categories