PHP String function

PHP String function

In this article I will describe some basic string function which is used in php. Let’s we have a brief idea about it.

Concatenation Operator in PHP:

The concatenation operator is used to put two string values together. Concatenation operator is define by ‘.’(Dot) sign.

Let’s we have an example, how to concatenate two string values using concatenation operator (.).

Example:

<html>

<head>

<title>Concatenation Operator Demo</title>

</head>

<body>

<?php

// Declarefirststring

$str1="Hello";

// Declaresecondstring

$str2="word";

// Concatenatestr1andstr2

$result=$str1."".$str2;

// display result

echo $result;

?>

</body>

</html>

Output:

strlen () function :

In PHP, strlen() function is used to return the length of string.

Let’s we have an example, how to use strlen() function in php.

Syntax: strlen (string value);

Example:

<html>

<head>

<title>Calculate String Length </title>

</head>

<body>

<?php

// Declarestring type variable

$lenDemo="This is a demo string to calculate its length";

// calculatelengthofstring

$calstrlen =strlen($lenDemo);

echo"String length is:" .$calstrlen;

?>

</body>

</html>

Output:

strops() function:

The strpos() function is used to search for a character/textwithin a string. i.e. with the help of strpos() functionyou can search a specific character or specific text string. If match is found, strops() function will returns character position of first match. If no match is found, it will return false (Nothing to display).

Syntax:strops(string strname, value);

Let’s we have an example, how to search a character or text within a string.