Link to home
Start Free TrialLog in
Avatar of breeze351
breeze351

asked on

Strip leading 0 from a var

What's the easiest way to strip leading 0 from a variable.  I need to keep the 0 for the data, this is just for display.
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
PHP ltrim() is probably a good answer, but be careful about defining an integer variable with a leading zero.  That denotes octal notation.
http://php.net/manual/en/language.types.integer.php

Another way is to cast the value to in integer, then echo it.

<?php // demo/temp_breeze351.php
/**
 * https://www.experts-exchange.com/questions/28987009/Strip-leading-0-from-a-var.html
 *
 * http://php.net/manual/en/language.types.integer.php
 */
error_reporting(E_ALL);


// USING DECIMAL VALUES
$str = '01234';
echo PHP_EOL . $str;
$int = $str * 1;
echo PHP_EOL . $int;


// USING OCTAL VALUES (OOPS!)
$str = 01234;
echo PHP_EOL . $str;
$int = $str * 1;
echo PHP_EOL . $int;

Open in new window

Avatar of breeze351
breeze351

ASKER

thanks