PHP String
In PHP, a string is a sequence of characters. Strings can be created by enclosing characters within single quotes ('...') or double quotes ("..."). Here are some examples:
$name = 'John'; // single-quoted string
$message = "Hello $name!"; // double-quoted string with variable interpolation
In the example above, the variable $name
is interpolated into the string using the syntax "$name"
.
PHP also provides a wide range of functions for manipulating strings, such as strlen()
for getting the length of a string, substr()
for extracting a substring from a string, strpos()
for finding the position of a substring within a string, and many others. Here's an example:
$str = 'hello world';
echo strlen($str); // output: 11
echo substr($str, 0, 5); // output: hello
echo strpos($str, 'world'); // output: 6
In the example above, the strlen()
function is used to get the length of the string $str
, the substr()
function is used to extract the first 5 characters of the string, and the strpos()
function is used to find the position of the substring 'world' within the string.