PHP Strings

Strings are an integral part of the programming world. They allow our code to communicate with others. Strings are an integral part of the programming world. PHP also provides various string methods to manipulate strings effortlessly. In this unit, we will learn about string data in PHP.

What Is String?

PHP programming supports string datatype. A string is a sequence of numbers, letters, special characters, and arithmetic values or a combination of all. Everything inside a single quote (‘ ‘) and Double quotes (” “) treats as a string. In simple words, a string is a series of characters used to store and manipulate text.

There are four ways to specify the string literals in PHP:

  1. Create string using single quotes
  2. Create string using double quotes
  3. Heredoc syntax
  4. Nowdoc syntax

Read Also: PHP Operators

Create string using single quotes

We can create the Php string by writing the text within single quotes. It is the easiest way to create a string. This type of string does not process the unique characters inside the single quotes.

Example 1

<?php

// single-quote strings

$a= 'Welcome Suman'; 

echo $a;

?>

Output

Welcome Suman

In the above example, we create a string welcome Suman and store it in a variable and print it using an echo statement.

Example 2

<?php

// single-quote strings 

$a= 'Welcome Suman'; 

echo 'Welcome to $a'; 

?>

Output

Welcome $a

In the above example, the echo statement prints the variable name rather than printing the variable’s content because the single quote in a string does not process the individual characters. The string is unable to identify the $ sign.

Create string using Double quotes

We can create the Php string by writing the text within the double-quotes. This type of string process the individual characters within the double-quotes. It uses to create relative complex strings compare to single quotes.

Example

<?php 

// double-quote strings 

echo "Welcome Suman \n"; 

$a = "errorsea.com"; 

echo "Welcome to $a"; 

?>

Output

Welcome Suman

Welcome to errorsea.com

In the above example, as we can see, the double quotes process the special characters. It prints the content of the variable instead of $a.

Some of the special characters that uses with double quotes:

  • \n -> a new line
  • \t -> a tab space
  • \$ -> a dollar sign
  • \r -> a carriage return
  • \\ -> a backslash
  • \” -> a double quote
  • \’ -> a single quote

Heredoc Syntax

The heredoc syntax uses to create a complex string compare to double-quotes. If we create a string with multiple lines using double quotes, it will generate an error as heredoc overcome all the problems that we are facing with double-quotes. Heredoc supports all the features and allows creating string values with multiple lines. In heredoc syntax, an identifier provides after heredoc <<< operator and after that a new line is started to write any text. The identifier should only contain alphanumeric characters and underscores and must begin with an underscore or a non-digit character.

Example 1

<?php

$str = <<<SUMAN

It is a valid example

SUMAN; //Valid code as whitespace or tab is not valid before closing identifier

echo $str;

?>

Output

It is a valid example.

Example 2

<?php

$str = <<<SUMAN

It is Invalid example

  SUMAN; //Invalid code as whitespace or tab is not valid before closing identifier

echo $str;

?>

The code in the above example will generate an error. We cannot use whitespace or tab before and after the identifier & semicolon.

Nowdoc syntax

The nowdoc creation method is like the heredoc syntax, but it uses single quotes. No parsing takes place inside the new doc. It can also identify with three less than symbol <<< followed by an identifier.

The difference between the heredoc and the new doc is that it uses single quote strings, and the new doc uses double quote strings.

Example

<?php

$str = <<<'DEMO'  

Welcome Suman.

DEMO;

echo $str;

echo '</br>';

echo <<< 'Demo' // Here we are not storing string content in variable str.  

Welcome Suman.

Demo;

?>

Output

Welcome Suman

Welcome Suman

Built-In String Functions

PHP provides many built-in string functions. String values manipulate using PHP string functions. Below we discuss some PHP string functions that we use in our daily and regular programs.

strlen() Function

The strlen() function displays the length of any string. This function accepts the arguments and returns the length or number of characters in the string.

Syntax

strlen(string);

Example

<?php

echo strlen("Hello Suman!");

?>

Output

12

strrev() Function

The string is reverse using strrev() function. This function accepts the arguments and the reverse string.

Syntax

strrev(string);

Example

<?php

echo strrev("Hello suman!"); 

?>

Output

!namus olleH

str_replace Function

For replacing specific text within a string str_replace(), built function comes in picture.

Syntax

Str_replace(string to be replaced,text,string);

Example

<?php

echo str_replace(“Home”, “Suman”, “Welcome Home”);

?>

Output

Welcome Suman

str_pos Function

This function takes two string arguments, and if the second string is present in the first one, it will return the starting position of the string otherwise returns FALSE.

Syntax

Strpos(string,text);

Example

<?php

echo strpos("Hello suman!", "suman")

?>

Output

6

strtoupper() Function

It converts the whole string into uppercase.

Syntax

Strtoupper(string);

Example

<?php

echo strtoupper(“welcome suman”);

?>

Output

WELCOME SUMAN

Strtolower() Function

It converts the whole string into lowercase.

Syntax

Strtolower(string);

Example

<?php

echo strtolower(“WELCOME SUMAN”);

?>

Output

Welcome suman

str_word_count() Function

str_word_count function displays the number of words in a specific string.

Syntax

Str_word_count(string);

Example

<?php

echo str_word_count(“Welcome Suman”);

?>

Output

2

str_repeat() Function

For repeating a string specific number of times, str_repeat() function is used.

Syntax

str_repeat(string,repeat);

Example

<?php

echo str_repeat(“=”,13);

?>

Output

=============

strcmp() Function

Using this built-in function, we can compare the two strings. It returns output greater than zero, less than zero, or equal to zero. Returns greater than zero if string 1 is greater than string 2. It returns less than zero If string 1 is less than string 2. If strings are equal, it returns zero.

Syntax

Strcmp(string1,string2);

Example

<?php

echo strcmp(“Suman”,”SUMAN”);

echo “<br>”;

echo strcmp(“suman”,”suman”);  //Both the strings are equal

echo “<br>”;

echo strcmp(“Suman”,”Hello”);

echo “<br>”;

echo strcmp(“a”,”b”);  //compares alphabetically

echo “<br>”;

echo strcmp(“abb baa”,”abb baa caa”);  //compares both strings and returns the result in terms of number of characters.

?>

Output

1

0

-1

-1

-4

substr() Function

Using substr() function, we can display or extract a string from a particular position.

Syntax

substr(string,start,length);

Example

<?php

echo substr (“Hello World”,6);

?>

Output

World

trim() Function

This function allows us to remove whitespace or predefined characters from both sides of the string.

Syntax

trim(string,charlist);

Example

<?php 

echo trim("Hello World!", "Hed!"); 

?>

Output

llo worl

Read More: PHP Numbers

Conclusion

A string is a set of characters. Everything inside a single quote (‘ ‘) and Double quotes (” “) treats as a string.

In this unit, we learn the most commonly use PHP String functions, and about strings, I hope you have a better understanding of PHP strings.

Leave a Reply

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

Read previous post:
top 25 css interview questions
Top 25 CSS Interview Questions and Answers for Experienced (Download Free PDF)

We have the set of HTML CSS interview questions and answers for experienced to freshers, which will help you to...

Close