Q.1 String Variables in PHP
String variables are used for values that contains characters.
we create a string we can manipulate it. A string can be used directly in a function or it can be stored in a variable.
Below, the PHP script assigns the text "Hello World" to a string variable called $txt:
The output of the code above will be:
Hello World
Q.2 The Concatenation Operator
There is only one string operator in PHP.
The concatenation operator (.) is used to put two string values together.
To concatenate two string variables together, use the concatenation operator:
The output of the code above will be:
Hello World! What a nice day!
Q.3 The strlen() function
The strlen() function is used to return the length of a string.
Let's find the length of a string:
The output of the code above will be:
12
Q.4 The strpos() function
The strpos() function is used to search for character within a string.
If a match is found, this function will return the position of the first match. If no match is found, it will return FALSE.
Let's see if we can find the string "world" in our string:
The output of the code above will be:
6
Q.5 What is String?
A string is a data type used in programming, such as an integer and floating point unit, but is used to represent text rather than numbers. It is comprised of a set of characters that can also contain spaces and numbers. For example, the word "hamburger" and the phrase "I ate 3 hamburgers" are both strings. Even "12345" could be considered a string, if specified correctly. Typically, programmers must enclose strings in quotation marks for the data to recognized as a string and not a number or variable name.
For example, in the comparison:
if (Option1 == Option2) then ...
Option1 and Option2 may be variables containing integers, strings, or other data. If the values are the same, the test returns a value of true, otherwise the result is false. In the comparison:
if ("Option1" == "Option2") then ...
Option1 and Option2 are being treated as strings. Therefore the test is comparing the words "Option1" and "Option2," which would return false. The length of a string is often determined by using a null character.