03 January 2011

Php For Beginners (Lesson 3)

Overview

In this lesson we will be discussing strings in php and some common and useful functions and operations performed on strings. So what is a string variable? Basically a string variable is just an ordinary variable except that it stores text in it.

Example
< ?php
$test=”This is a test string variable which stores text values.”;
echo $test; // outputs “This is a test string variable which stores text values.”
?>

Concatenation

If you want to merge two string variables or you want to append some text in an existing variable then you need to use concatenation operator (.).

Example # 1
< ?php
$first_variable=”Hello ”;
$second_variable=”world”;
echo $first_variable.$second_variable; // outputs “Hello world”
?>

In Example 1 we are concatenating two string variables. So the output will be the concatenated value i.e “Hello world”.

Example # 2
< ?php
$first_variable=”Hello ”;
echo $first_variable.” world”; // outputs “Hello world”
?>

In example 2 we are appending some text in an existing variable. So the output will be “Hello world”.

Str_replace() Function

Str_replace() is one of the most important functions used in manipulating string variables. This function is used to find and replace any piece of text.

Syntax

str_replace(string to be searched ,string to be replaced with,subject variable);

Example
< ?php
$first_variable=”Hello world”;
str_replace(“world”,”guys”,$first_variable);
echo $first_variable; // outputs “Hello guys”
?>

In above example we replaced “world” with “guys” so the output was “Hello guys”.
Strstr() Function

Strstr() is another useful function for manipulating string variables. This function is used to check whether a piece of text exist in a string variable or not. Strstr() returns false if searched string is not fount and if it is found then it will return the rest of the string from the last occurrence of searched string.

Syntax

strstr(subject variable,string to be searched);

Example
< ?php
$first_variable=”My email address is test@example.com”;
$check=strstr($first_variable,”@”); // outputs “@example.com”
?>

Strlen() Function

Strlen() function returns the length of the given string variable or string. This function comes in handy in many situations where you need to find out exactly how many characters a string contains.

Syntax

Strlen(string str);

Example
< ?php
$first_variable=”My email address is test@example.com”;
echo strlen($first_variable); // outputs “36”
?>

In the above example we tried to find out the length of a string variable so the output was 36 i.e. number of characters in that variable including spaces were 36.
Strpos() Function

This function is used to find out the exact position at which a certain piece of text or a character is located in the given string variable or string.

Syntax

Strops(subject,string to be searched);

Example
< ?php
$first_variable=”My email address is test@example.com”;
echo strpos($first_variable,”@”); // outputs “24”
?>

In the above example we tried to find out the location of “@” in the given string. So the output was 24 i.e. exact position of “@” in the given string variable.

References

http://www.php.net
http://www.w3schools.com

next Previous Lesson --- Next Lesson next

No comments: