admin管理员组文章数量:1026912
I am trying to generate a number that will be 5 digits including the post ID. For example: if post ID is 25, the number will be 00025.
So far my codes are below. It's working but is there any better way to lessen the code line? more dynamic?
$post_id = get_the_ID();
$postidlength = strlen($post_id);
if($postidlength = 1){
$zero="0000";
}
elseif($postidlength = 2){
$zero="000";
}
elseif($postidlength = 3){
$zero="00";
}
elseif($postidlength = 4){
$zero="0";
}
else{
echo "invalid id";
}
$result = $zero.$post_id;
echo $result;
I am trying to generate a number that will be 5 digits including the post ID. For example: if post ID is 25, the number will be 00025.
So far my codes are below. It's working but is there any better way to lessen the code line? more dynamic?
$post_id = get_the_ID();
$postidlength = strlen($post_id);
if($postidlength = 1){
$zero="0000";
}
elseif($postidlength = 2){
$zero="000";
}
elseif($postidlength = 3){
$zero="00";
}
elseif($postidlength = 4){
$zero="0";
}
else{
echo "invalid id";
}
$result = $zero.$post_id;
echo $result;
Share
Improve this question
edited Mar 27, 2019 at 22:05
Krzysiek Dróżdż
25.6k9 gold badges53 silver badges74 bronze badges
asked Mar 27, 2019 at 20:47
NoobieNoobie
1091 gold badge2 silver badges10 bronze badges
1 Answer
Reset to default 2str_pad
is the function you're looking for.
echo str_pad( get_the_ID(), 5, "0", STR_PAD_LEFT);
This should do the trick.
I am trying to generate a number that will be 5 digits including the post ID. For example: if post ID is 25, the number will be 00025.
So far my codes are below. It's working but is there any better way to lessen the code line? more dynamic?
$post_id = get_the_ID();
$postidlength = strlen($post_id);
if($postidlength = 1){
$zero="0000";
}
elseif($postidlength = 2){
$zero="000";
}
elseif($postidlength = 3){
$zero="00";
}
elseif($postidlength = 4){
$zero="0";
}
else{
echo "invalid id";
}
$result = $zero.$post_id;
echo $result;
I am trying to generate a number that will be 5 digits including the post ID. For example: if post ID is 25, the number will be 00025.
So far my codes are below. It's working but is there any better way to lessen the code line? more dynamic?
$post_id = get_the_ID();
$postidlength = strlen($post_id);
if($postidlength = 1){
$zero="0000";
}
elseif($postidlength = 2){
$zero="000";
}
elseif($postidlength = 3){
$zero="00";
}
elseif($postidlength = 4){
$zero="0";
}
else{
echo "invalid id";
}
$result = $zero.$post_id;
echo $result;
Share
Improve this question
edited Mar 27, 2019 at 22:05
Krzysiek Dróżdż
25.6k9 gold badges53 silver badges74 bronze badges
asked Mar 27, 2019 at 20:47
NoobieNoobie
1091 gold badge2 silver badges10 bronze badges
1 Answer
Reset to default 2str_pad
is the function you're looking for.
echo str_pad( get_the_ID(), 5, "0", STR_PAD_LEFT);
This should do the trick.
本文标签: phpGenerating a number based on post ID
版权声明:本文标题:php - Generating a number based on post ID 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745652528a2161403.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论