Skip to content

Strings in php

A string in PHP is a sequence of characters, including letters, numbers, and special symbols. Strings are one of the most commonly used data types in PHP, allowing developers to manipulate text, display messages, and process user input.


1. Declaring Strings in PHP

Strings can be created using single quotes (‘) or double quotes (“).

1.1 Single-Quoted Strings

  • Literal strings (variables are not parsed).
  • Escape sequences like \n (new line) do not work except \\ and \’.

<?php

$str = ‘Hello, PHP!’;

echo $str; // Output: Hello, PHP!

?>

<?php

$name = ‘John’;

echo ‘Hello, $name’; // Output: Hello, $name (no variable interpolation)

?>

1.2 Double-Quoted Strings

  • Variable interpolation is supported (variables inside double quotes are replaced with their values).
  • Escape sequences like \n (new line), \t (tab) work.

<?php

$name = “John”;

echo “Hello, $name”; // Output: Hello, John

?>

<?php

echo “Hello,\nWorld!”; // Output: Hello,

                        //         World!

?>


2. String Concatenation

Strings can be joined using the . operator.

<?php

$firstName = “John”;

$lastName = “Doe”;

$fullName = $firstName . ” ” . $lastName;

echo $fullName; // Output: John Doe

?>


3. String Functions in PHP

PHP provides various built-in functions for string manipulation.

3.1 Finding String Length

The strlen() function returns the length of a string.

<?php

$str = “Hello, World!”;

echo strlen($str); // Output: 13

?>

3.2 Counting Words in a String

The str_word_count() function counts the number of words in a string.

<?php

$str = “Hello World, PHP is awesome!”;

echo str_word_count($str); // Output: 5

?>

3.3 Reversing a String

The strrev() function reverses a string.

<?php

$str = “PHP”;

echo strrev($str); // Output: PHP (palindromes remain the same)

?>

3.4 Finding a Substring

The strpos() function returns the position of the first occurrence of a substring.

<?php

$str = “Hello, World!”;

echo strpos($str, “World”); // Output: 7

?>

3.5 Replacing a Substring

The str_replace() function replaces occurrences of a substring with another.

<?php

$str = “Hello, World!”;

echo str_replace(“World”, “PHP”, $str); // Output: Hello, PHP!

?>


4. String Case Manipulation

4.1 Convert to Lowercase

The strtolower() function converts a string to lowercase.

<?php

echo strtolower(“HELLO WORLD!”); // Output: hello world!

?>

4.2 Convert to Uppercase

The strtoupper() function converts a string to uppercase.

<?php

echo strtoupper(“hello world!”); // Output: HELLO WORLD!

?>

4.3 Capitalizing the First Letter

The ucfirst() function capitalizes the first letter of a string.

<?php

echo ucfirst(“hello world!”); // Output: Hello world!

?>

4.4 Capitalizing the First Letter of Each Word

The ucwords() function capitalizes the first letter of each word.

<?php

echo ucwords(“hello world php”); // Output: Hello World Php

?>


5. Extracting Substrings

The substr() function extracts a portion of a string.

<?php

$str = “Hello, World!”;

echo substr($str, 0, 5); // Output: Hello

?>

  • substr($str, start, length)
  • Negative values extract from the end.

<?php

echo substr(“Hello, World!”, -6); // Output: World!

?>


6. Trimming Strings

6.1 Removing Spaces

  • trim() removes spaces from both ends.
  • ltrim() removes spaces from the left.
  • rtrim() removes spaces from the right.

<?php

$str = ”  Hello, World!  “;

echo trim($str);  // Output: “Hello, World!”

?>


7. Splitting Strings

The explode() function splits a string into an array.

<?php

$str = “apple,banana,orange”;

$arr = explode(“,”, $str);

print_r($arr);

?>

Output:

Array ( [0] => apple [1] => banana [2] => orange )

The implode() function joins an array into a string.

<?php

$arr = [“apple”, “banana”, “orange”];

echo implode(“, “, $arr);

?>

Output:

apple, banana, orange


8. Escaping Special Characters

The addslashes() function adds slashes before special characters like , , and ****.

<?php

$str = “John’s book”;

echo addslashes($str); // Output: John\’s book

?>

To remove slashes, use stripslashes().

<?php

echo stripslashes(“John\’s book”); // Output: John’s book

?>


9. Multiline Strings

9.1 Using Heredoc Syntax

Heredoc allows writing multi-line strings without escaping quotes.

<?php

$text = <<<EOT

This is a multiline

string using heredoc syntax.

EOT;

echo $text;

?>

9.2 Using Nowdoc Syntax

Nowdoc is similar to single-quoted strings, meaning variables are not parsed.

<?php

$text = <<<‘EOT’

This is a nowdoc string. $name will not be parsed.

EOT;

echo $text;

?>


10. Checking String Type

The is_string() function checks if a variable is a string.

<?php

$var = “PHP”;

echo is_string($var) ? “Yes” : “No”; // Output: Yes

?>


11. Comparing Strings

11.1 Using strcmp()

Compares two strings (case-sensitive). Returns:

  • 0 if equal
  • < 0 if first string is smaller
  • > 0 if first string is larger

<?php

echo strcmp(“apple”, “banana”); // Output: -1

?>

11.2 Using strcasecmp()

Same as strcmp() but case-insensitive.

<?php

echo strcasecmp(“Apple”, “apple”); // Output: 0 (equal)

?>


12. Conclusion

  • PHP strings can be declared using single (‘) or double (“) quotes.
  • String functions like strlen(), strpos(), str_replace(), and substr() are commonly used for manipulation.
  • Heredoc and Nowdoc allow multi-line string handling.
  • Escape characters and trimming functions help in formatting.
  • Comparison functions (strcmp(), strcasecmp()) are useful for checking equality.

Understanding these functions helps in processing user input, generating dynamic content, and working with databases efficiently.