# Php - adding zeros

**URL:** <https://forum.kirupa.com/t/php-adding-zeros/250025>\
**Category:** Uncategorized\
**Created:** [January 24, 2008, 6:00pm UTC](https://forum.kirupa.com/t/php-adding-zeros/250025 "2008-01-24T18:00:54Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![hroth](https://avatars.discourse-cdn.com/v4/letter/h/8e8cbc/32.png) [@hroth](https://forum.kirupa.com/u/hroth)\
**Post date:** [January 24, 2008, 6:00pm UTC](https://forum.kirupa.com/t/php-adding-zeros/250025/1 "2008-01-24T18:00:54Z")

</div>

Hey there,

I need to add the appropriate number of zeros to a number depending on it’s length.

1 = 001  
10 = 010  
100 = 100

here are a couple failed attempts…

```php

for ($i = 1; $i <= $maxtotal; $i++) { 
if (i>=100) {
${$i.'nam'} = stripslashes($_POST[$i.'_prod_name']);
}elseif (i<100 && i>=10) {
${$i.'nam'} = stripslashes($_POST['0'.$i.'_prod_name']);
} elseif (i<10) {
${$i.'nam'} = stripslashes($_POST['00'.$i.'_prod_name']);
}
}

```

or

```php

$num_str = strlen(i);
if ($num_str <= 1) {
$setdigit = "00";
} else if ($num_str == 2) {
$setdigit = "0";
} else {
$setdigit = "";
}
${$i.'nam'} = stripslashes($_POST[$setdigit.$i.'_prod_name']);

```
