Advertisement

Google Ad Slot: content-top

PHP Strings

~1 min read · PHP
On this page

Strings are sequences of characters used to store and manipulate text. PHP provides a variety of functions for working with strings, making it easy to create, modify, and manipulate text content.



1. Creating Strings


Single Quoted Strings:

The simplest way to create a string. It does not interpret variables or special characters (except for \\ and \').

Example
Try it yourself

Double Quoted Strings:

More flexible. They parse variables and escape sequences like \n (newline).

Example
"; echo $newline."
"; echo $str; ?>
Try it yourself

Common String Functions

Method

Description

Example

strlen()

Returns the length of a string

echo strlen("Hello World!"); // Outputs: 12


Try it yourself

str_word_count()

Returns counts the number of words in a string

echo str_word_count("Hello world!"); // Outputs: 2


Try it yourself

strpos()

Finds the position of the first occurrence of a substring in a string

echo strpos("Hello World", "World"); // Outputs: 6


Try it yourself

substr()

Returns a portion of a string

echo substr("Hello World", 0, 5); // Outputs: Hello


Try it yourself