|
| 1 | +PHP has a huge standard library with the most extravagant and highly specific string and array functions available. |
| 2 | +`levenshtein` & `array_change_key_case` anyone?? |
| 3 | + |
| 4 | +On the flip side, historically it has missed some very common string operations. There's always been a way to get the same results, but they are cumbersome and error prone. |
| 5 | + |
| 6 | +For example, if you want to check if a string contains a string you might use `strpos`. Eg: |
| 7 | + |
| 8 | +```php |
| 9 | +if (strpos('colourful', 'colour')) { |
| 10 | +} |
| 11 | +``` |
| 12 | +Well, this check will fail because `strpos` will return `0` which will be interpreted as false. You will need to adapt that check to account for 0 with `strpos('colourful', 'colour') !== false` where the strict comparison is important. |
| 13 | + |
| 14 | +Thankfully, PHP 8 has done away with all that nonsense. Please welcome the following functions in to the world of PHP: |
| 15 | + |
| 16 | +* `str_contains` |
| 17 | +* `str_starts_with` |
| 18 | +* `str_ends_with` |
| 19 | + |
| 20 | +---------------------------------------------------------------------- |
| 21 | + |
| 22 | +Create a program the accepts two arguments, the first is a word, the second is a sentence (random words). |
| 23 | + |
| 24 | +You must print a table which contains the results of each of the above functions with the provided word and sentence. |
| 25 | + |
| 26 | +* First, does the sentence contain the word? |
| 27 | +* Second, does the sentence start with the word? |
| 28 | +* Lastly, does the sentence end with the word? |
| 29 | + |
| 30 | +You will use the composer package `symfony/console` to print a table using the `Table` helper. |
| 31 | + |
| 32 | +The table headers should be `Function` & `Result` |
| 33 | + |
| 34 | +There should be three rows, one for each function. The function column should contain the function name, eg `str_contains`. |
| 35 | +The result column should be `true` or `false` based on the result of the corresponding function call. |
| 36 | + |
| 37 | +### The advantages of the new string functions |
| 38 | + |
| 39 | +* Simpler and more concise. |
| 40 | +* Saner return types. |
| 41 | +* It is harder to get their usage wrong, for example checking for 0 vs false with `strpos` |
| 42 | +* The functions are faster, being implemented in C. |
| 43 | +* The operations require less function calls, for example no usages of `strlen` are required. |
| 44 | + |
| 45 | +---------------------------------------------------------------------- |
| 46 | +## HINTS |
| 47 | + |
| 48 | +Point your browser to [https://getcomposer.org/doc/00-intro.md](https://getcomposer.org/doc/00-intro.md) which will walk you through **Installing Composer** if you haven't already! |
| 49 | + |
| 50 | +Use `composer init` to create your `composer.json` file with interactive search. |
| 51 | + |
| 52 | +For more details look at the docs for... |
| 53 | + |
| 54 | +**Composer** - [https://getcomposer.org/doc/01-basic-usage.md](https://getcomposer.org/doc/01-basic-usage.md) |
| 55 | +**Symfony Console** - [https://symfony.com/doc/current/components/console.html](https://symfony.com/doc/current/components/console.html) |
| 56 | +**str_contains** - [https://www.php.net/manual/en/function.str-contains.php](https://www.php.net/manual/en/function.str-contains.php) |
| 57 | +**str_starts_with** - [https://www.php.net/manual/en/function.str-starts-with.php](https://www.php.net/manual/en/function.str-starts-with.php) |
| 58 | +**str_ends_with** - [https://www.php.net/manual/en/function.str-ends-with.php](https://www.php.net/manual/en/function.str-ends-with.php) |
| 59 | + |
| 60 | +For Symfony Console you will want to look specifically for the Table Helper. |
| 61 | + |
| 62 | +Oh, and don't forget to use the Composer autoloader with: |
| 63 | + |
| 64 | +```php |
| 65 | +require_once __DIR__ . '/vendor/autoload.php'; |
| 66 | +``` |
0 commit comments