PHP – Strings Functions

PHP – Strings Functions

String functions are the most important functions frequently used in PHP. String is the sequence of characters, like “Let’s learn String functions”. There is no installation required to use string function.

Example

<?php

$name = “Hirdesh Bhardwaj”;

print($name);

?>

String Concatenation Operator

To concatenate two string variables together, we use the dot (.) operator −

<?php

$txt1=”Hello Readers”;

$txt2=”8756″;

echo $txt1 . ” ” . $txt2;

?>

Using the strlen() function

The strlen() function is used to get the length of a string.

<?php

$txt=”Webs Jyoti”;

echo strlen($txt);

?>

This will produce the following result – 10 (including space)

Using the strpos() function

The strpos() function is used to search for a text or character within a string. If the value is found then this will return the position of the first value. If no match is found, it will return FALSE.

Let’s see if we can find the string “PHP” in our string −

<?php

echo strpos(“Hello PHP”,”PHP”);

?>

This will produce the following result – 6

The explode() Function: Used to convert strings into an array variable. It can be used in case where the information that you want lies in a larger string, you can extract it out with the explode() function.

Example:

$db = explode(‘;’,”host=localhost;db=hirdesh;uid=root “);

print_r($settings);

Output:

Array ( [0] => host=localhost [1] => db=hirdesh [2] => uid=root [3]

Enjoy the tutorial? Please spread the word :)

Follow by Email
Facebook
Facebook
Google+2k
Google+
http://hirdeshbhardwaj.com/2017/09/07/php-2/
YouTube39
YouTube