How To Work with Strings in PHP | Strings in php

PHP Strings | How To Work with Strings in PHP

Strings can be seen as a stream of characters. For example, ‘V’ is a character and ‘Vkprogramming’ is a string. We have learned about the basics of string data type in PHP in PHP | Data types and Variables.

In this article, we will discuss strings in detail. Everything inside quotes, single (‘ ‘) and double (” “) in PHP is treated as a string.

PHP string is a sequence of characters i.e., used to store and manipulate text. PHP supports only 256-character set and so that it does not offer native Unicode support. There are 4 ways to specify a string literal in PHP.

  • single quoted
  • double quoted
  • heredoc syntax
  • newdoc syntax (since PHP 5.3)

You may like

Creating Strings:

There are four ways of creating strings in PHP:

1.Single-quote strings: This type of string does not process special characters inside quotes.


 // single-quote strings

$site  = 'Welcome to vkprogramming';

echo $site;



Welcome to vkprogramming

The above program compiles correctly.We have created a string ‘Welcome to GeeksforGeeks’ and stored it in variable and printing it using echo statement. Let us now look at the below program:


// single-quote strings

$site  = 'vkprogramming';

echo 'Welcome to $site';


Welcome to $site

In the above program the echo statement prints the variable name rather than printing the contents of the variables. This is because single-quotes strings in PHP do not process special characters. Hence, the string is unable to identify the ‘$’ sign as the start of a variable name.


2.Double-quote strings : Unlike single-quote strings, double-quote strings in PHP are capable of processing special characters.


// double-quote strings

echo"Welcome to vkprogrammingn";

$site  = "vkprogramming";

echo"Welcome to $site";



Welcome to vkprogramming

Welcome to vkprogramming

In the above program, we can see that the double-quote strings are processing the special characters according to their properties. The ‘n’ character is not printed and is considered as a new line. Also instead of the variable name $site, “GeeksforGeeks” is printed.

PHP treats everything inside double quotes(” “) as Strings.

In this article, we will learn about the working of the various string functions and how to implement them along with some special properties of strings. Unlike other data types like integers, doubles, etc. Strings do not have any fixed limits or ranges. It can extend to any length as long as it is within the quotes.

It has been discussed earlier that string with single and double quotes are treated differently. Strings within a single quote ignore the special characters, but double-quoted strings recognize the special characters and treat them differently.


$name = "vikram";

echo"The name of the geek is $name n";

echo'The name of the geek is $name';


The name of the geek is vikram

The name of the geek is $name

Some important and frequently used special characters that are used with double-quoted strings are explained below:


strrev() – Reverse a String

The PHP strrev() function reverses a string.


echo strrev(“Hello world!”); // outputs !dlrow olleH


!dlrow olleH


strpos() – Search For a Text Within a String

The PHP strpos() function searches for a specific text within a string. If a match is found, the function returns the character position of the first match. If no match is found, it will return FALSE.


echo strpos("Hello world!", "world"); // outputs 6




str_replace() – Replace Text Within a String

The PHP str_replace() function replaces some characters with some other characters in a string.


echo str_replace(“world”, “Dolly”, “Hello world!”); // outputs Hello Dolly!


Hello Dolly!

Related:How To Work with Strings in PHP | Strings in php

You may like

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top