I’m generating auto incremental code through a PHP script. The sequence goes like this, L001, L002….L039, L040. Below is the PHP code I have written. The comments show the output of each statement.
<?php
require_once("db_handler.php");
$conn = iniCon();
$db = selectDB($conn);
$query = "SELECT LID FROM locations ORDER BY LID DESC LIMIT 1";
$result = mysql_query($query, $conn);
$row = mysql_fetch_array($result);
$last_id = $row['LID']; //L040
$id_letter = substr($last_id, 0, 1); //L
$id_num = substr($last_id, 1) + 1; //040 + 1
$new_id = $id_letter . $id_num;
mysql_close($conn);
?>
I’m displaying the $new_id in a HTML textbox. though everything works fine up to this point, the new generated code shows as L41, cutting off the leading zero.
How can I stop this from happening? I need to have that zero in the front.
Thank you.
Use either
str_padorsprintffunction:or