All About Strings in PHP

All About Strings in PHP

Strings in PHP are used to represent a sequence of characters. It is one of the data types that are supported by PHP. It contains alphanumeric characters.

When are strings created in PHP?

Strings in PHP are created when:

  • There’s a need to declare variables and assign characters to them.
  • You need to use PHP strings with the echo statement.

String Functions

In PHP, string functions are used to modify a string or query knowledge about a string. One of the basic functions of strings is the length function. The length of a string is returned by this function.

Ways to Specify a String in PHP?

Specifically, there are four ways of specifying a string in PHP.

1. Single Quoted:

This type of string doesn’t process the special characters inside the quotes.

Code:

<?php

// single-quote strings

$site  = 'World';

echo 'Welcome to $site';

?>

 

Output:

Welcome to $site

 

2. Double Quoted:

The double quote strings allow the processing of special characters.

Code:

<?php

// double-quote strings

echo "Welcome to World \n";

$site  = "World";

echo "Welcome to $site";

?>

 

Output:

Welcome to World

 

3. Heredoc:

Heredoc syntax (<<<) is a way of delimiting the strings. In this, an identifier is provided after this <<< operator and a new line is started for text.

Code:

<?php

$str = <<<Demo

Hello World

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

echo $str;

?>

 

Output:

Hello World

 

4. Newdoc:

Newdoc syntax (<<<) is similar to heredoc, but in this, parsing is not done. Here, an identifier is enclosed in single quotes. For example, <<<‘DEMO’.

Code:

<?php

$str = <<<'DEMO'

Welcome to PRP Webs.

Let’s learn the newdoc example.

DEMO;

echo $str;

echo '</br>';




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

Welcome to PRP Webs.

Let’s learn the newdoc example.

Demo;

?>

 

Output:

Welcome to PRP Webs. Let’s learn the newdoc example.

Welcome to PRP Webs. Let’s learn the newdoc example.

 

Main String Functions in PHP

1. strlen()

This string function is used to return the length of any string.

Syntax:

Strlen(String);

Code:

<?php

echo strlen('Welcome to string functions in php');//will return the length of given string

?>

 

Output:

34

 

2. Str_word_count()

It is used to display the number of words in a given string ().

Syntax:

Str_word_count(String)

Code:

<?php

echo str_word_count('Welcome to string functions in php');//return the number of words in an String

?>

 

Output:

6

 

3. Strrev()

It is a function that reverses a string with any characters.

Syntax:

Strrev()

Code:

<?php

echo strrev('Welcome to string functions in php');//return the number of words in an String

?>

 

Output:

Php ni snoitcnuf gnirts ot emocleW

 

4. Strpos()

This strings allows you to check for a specific text within a string. It functions by matching its basic text. If a match is found, the location is returned. Otherwise, you’ll get “False” as an output.

Syntax:

Strpos(String,text);

Code:

<?php

echo strpos('Welcome to string functions in php','string');

?>

 

Output:

11

 

5. Strreplace()

It is a built-in function that allows you to replace specific text in a string.

 

Syntax:

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

Code:

<?php

echo str_replace('java', 'php', 'Welcome to java');

?>

 

Output:

Welcome to java

 

6. Ucwords()

This string function converts the first letter of each word to uppercase.

 

Syntax:

Ucwords(string);

Code:

<?php

echo ucwords('Welcome to PHP');

?>

 

Output:

WELCOME TO PHP

welcome to php

 

7. Strcmp()

It is be used to compare two strings. It produces an output that is greater than, less than, or equal to zero.

 

Syntax:

Strcmp(string1,string2)

Code:

<?php

echo strcmp("hi","hi");

echo '<br>';

?>

 

Output:

0

 

8. substr()

This string function gives the substring of a given string.

Code:

<?php

$input  = "Hello World";

echo(substr($input,3));

?>

 

Output:

lo World

 

9. strtoupper()

This string function converts a given string into uppercase.

Code:

<?php

$input  = "Hello World";

echo strtoupper($input);

?>

 

Output:

HELLO WORLD

 

10. strtolower()

This string function converts a given string into lowercase.

Code:

<?php

$input  = "Hello World";

echo strtolower($input);

?>

 

Output:

hello world

 

11. explode()

This string function allows a string to convert into an array.

Code:

<?php

$input  = "Hello World";

print_r(explode(" ",$input));

?>

 

Output:

Array ( [0] => Hello [1] => World )

 

12. trim() function

This string function allows you to remove the whitespace from the sides of a string.

Code:

<?php

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

?>

 

Output:

llo Worl

 

Conclusion

A string is a set of letters, numbers, arithmetic values, or a mix of them. It can be enclosed in single quotation marks (‘’) or double quotation marks (“”).

The PHP string functions are built into the heart of this programming language. It doesn’t require any installations.

That’s all about Strings functions in PHP. If you’re interested in knowing more about PHP string functions, do let us know in the comments section.

In case you have any questions for us, leave them in the comments sections below, and we’ll get in touch with you right away!

Hire your Expert Shopify Developer
No credit card required.
Free Trial
Image
Hire your Expert Shopify Developer
No credit card required.
Free Trial
Image