Search code examples
phpvariablesprefix

PHP how to add Prefix to variable


I'm looking to add a prefix to a variable I created. The variable is a number from 1 to 999. Lets call the variable $withoutPrefix. I want all variables to become prefix-1 to prefix-999. I found str_pad does the trick but you have to submit a desired number of total characters making the solution unsuitable for variables with multiple different number of digits.

Example of my current non ideal solution:

<?php $withPrefix = str_pad($withoutPrefix,7, 'post-' ,STR_PAD_LEFT); ?>

it gives me prefix-10 and prefix-p7. Not ideal.

ps. While typing my question: The problem would be solved if my variable started at 100 and ranged to 999 (something I can actually do). But i still don't like the rigid non flexible solution.


Solution

  • $withPrefix = "prefix-" . $withoutPrefix;