DONATE Hyvor Developer
PHP
PHP TUTORIALPHP IntroductionPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP StringsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms Finishing

PHP Strings

A string is any finite sequence of characters (letters, numerals, symbols, punctuation marks, etc.)

PHP Strings Functions

PHP has a lot of string functions. Here we will learn about commonly used string functions with its usage.

PHP Strings - Output

The most popular function in PHP, echo is used to output a string.

PHP Output String Example


<?php
echo 'I am a string';
echo '<br>';

// multiple arguments
echo 'Hello', ' World', '!';

Run Example ››

Note: We have already discussed outputs in PHP in the PHP Output chapter. If you missed it, check it to learn more.

PHP Strings - Getting the Length

The strlen() function can be used to get the length of a string.

PHP Strings - Getting the Length Example


<?php
$str = 'Hyvor Developer';
echo strlen($str); // outputs 15

Run Example ››

PHP Strings - Number of Words

The str_word_count() function returns the number of words in a string.

PHP Strings - Number of Words Example


<?php
$str = 'This is a string with seven words';
echo str_word_count($str);

Run Example ››

PHP Strings - Changing The Case

The strtolower() and strtoupper() are used to make a string lowercase and uppercase respectively.

PHP Strings - Changing The Case Example


<?php
$str = 'Hyvor Developer';

echo strtolower($str); // hyvor developer
echo '<br>';
echo strtoupper($str); // HYVOR DEVELOPER

Run Example ››

PHP Strings - Removing Unnecessary Whitespace

The trim() function removes unnecessary whitespace from the beginning and the end of a string.

PHP Strings - Removing Unnecessary Whitespace Example


<?php
$str = '  Hyvor Developer    ';
echo strlen($str) . '<br>'; // length is 21

$str = trim($str); // remove whitespace
echo strlen($str); // now, length is 15

Run Example ››

Note: The trim() function is commonly used with PHP Forms.

PHP Strings - Reverse

The strrev() function is used to reverse a string.

PHP Strings - Reverse Example


<?php
$str = 'Hyvor Developer';
echo strrev($str);

Run Example ››

The strpos() function returns the position of the first occurrence of a substring in a string.

PHP Strings - Searching for a Substring Example


<?php
$str = 'Hello World';
echo strpos($str, 'World'); // will output 6

Run Example ››

Tip: In PHP, index of strings starts from 0. Therefore, the strpos() function returns 6 instead of 7 in the above example.

PHP Strings - Replacing Text within a String

The str_replace() function replaces a text within a string.

In this example, "Morning" in "Good Morning" will be changed to "Evening". The final result will be "Good Evening".

PHP Strings - Replacing Text within a String Example


<?php
$str = 'Good Morning';
echo str_replace('Morning', 'Evening', $str);

Run Example ››

PHP Strings - Repeating a String

The str_repeat() function is used to repeat a string number of times.

PHP Strings - Repeating Example


<?php
echo str_repeat('*', 20);

Run Example ››
0
157