PHP

String

PHP / String

String

Strings

In PHP, a string is a data type used to store text. It is made up of a sequence of characters such as letters, numbers, symbols, and spaces.

A string must always be written inside quotes. 

Double or Single Quotes

Single Quotes (' ')

  • Treats everything as plain text
  • Does not replace variables with values
  • Does not interpret special characters like \n, \t

Double Quotes (" ")

  • Replaces variables with their values
  • Supports special characters like:
    • \n → new line
    • \t → tab space

 

String Functions

string functions are built-in functions used to perform operations on text (strings) such as finding length, changing case, replacing words, and more.

1. strlen() – Returns the number of characters in a string.

2. str_word_count() – Counts the number of words in a string.

3. strrev() –Reverses the given string.

4. str_replace() – Replaces a word or character in a string.

5. strtoupper() – Converts string to uppercase.

6. strtolower() – Converts string to lowercase.

 

Example

// String length: counts total characters
echo strlen("Hello PHP");
echo "
";

// Word count: counts total words
echo str_word_count("Hello PHP Language");
echo "
";

// Reverse string: reverses the text
echo strrev("Hello");
echo "
";

// Replace string: replaces PHP with World
echo str_replace("PHP", "World", "Hello PHP");
echo "
";

// Uppercase: converts all letters to capital
echo strtoupper("hello");
echo "
";

// Lowercase: converts all letters to small
echo strtolower("HELLO");
?>

Concatenate Strings

concatenation means joining two or more strings together to form a single string.

Example

$firstName = "John";
$lastName = "Doe";

echo $firstName . $lastName;
?>

Technology
PHP
want to connect with us ?
Contact Us