This section contains the details about String Handling in PHP.
String Handling in PHP
String variable is used for storing characters. PHP have many functions and operators to manipulate string. A string can be used directly in a function or it can be stored in a variable.
Concatenation of String
PHP has only one operator -concatenate operator(.) which is used to put two values together. Given below the example which will give you a clear idea about how to concatenate two string :
<?php $input_txt1="Hello World!"; $input_txt2="Welcome to PHP world!"; echo $input_txt1 . " " . $input_txt2; ?>
The output of the above code will be :
Hello World! Welcome to PHP world!
In the above example, you can see that we use three things to concatenate- two string and a white space between them.
strlen() function
The strlen() function is used to return the length of the string. Given below the example:
<?php echo strlen("Hello world!"); ?>
strpos() function
The strpos() function is used to search for character within a string. This function return the position of first match. If no match is found, it will return FALSE. For example :
<?php echo strpos("Hello PHP!","PHP"); ?>
The output of the following code is given below :
6
Since index starts with 0 that is why it shows 6. As in normal counting it is at 7th place.
[ 0 ] Comments