29

Want to remove all 0 placed at the beginning of some variable.

Some options:

  1. if $var = 0002, we should strip first 000 ($var = 2)
  2. if var = 0203410 we should remove first 0 ($var = 203410)
  3. if var = 20000 - do nothing ($var = 20000)

What is the solution?

drAlberT
  • 22,059
  • 5
  • 34
  • 40
James
  • 42,081
  • 53
  • 136
  • 161

7 Answers7

89

Just cast it to integer

$var = (int)$var;
drAlberT
  • 22,059
  • 5
  • 34
  • 40
  • 2
    @Lekensteyn: Only if it is meant to be a statement instead of an expression. – Gumbo Aug 25 '10 at 07:39
  • it was not the entire line :) ehehhe – drAlberT Aug 25 '10 at 07:40
  • is there similar function in javascript? – James Aug 25 '10 at 07:41
  • of course, casting is a feature present in almost every language. In JS you can use parseInt() – drAlberT Aug 25 '10 at 07:42
  • (int)$var is an statement, but assigning it to $var looks like a expression to me. Anyway, this is a oneliner, you don't need to add something else after it. – Lekensteyn Aug 25 '10 at 07:43
  • 2
    @Workinghard, `parseInt(var, 10)` or `1 * var` (my favorite :)) – Lekensteyn Aug 25 '10 at 07:44
  • @Lekensteyn: `(int)$var` is an expression and an assignment is an expression too. – Gumbo Aug 26 '10 at 07:55
  • Just remember if you do it this way (int)$var, and the number is above max integer, 2147483647 - then your number will change and not only remove the zero – WoodyDRN Dec 02 '15 at 10:31
  • When I tried this solution in php version 5.3.3 and not working as expected. I tried with $var = 0131;$var = (int)$var);echo $var; it results 89. – Viswa Aug 26 '16 at 13:32
  • @viswa Google for "89 to octal" – drAlberT Aug 28 '16 at 13:25
  • Of course it is an I interpreter behaviour to treat a number starting g with 0 as an octal representation of an integer .. I think the "printed number" in the question was in fact it's striking g representation .. maybe it cab be investigated deeper – drAlberT Aug 28 '16 at 13:31
  • @drAlberT So what is the point my value got changed and if I try to get a value from an array using this value as key then, I will get an in-appropriate result or empty result. – Viswa Aug 29 '16 at 07:53
  • I really think the "pseudo code" in the question was referring to *strings* .. an octal number IS already an int .. the representation is not influent on the value. Your test should be on `$var = '0131';`, a string. – drAlberT Aug 29 '16 at 09:00
  • Watch your step. That only works for integers! If the value is a decimal number, then the decimal is removed. Therefore, I think ltrim is the better idea to remove leading zeros (as the title of the question is) – Sarah Trees Nov 22 '19 at 09:53
  • true, but the question was for integers ;) – drAlberT Nov 22 '19 at 10:30
35

Maybe ltrim?

$var = ltrim($var, '0');
robertbasic
  • 4,325
  • 1
  • 27
  • 25
15
$var = ltrim($var, '0');

This only works on strings, numbers starting with a 0 will be interpreted as octal numbers, multiple zero's are ignored.

Lekensteyn
  • 64,486
  • 22
  • 159
  • 192
5

Just use + inside variables:

echo +$var;
poostchi
  • 422
  • 1
  • 5
  • 14
5
$var = strval(intval($var));

or if you don't care about it remaining a string, just convert to int and leave it at that.

Amber
  • 507,862
  • 82
  • 626
  • 550
2

Carefull on the casting type;

var_dump([
    '0014010011071152',
    '0014010011071152'*1,
    (int)'0014010011071152',
    intval('0014010011071152')
]);

Prints:

array(4) {
    [0]=> string(16) "0014010011071152"
    [1]=> float(14010011071152)
    [2]=> int(2147483647)
    [3]=> int(2147483647)
}
  • for [2]=> int(2147483647) [3]=> int(2147483647) its integer overflow , so results will not be expected if value is more than max integer limit i.e. 2,147,483,647 (or hexadecimal 7FFFFFFF16) – Asad kamran Jun 07 '23 at 11:20
2

Multiply it by 1

$var = "0000000000010";
print $var * 1;  

//prints 10
Jhourlad Estrella
  • 3,545
  • 4
  • 37
  • 66