Defining Function
Syntax:
function functionName() {
code to be executed;
}
Example:
function familyName($fname, $year) {
echo "$fname Refsnes. Born in $year <br>";
}
familyName("Hege", "1975");
Default argument
function setHeight($minheight = 50) {
echo "The height is : $minheight <br>";
}
setHeight(350);
setHeight(); // will use the default value of 50
User Defined Functions
Function with argument:
Formal Argument
<?php
function writeName($fname)
{
echo $fname . " Refsnes.<br />";
} Fn. Call with Actual Argument
echo "My name is ";
writeName("Kai Jim");
echo "My sister's name is ";
writeName("Hege"); Output:
echo "My brother's name is ";
writeName("Stale"); My name is Kai Jim Refsnes.
?> My sister's name is Hege Refsnes.
My brother's name is Stale Refsnes.
User Defined Functions
Function with arguments & return value:
<?php
function calculateAmt($cost, $num)
{
return ($cost * $num);
}
$price=10;
$tot=5;
Echo “ Total Amount “, calculateAmt($price, $tot);
?>
Output:
Total Amount 50
User Defined Functions
Fn. Call by Ref.:
<?php
$cost = 20.99;
$tax = 0.0575;
function calculateCost(&$cost, $tax) Output:
{
// Modify the $cost variable (bfr fn call) Cost is 20.99
$cost = $cost + ($cost * $tax); Tax is 5.75
// Perform some random change to the (aft fn call) Cost is: 22.20
$tax variable.
$tax += 4;
}
Echo “(bfr fn call) Cost is $cost <br>”;
calculateCost($cost, $tax);
Echo "Tax is $tax*100 <br>";
Echo “(aft fn call) Cost is: $cost”;
?>
String Functions
The count_chars(string,mode ) function returns how many times an
ASCII character occurs within a string and returns the information.
Output:
Array
E.g: (
[32] => 1
[33] => 1
<?php [72] => 1
$str = "Hello World!"; [87] => 1
print_r(count_chars($str,1)); [100] => 1
?> [101] => 1
[108] => 3
[111] => 2
[114] => 1
)
String Functions
The different return modes are:
0 - an array with the ASCII value as key and number of
occurrences as value
1 - an array with the ASCII value as key and number of
occurrences as value, only lists occurrences greater than zero
2 - an array with the ASCII value as key and number of
occurrences as value, only lists occurrences equal to zero are
listed
3 - a string with all the different characters used
4 - a string with all the unused characters
String Functions
The explode(separator, string, limit) function breaks a string into an
array.
Output:
Array
(
E.g: [0] => Hello
[1] => world.
[2] => It's
<?php
[3] => a
$str = "Hello world. It's a beautiful day.";
[4] => beautiful
print_r (explode(" ",$str));
[5] => day.
?>
)
String Functions
The implode(separator,array ) & join(separator,array ) function
returns a string from the elements of an array.
E.g: Output:
Hello World! Beautiful Day!
<?php
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
?>
String Functions
The ltrim(string,charlist) & rtrim(string,charlist) function will remove
whitespaces or other predefined character from the left and right side of a string respectively.
The str_shuffle(string) function randomly shuffles all the characters of a string.
String Functions
The strlen(string) function returns the length of a string.
E.g: Output:
<?php Length = 12
$a= “hello World!”;
echo “Length = “, strlen($a);
?>
String Functions
The strrev(string) reverses the given string.
E.g: Output:
<?php Reverse of “hello World!” is !dlroW olleh
$a= “hello World!”;
echo “Reverse of \”$a\” is “, strrev($a);
?>
String Functions
The strtoupper(string) converts to upper case character
The strtolower(string) converts to lower case character
E.g: Output:
<?php
$a= “hello World!”; Upper of “hello World!” is HELLO
echo “Upper of \”$a\” is “, strtoupper($a); WORLD!
echo “Lower of \”$a\” is “, strtolower($a); Lower of “hello World!” is hello world
?>
String Functions
The strpos(string,exp) returns the numerical position of first appearance
of exp.
The strrpos(string,exp) returns the numerical position of last appearance
of exp.
strripos() - Finds the position of the last occurrence of a string inside another
string (case-insensitive)
E.g: Output:
<?php 2
$a= “hello World!”; 10
echo strpos($a,”l”),”<br>”;
echo strrpos($a,”l”);
?>
String Functions
The substr(string,start,length) function returns a sub string of the size
“length” from the position of “start”.
E.g: Output:
<?php lo
$a= “hello World!”; ld!
echo substr($a,3,2); l
echo substr($a,-3); hello Wor
echo substr($a,-3,1);
echo substr($a,0,-3);
?>
String Functions
The substr_count(string,substr) counts number of times a sub string
occurred in given string.
E.g: Output:
<?php 2
$a= “hello Worlod!”;
echo substr_count($a,”lo”);
?>
String Functions
The substr_replace(string,replacement,start,len) replaces a
portion of a string with a replacement string, beginning the substitution at a specified
starting position and ending at a predefined replacement length.
E.g: Output:
<?php heaaorld!
$a= “hello World!”;
echo substr_replace($a,”aa”,2,5);
?>
String Functions
The ucfirst(string) converts the first character to upper case.
The ucwords(string) converts the first character of each word to upper case.
E.g: Output:
<?php Hello world!
$a= “hello world!”; Hello World!
echo ucfirst($a),”<br>”;
echo ucwords($a);
?>
String Functions
The parse_str(string,arr) function parses a query string into variables.
E.g: Output:
<?php 23
parse_str("id=23&name=Kai Jim"); Kai Jim
echo $id."<br />";
echo $name;
?>
String Functions
The str_replace(find,replace,string,count ) function replaces some
characters with some other characters in a string.
Note: str_ireplace() – for case insensitive
Output:
E.g:
Array
<?php (
$arr = array("blue","red","green","yellow"); [0] => blue
print_r(str_ireplace("RED","pink",$arr,$i)); [1] => pink
echo "Replacements: $i"; [2] => green
?> [3] => yellow
)
Replacements: 1
String Functions
The str_pad(string,length,padchar,padtype) function pads a string to a
new length.
E.g:
Output:
<?php .........Hello World
$str = "Hello World";
echo str_pad($str,20,".",STR_PAD_LEFT);
?>
String Functions
The str_split(string,length ) function splits a string into an array. Default length is 1.
E.g:
Output:
<?php Array
print_r(str_split("Hello",3)); (
?> [0] => Hel
[1] => lo
)
String Functions
The str_word_count(string) function counts the number of words in a string.
E.g:
Output:
<?php 2
echo str_word_count("Hello world!");
?>
String Functions
The strcasecmp(string1,string2) function compares two strings as case insensitive.
strcmp(string1,string2) compares as case sensitive.
This function returns:
0 - if the two strings are equal
< 0 - if string1 is less than string2
> 0 - if string1 is greater than string2
E.g:
Output:
<?php
0
echo strcasecmp("Hello world!","HELLO WORLD!");
1
echo “<br>”;
echo strcmp("Hello world!","HELLO WORLD!");
?>
String Functions
The stripos(string,find,start) function returns the position of the first
occurrence of a string inside another string. strpos(string,find,start) – case sensitive.
If the string is not found, this function returns FALSE.
E.g:
Output:
<?php 6
echo stripos("Hello world!","WO");
?>
String Functions
The stristr(string,exp) function searches for the first occurrence of a string inside
another string.
This function returns the rest of the string (from the matching point), or FALSE, if the string to
search for is not found
E.g:
Output:
<?php World! Have a nice day
echo stristr("Hello world! Have a nice day","WORLD");
?>
String Functions
The stripslashes() function removes backslashes added by
the addslashes() function.
E.g:
Output:
<?php
Who’s Peter Griffin
echo strisplashes(“who \’s Peter Griffin");
?>
String functions
Function Description
chop() Removes whitespace or other characters from the right end of a string
count_chars() Returns information about characters used in a string
hex2bin() Converts a string of hexadecimal values to ASCII characters
implode() Returns a string from the elements of an array
join() Alias of implode()
ltrim() Removes whitespace or other characters from the left side of a string
parse_str() Parses a query string into variables
String functions
Function Description
rtrim() Removes whitespace or other characters from the right side of a string
similar_text() Calculates the similarity between two strings
str_ireplace() Replaces some characters in a string (case-insensitive)
str_pad() Pads a string to a new length
str_repeat() Repeats a string a specified number of times
str_replace() Replaces some characters in a string (case-sensitive)
str_shuffle() Randomly shuffles all characters in a string
String functions
Function Description
str_split() Splits a string into an array
str_word_count() Count the number of words in a string
strcasecmp() Compares two strings (case-insensitive)
strchr() Finds the first occurrence of a string inside another string (alias of strstr())
strcmp() Compares two strings (case-sensitive)
strlen() Returns the length of a string
strncmp() String comparison of the first n characters (case-sensitive)
String functions
Function Description
strpos() Returns the position of the first occurrence of a string inside another string (case-
sensitive)
strrchr() Finds the last occurrence of a string inside another string
strrev() Reverses a string
strripos() Finds the position of the last occurrence of a string inside another string (case-
insensitive)
strrpos() Finds the position of the last occurrence of a string inside another string (case-
sensitive)
strstr() Finds the first occurrence of a string inside another string (case-sensitive)
strtolower() Converts a string to lowercase letters
strtoupper() Converts a string to uppercase letters
String functions
Function Description
substr() Returns a part of a string
substr_count() Counts the number of times a substring occurs in a string
substr_replace() Replaces a part of a string with another string
trim() Removes whitespace or other characters from both sides of a string
strlen("Hello world!") 12
strpos("Hello world!","world") 6
String Functions
str_shuffle("Hello World") loerllWdoH
str_replace("world","Peter","Hello world!")Hello Peter!
str_split("Hello")a string into an array
str_split("Hello",3)Array ( [0] => Hel [1] => lo )
str_word_count("Hello world!")Count the number of words
strcmp("Hello world!","Hello world!")case-sensitive comparison
strcasecmp("Hello","HELLO") case-insensitive comparison
strncmp("Hello world!","Hello earth!",6) first n chars (case-sensitive)
strpos("I love php, I love php too!","php")7 pos of first occurrence
substr("Hello world",6) world
substr("Hello world",1,8) ello wor
strtolower(), strtoupper()
substr_count("Hello world. The world is nice","world")2