Friday 31 May 2013

String Operators and Functions in PHP

Strings are used for values that contain a character or group of characters.
As earlier told Strings can be represented in 3 ways : double quotes, single quotes and heredoc.
For more info check out the string datatype in earlier post: http://php-revisited.blogspot.in/2013/05/php-data-types.html

More detailed description can be be Found herePHP MANUAL

Now let us move on To String operators and common functions

String Operators
There are two String Operators in PHP.


  1. Concatenation Operator
To concatenate strings in PHP we use the operator "." (quotes for clarity) .

Example:

<?php
$txt1="Hello world!";
$txt2="What a nice day!";
echo $txt1 . " " . $txt2;
?>

Output:

      2.  Concatenating assignment operator 

This operator appends the argument on the right side to the argument on the left side. It is represented by '.='(quotes for clarity).

Example:

<?php
$a = "Hello ";
$b = $a . "World!"; // now $b contains "Hello World!"
echo $b.'<br>';
$a = "Hello ";
$a .= "World Howz U All!";     // now $a contains "Hello World Howz U All!"
echo $a;
?>

Output:

String Based Functions in PHP

We would here talk only about three basic and most commonly used String functions in PHP. Though there are many string related functions but most of them are not commonly used in basic designing procedure. 

Though if you have any problem related to any one other function feel free to post a comment and ask questions about it and i would try to reply to them as soon as possible.:)
You can get more info about them you by visiting Link1 or Link2

1. strlen()
    This is the most commonly used function. This is  used to find the length of a string. This function can be used with PHP loops to loop through a string.

Example:

<?php
$app="This is a Good Blog";
echo "THE LENGTH OF STRING IS:-->".strlen($app);
?>

Output:
2. strpos()
    The strpos function is used to search for a string or character within a string. 
If a match is found in the string this function returns the position of the first match. If no match is found it will return FALSE.

Example:

<?php
$a="This is great Blog";
$b="This is great great Blog";
echo strpos($a,"great").'<br>';
echo strpos($b,"great").'<br>';
if(strpos($b,"ok")==FALSE)
echo "NOT FOUND";
?>

Output:

3. strcmp()
     int strcmp ( string $str1 , string $str2 )

    This is used to compare two strings and Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal. ie:

If $str1 == $str2 strcmp return 0.
If $str1  > $str2 strcmp return 1.
If $str1  < $str2 strcmp return -1.

This is case sensitive compare.

Example:

<?php
$a="This is Good Blog";
$b="This is Good Blog";
$c="this is Good Blog";
if( strcmp($a,$b) ==0) //returns 0 in this case as both are same
echo "They are equal".'<br>';
if( strcmp($a,$c)==0 ) //case sensitive compare
echo "They are equal".'<br>';
else
echo "They are not equal".'<br>';
?>

Output:


So This was all about string u can get more at PHP STRING and STRING FUNCTIONS or you could simply post a comment and ask questions to me which i would like to answer as fast as possible.

No comments:

Post a Comment