PHP provides string padding and repeating functions like str_pad() and str_repeat() that allow you to modify strings by padding them with characters or repeating them multiple times. These functions are useful for formatting strings or creating fixed-length strings.
str_pad(string, length, pad_string, pad_type)str_repeat(string, times)
// Padding a string to fixed length using default right padding
<?php
$string = "PHP";
$padded = str_pad($string, 10, "*");
echo $padded;
?>
PHP******
// Demonstrating left, right, and both side padding
<?php
$string = "PHP";
echo str_pad($string, 10, "*", STR_PAD_LEFT);
echo str_pad($string, 10, "*", STR_PAD_RIGHT);
echo str_pad($string, 10, "*", STR_PAD_BOTH);
?>
*****PHP
PHP*****
**PHP****
// Repeating a string multiple times
<?php
$string = "PHP";
echo str_repeat($string, 3);
?>
PHPPHPPHP
Padding visualization using blocks
str_pad() for clean aligned outputSTR_PAD_LEFT for claritystr_repeat()str_pad()