PHP Strings
A string is a sequence of characters, like “Hello, World!”.
1 2 3 4 |
<?php $str = "Hello, World!"; echo $str; ?> |
PHP – Modify Strings
PHP provides numerous functions to modify strings.
1 2 3 4 |
<?php echo strtolower("HELLO WORLD!"); // Outputs: hello world! echo strtoupper("hello world!"); // Outputs: HELLO WORLD! ?> |
PHP – Concatenate Strings
Concatenate two or more strings using the .
operator.
1 2 3 4 5 |
<?php $txt1 = "Hello"; $txt2 = "World"; echo $txt1 . " " . $txt2; // Outputs: Hello World ?> |
PHP – Slicing Strings
Extract a part of a string using substr()
.
1 2 3 |
<?php echo substr("Hello, World!", 0, 5); // Outputs: Hello ?> |
PHP – Escape Characters
Escape special characters within strings.
1 2 3 |
<?php echo "He said, \"Hello, World!\""; // Outputs: He said, "Hello, World!" ?> |