- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
str_pad() function in PHP
The str_pad() function is used to pad a string to a new length. It returns the padded string.
Syntax
str_pad(str, len, pad_str, pad_type)
Parameters
str − The string to pad
len − The new string length
pad_str − The string to use for padding
pad_type − The side to pad the string.
The following are the possible values −
STR_PAD_BOTH − Pad to both sides of the string. If not an even number, the right side gets the extra padding
STR_PAD_LEFT − Pad to the left side of the string
STR_PAD_RIGHT − Pad to the right side of the string.
Return
The str_pad() function returns the padded string.
Example
The following is an example −
<?php $s = "Welcome"; echo str_pad($s,10,"$",STR_PAD_LEFT); ?>
Output
$$$Welcome
Example
The following is an example −
<?php $s = "Welcome"; echo str_pad($s,10,"$",STR_PAD_RIGHT); ?>
Output
Welcome$$$
Example
The following is an example −
<?php $s = "Welcome"; echo str_pad($s,10,"$",STR_PAD_BOTH); ?>
Output
$Welcome$$
Advertisements
To Continue Learning Please Login
Login with Google