PHP Strings
In PHP, strings are sequences of characters used to represent text. Strings are one of the most commonly used data types in PHP and are essential for tasks such as displaying messages, processing user input, and manipulating text data.
Defining a String:
$string = "Hello, World!";
Types of Strings:
Single-Quoted Strings:
'
).\n
(newline) are treated as literals.Example:
$name = 'Alice';
echo 'Hello, $name'; // Outputs: Hello, $name
echo 'Line break \n here'; // Outputs: Line break \n here
Double-Quoted Strings:
"
).\n
(newline) are interpreted.Example:
$name = 'Alice';
echo "Hello, $name"; // Outputs: Hello, Alice
echo "Line break \n here"; // Outputs: Line break
// here
To combine strings, use the concatenation operator (.
).
Example:
<?php
$firstName = "John";
$lastName = "Doe";
$fullName = $firstName . " " . $lastName;
echo $fullName; // Outputs: John Doe
?>
To find the length of a string, use the strlen()
function.
Example:
<?php
$string = "Hello, World!";
echo strlen($string); // Outputs: 13
?>
@aCodeTutorials All Rights Reserved
privacy policy | about