PHP is a programming language that is commonly used for web development. One of the most basic data types in PHP is the string, which is a sequence of characters enclosed in quotation marks.
To declare a string in PHP, you can use either single quotes or double quotes. Here are some examples:
$name = 'John';
$message = "Hello, $name!";
In the first example, the string “John” is assigned to the variable $name
using single quotes. In the second example, the string “Hello, John!” is assigned to the variable $message
using double quotes. Note that you can include variables within double-quoted strings using curly braces {}
.
PHP provides many built-in functions for working with strings. Here are a few examples:
$string = 'hello world';
// string length
echo strlen($string); // output: 11
// uppercase
echo strtoupper($string); // output: HELLO WORLD
// lowercase
echo strtolower($string); // output: hello world
// substring
echo substr($string, 0, 5); // output: hello
In the first example, the strlen()
function is used to determine the length of the string (which is 11 characters). In the second and third examples, the strtoupper()
and strtolower()
functions are used to convert the string to all uppercase and all lowercase, respectively. In the fourth example, the substr()
function is used to extract a substring from the original string (starting at index 0 and including the next 5 characters, resulting in the string “hello”).