String Handling in PHP

String Handling in PHP


Posted in : PHP Posted on : December 31, 2010 at 5:43 PM Comments : [ 0 ]

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.

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics