Developer
PHP
PHP TUTORIALPHP IntroductionPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP VariablesPHP ConstantsPHP OutputPHP Data TypesPHP 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 about outputs in PHP in the PHP Output chapter. If you missed it, check it to learn more.

PHP Strings - Getting the Length

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

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

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

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 ››

PHP Strings - Reverse

strrev() function is used to reverse a string.

PHP Strings - Reverse Example


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

Run Example ››

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, above example returns 6 instead of 7.

PHP Strings - Replacing Text within a String

str_replace() function replaces a text within a string.

In this example, "Morning" in "Good Morning" will be changed to "Evening". Therefore, the last 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

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

PHP Strings - Repeating Example


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

Run Example ››
Advertisement
0
57