Attaching two zero at the end of PHP var

im having this code


$ab='abc';
$zero=0;

for($zz=1; $zz<=120; $zz++)
{
echo "<br>";
if ($zz<10)
{
$a=$ab . "0" . $zero . $zz;
// $a=$ab . "0" . "0" . $zz;
// $a=$ab . "00" . $zz;
}

if ($zz<100)
$a=$ab . "0" . $zz;

else
$a=$ab . "" . $zz;

echo $ab;
}

i want to output this sequence

abc001
abc002
........
abc010
abc011
.....
abc100

but its being displayed as

abc01
abc02
...
abc010
abc011
....
abc100

have tried these for generating double 00
$a=$ab . “0” . $zero . $zz;
// $a=$ab . “0” . “0” . $zz;
// $a=$ab . “00” . $zz;
but not working …

You can use the [fphp]sprintf[/fphp] function to format strings with leading zeros. Here’s how you’d use it to get the result you want:


$ab='abc';

for($zz=1; $zz<=120; $zz++)
{
    echo $ab . sprintf('%03s', $zz) . '<br>';
}

hi bro…
ya that works but im not getting how to store that in variable …

used this to store
$ab=$ab . sprintf(‘%03s’, $zz);

again get converted to abc01

Can you post the whole section of code… what is it you want to do with the value once it’s generated?

$ab is also generated by merging diff strings in the intial part of the code…

u can refer the code in first post its similar…but at the end instead of using $ab in an echo statement im using that for some operations in html form , using as input to mysql db…
so i want the final string to be in a variable instead of echo…

Thanks

This code works fine for me:


$ab='abc';
$output = array();

for($zz=1; $zz<=120; $zz++)
{
    $output[] = $ab . sprintf('%03s', $zz);
}

// Just to test the output
echo '<pre>' . print_r($output, true) . '</pre>';

You could then take $output which is an array of all the generated strings, and use to insert into a DB or something.

ya this works fine…
but is it possible to get value [$ab] one no. at a time in normal php var instead of array?
bcause ive already written, tested the below part of the code, the db part and everything and some or most of it is in same for loop which uses this var…
so is it possible to get this in normal var as in 1st post?

Just change what happens in the loop:


$ab='abc'; 

for($zz=1; $zz<=120; $zz++) 
{ 
    $code = $ab . sprintf('%03s', $zz);
    // Do something with $code here.. insert to DB or whatever
}

already tried this , check my prev post

used this to store

$ab=$ab . sprintf('%03s', $zz); 

again get converted to abc01

and modified if condition…

$ab='abc';
$zero=0;

for($zz=1; $zz<=120; $zz++)
{
echo "<br>";
if ($zz<10)
{
$a=$ab . "0" . $zero . $zz;  
// $a=$ab . "0" . "0" . $zz;  
// $a=$ab . "00" . $zz;
}

if ($zz>10 && $zz<100) // befor it was if($zz<100)
$a=$ab . "0" . $zz;

else
$a=$ab . "" . $zz;

echo $ab;
}

and nw output is…
abc01
abc02

abc10
abc11

abc100

but should be “abc001”

Why do you still have this code?


$ab='abc';
$zero=0;

for($zz=1; $zz<=120; $zz++)
{
echo "<br>";
if ($zz<10)
{
$a=$ab . "0" . $zero . $zz;
// $a=$ab . "0" . "0" . $zz;
// $a=$ab . "00" . $zz;
}

if ($zz>10 && $zz<100)
$a=$ab . "0" . $zz;

else
$a=$ab . "" . $zz;

echo $ab;
}

All those IF statements are unnecessary… the way I’ve used the sprintf function will take any number and add leading zeros if needed to return a 3 digit string… so 1 will be 001, 15 will be 015, and 136 will be 136.

If you do this:


$ab=$ab . sprintf('%03s', $zz);

you’re overwriting $ab, so the next time through the loop the value of $ab will be wrong.

Can you please post the whole block of code? Working with fragments is confusing because we’re not looking at the whole picture and it’s harder to point out what’s going wrong.

oh so this works similar to output funct in c language…
working fine…thanks…