PHP: Traversing strings
Loops are good not only for processing numbers but also for working with strings — thanks to the ability to retrieve a specific character by its index. Below is some sample code that prints the letters of each word on a separate line:
<?php
function printNameBySymbol(string $name): void
{
$i = 0;
// This check runs until the end of the string,
// including the last character
// Its index is `string length - 1`
while ($i < strlen($name)) {
// Accessing a character by its index
print_r("$name[$i]\n");
$i = $i + 1;
}
}
$name = 'Arya';
printNameBySymbol($name);
// => 'A'
// => 'r'
// => 'y'
// => 'a'The loop goes through each character of the string one by one:
'Arya'
│ │ │ │
A r y a
↓ ↓ ↓ ↓
each character is processed in turnThe most important thing in this code is to set the right condition in while. This can be done in two ways at once:
$i < strlen($name)$i <= strlen($name) - 1
Both ways lead to the same result.
Reversing a string
Instead of printing, you can build a new string. For example, let's write a function that reverses a string:
<?php
function reverseString(string $text): string
{
$result = '';
$i = strlen($text) - 1;
while ($i >= 0) {
$result = $result . $text[$i];
$i = $i - 1;
}
return $result;
}
print_r(reverseString('Arya') . "\n"); // => ayrA
print_r(reverseString('hexlet') . "\n"); // => telxehThe $result variable is initialized with an empty string as the neutral element for concatenation. The loop starts at the last index (strlen($text) - 1), moves toward zero, and ends when the index becomes less than zero. At each step, the current character is added to the result. As a result, the string is built in reverse order.
Instructions
Write a function maskCardNumber() that hides a bank card number. The function should replace all characters of the string with *, except for the last four.
Example of how it works:
<?php
maskCardNumber('1234567812345678'); // '************5678'
maskCardNumber('12345678'); // '****5678'If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
PHP: Traversing strings
Loops are good not only for processing numbers but also for working with strings — thanks to the ability to retrieve a specific character by its index. Below is some sample code that prints the letters of each word on a separate line:
<?php
function printNameBySymbol(string $name): void
{
$i = 0;
// This check runs until the end of the string,
// including the last character
// Its index is `string length - 1`
while ($i < strlen($name)) {
// Accessing a character by its index
print_r("$name[$i]\n");
$i = $i + 1;
}
}
$name = 'Arya';
printNameBySymbol($name);
// => 'A'
// => 'r'
// => 'y'
// => 'a'The loop goes through each character of the string one by one:
'Arya'
│ │ │ │
A r y a
↓ ↓ ↓ ↓
each character is processed in turnThe most important thing in this code is to set the right condition in while. This can be done in two ways at once:
$i < strlen($name)$i <= strlen($name) - 1
Both ways lead to the same result.
Reversing a string
Instead of printing, you can build a new string. For example, let's write a function that reverses a string:
<?php
function reverseString(string $text): string
{
$result = '';
$i = strlen($text) - 1;
while ($i >= 0) {
$result = $result . $text[$i];
$i = $i - 1;
}
return $result;
}
print_r(reverseString('Arya') . "\n"); // => ayrA
print_r(reverseString('hexlet') . "\n"); // => telxehThe $result variable is initialized with an empty string as the neutral element for concatenation. The loop starts at the last index (strlen($text) - 1), moves toward zero, and ends when the index becomes less than zero. At each step, the current character is added to the result. As a result, the string is built in reverse order.
Instructions
Write a function maskCardNumber() that hides a bank card number. The function should replace all characters of the string with *, except for the last four.
Example of how it works:
<?php
maskCardNumber('1234567812345678'); // '************5678'
maskCardNumber('12345678'); // '****5678'If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Your exercise will be checked with these tests:
<?php
namespace HexletBasics\Loops\IterationOverString;
use HexletBasics\Exercise\TestCase;
class SolutionTest extends TestCase
{
public function test()
{
$this->assertEquals('************5678', maskCardNumber('1234567812345678'));
$this->assertEquals('****5678', maskCardNumber('12345678'));
$this->assertEquals('************3333', maskCardNumber('0000111122223333'));
}
}Teacher's solution will be available in:
20:00
