PHP Strings

๐Ÿ“˜ PHP ๐Ÿ‘ 40 views ๐Ÿ“… Dec 22, 2025
โฑ Estimated reading time: 2 min

Strings in PHP are sequences of characters used to store and manipulate text. PHP provides powerful built-in functions to work with strings, making text processing easy and efficient.


1. Creating Strings

Strings can be defined using single quotes or double quotes.

$str1 = 'Hello World'; $str2 = "Hello PHP";

Difference:

  • Single quotes do not parse variables

  • Double quotes parse variables and escape characters

$name = "John"; echo "Hello $name"; // Hello John echo 'Hello $name'; // Hello $name

2. String Length

echo strlen("Hello"); // 5

3. String Concatenation

Use the dot (.) operator.

$first = "Hello"; $second = "PHP"; echo $first . " " . $second;

4. String Comparison

strcmp("php", "php"); // 0 (equal)

5. String Case Functions

strtolower("PHP"); // php strtoupper("php"); // PHP ucfirst("php"); // Php ucwords("hello world"); // Hello World

6. Searching in Strings

strpos()

strpos("Hello World", "World"); // 6

strstr()

strstr("Hello World", "World"); // World

7. Replacing Strings

str_replace("PHP", "Laravel", "I love PHP");

8. Substrings

substr("Hello World", 0, 5); // Hello

9. Trimming Strings

trim(" Hello "); ltrim(" Hello"); rtrim("Hello ");

10. String Escaping

echo "It's a beautiful day"; echo 'It\'s a beautiful day';

11. Formatting Strings

sprintf()

sprintf("Hello %s, age %d", "John", 25);

12. Multibyte Strings (UTF-8)

Use mb_* functions for Unicode strings:

mb_strlen("เคจเคฎเคธเฅเคคเฅ‡");

Conclusion

Strings are fundamental in PHP for handling text, user input, and output. Understanding PHP string operations and functions helps you write clean, efficient, and secure code, especially when developing real-world applications and frameworks like Laravel.


๐Ÿ”’ Some advanced sections are available for Registered Members
Register Now

Share this Post


โ† Back to Tutorials

Popular Competitive Exam Quizzes