Skip to content
Advertisement

PHP: Adapt number ID to words

What do I need to do to convert the number only if all the number matches?

At the moment, it’s converting to each number. (I’ve tried to do a lot and I didn’t succeed) I would like to know if have any way of adapting to only show the word if the number “is complete”, otherwise show the word “Doesn’t contain a name record”.

Example: When I try to use the number 1987, the output its: Name1 Name9 Name8 Name7, and I want to be only one word: Name1987.

JavaScript

Advertisement

Answer

only show the word if the number “is complete”, otherwise show the word “Doesn’t contain a name record”.
Example: … number 1987 I want (output) to be only one word: Name1987.

If this is all you need – piece of cake:

JavaScript

array_merge() joins arrays, range() is kind of array, in_array() checks if a value exists in an array


more flexible – for cases you omitted in your question

JavaScript

array_key_exists()

Arrays are fast and nice in use. Built-in functions are optimised and that makes them much better than step-by-step manipulations. Debugging a few lines of code is easy, debugging plenty lines of particular manipulations is a nightmare. For most cases in php the only concern is to find the name of built-in function which do it all at once.

I didn’t want to limit the numbers (…) want to add more than 200 ids

I didn’t limit anything, and it is not that much, even as associative array it wouldn’t be big array


more complex – it’s time to organise the code structure

JavaScript

In the name() function, sequence is important for the sub-ranges.

ternary operator a.k.a. Elvis
filter_var() if you like it

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement