Tuesday, June 23, 2009

Some useful php functions

Find below some useful functions in php. For complete reference of php functions you can refer php.net which is very useful for working with php projects.

If you are beginner for learning php you can refer w3schools.com

strstr() for finding string within a string in PHP

The strstr() function will search for the first occurrence of a string inside another string.

This function returns the rest of the string from the matching point if matching occurs. Otherwise( i-e if the string to search for is not found)it will return FALSE.

For example,
echo strstr("Welcome Hello world!","Hello");
The above statement will return "Hello world!".
Note that it is case sensitive.

So, strstr("Hello world!","hello"); will return false.

For case insensitive use stristr().

Use of eval() function

The eval() function evaluates a string as PHP code. So it can be used to store the php code in database.

$str=”qualitypoint technologies”
echo $str2=”$str $str1 you!”;

Guess what will be the output of $str2 or will it cause error?
It will display sting like that.
$str $str1 you!
Here the use of eval() function is to evaluate the php code/variable as string. So to display php variable as string use eval() function.

eval("\$str2 = \"$str2\";");
then it will display output like this “qualitypoint technologies welcomes you!” . Note the eval() function requires semicolon at the end of string.

Use of reset() function .

The reset() function points the internal cursor position at first element of array.

For example,

$arrNumber = array("first", "second", "third", "fourth");
echo current($arrNumber) . "<br>";
echo next($arrNumber) . "<br>";
echo reset($arrNumber);

Output will be,



Use of list() function

The list() function is used to assign values to a list of variables in one operation.

Note it only works in numeric array.

The below example is used to easily understand this function.

$arr = array("test", "test1", "test2", "test3");


echo “the array value is $a,$b,$c,$d”

output will be ‘the array value is test,test1,test2,test3’

It is mainly used to fetch data from table,.

Consider below example,

$sql="select * from persons";
echo "the id is $id and fname is $fname and lname is $lname and age is $age".”<br>”;

Use of each() function.

The each() function will return the current key and value pair from an array and will move the array cursor.

For example consider the below code,
$arr = array("test", "test1", "test2", "test3");
reset($arr) ; // point cursor to first element
echo “$key=$val”;
Output is, 0=test 1=test1 2=test2 3=test3
Instead of doing above, use foreach()
foreach($arr as $key=>$val)
echo “$key=$val”;
This code also will display the same output as above.

Use of implode() function

The implode function is used to join the array elements with string.
See the below code,
$arr = array("test", "test1", "test2", "test3");
echo $splitarray=implode(“,”,$arr);
then the output will be,
if you use like this $splitarray=implode(“*”,$arr); then it will return values like this
if you want to display array values in new line, use like this
Then it displays array elements like this,

Use of explode() function

The explode function is used to split a string a by string.

consider below example

$data = "foo:*:1023:1000::/home/foo:/bin/sh";
foreach($arr as $value)
echo "value=$value";
It will display output like this

is_array() function

$arr=array(‘test’,’test1’,’test2’) echo is_array($arr) ? 'Array' : 'not an Array'; $no = 'this is a string'; echo is_array($no) ? 'Array' : 'not an Array';
Here the first is_array() function returns ‘Array’ as output, because the $arr is array, but the second function returns the ‘not an Array’. Because it is not an array , noticed that it is just a string.

Use of realpath() function

The realpath() function returns the absolute path name.It removes all symbols like (‘/’,’./’,’../../’) and returns the absolute path name.It is easy way to get file location from root folder. Consider below example,
$filename=”index.php” echo $string=realpath($filename);
the output will be like that,
o/p: c:\apache\htdocs\anbarasan\uselist.php , but normally we required like that, c:/apache/htdocs/anbarasan/uselist.php , so just replace ‘\’ character with ‘/’ using str_replace() function.

Remove backslashes character from string using stripslashes() function

$str=”quality\point technologies” --- here the backslashes character comes between quality and point. But you want the output come without backslash character use stripslashes() function.
echo stripslashes($str);
then it display string like that, qualitypoint technologies. This function also can be used to clean up data retrieved from a database or from an HTML form.

Use of addslashes() function

The addslashes() function returns a string with backslashes in front of predefined characters. The predefined functions are single quotes (‘), double quotes (“) , back slashes (\). Below workaround code is used to easy to understanding of this function.
$name="qualitypoint's logo";
In this case, we will get syntax error from browser because of this single quotes. So to avoid this error we used addslashes() function.
echo addslashes($name);
the output for this code is qualitypoint\'s logo
we faced this single quotes problem earliest, instead of using addslashes() function to avoid this error we used str_replace() function.
$name="qualitypoint's logo";
$name=str_replace("'", "\'",$name);

More Articles...

1 comment:

Anonymous said...

Nice read although the first example is erroneous. Variables enclosed in double quotes evaluate before they get echoed. The case isn't the same with single quotes.

Search This Blog