[PHP] Repeat the same character string with str_repeat

Aug 29, 2020 PHP Laravel


・Host OS: Windows10 Home ・Guest OS: WSL Ubuntu20.04 ・PHP7.4 ・Laravel FrameWork 7.17.2

Function summary

If “str_repeat(string, int)**” is specified, a string in which int strings are concatenated is returned.

The usage example is as follows

echo str_repeat('Nyan', 3);
//Execution result
Meow Meow Meow Meow

When this function can be used is useful when creating a property with a large number of characters in a boundary value test.

For example, when performing a validation test on a column with a maximum of 200 characters, it is difficult to grasp the number of characters manually, and it is time-consuming and difficult to enter each time. Writing test code does not change typing in a long number of characters.

This function solves such a problem Since it is possible to generate a character string of any length, it is easy to perform a boundary value test for input values.

Compared to typing a few hundred characters directly, the readability of the code is higher, so there is also the advantage that the test is less likely to fail due to an excess or deficiency of the number of characters.

I would like to actively use this function when writing test code in PHP in the future.