73-PHP-Strings and String function-Arrays-08-10-2024 (1)
73-PHP-Strings and String function-Arrays-08-10-2024 (1)
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
<?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
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
E.g: Output:
Hello World! Beautiful Day!
<?php
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
?>
String Functions
E.g: Output:
<?php Length = 12
$a= “hello World!”;
echo “Length = “, strlen($a);
?>
String Functions
E.g: Output:
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
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
E.g: Output:
<?php 2
$a= “hello Worlod!”;
echo substr_count($a,”lo”);
?>
String Functions
E.g: Output:
<?php heaaorld!
$a= “hello World!”;
echo substr_replace($a,”aa”,2,5);
?>
String Functions
E.g: Output:
<?php Hello world!
$a= “hello world!”; Hello World!
echo ucfirst($a),”<br>”;
echo ucwords($a);
?>
String Functions
E.g: Output:
<?php 23
parse_str("id=23&name=Kai Jim"); Kai Jim
echo $id."<br />";
echo $name;
?>
String Functions
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
E.g:
Output:
<?php .........Hello World
$str = "Hello World";
echo str_pad($str,20,".",STR_PAD_LEFT);
?>
String Functions
E.g:
Output:
<?php Array
print_r(str_split("Hello",3)); (
?> [0] => Hel
[1] => lo
)
String Functions
E.g:
Output:
<?php 2
echo str_word_count("Hello world!");
?>
String Functions
E.g:
Output:
<?php
0
echo strcasecmp("Hello world!","HELLO WORLD!");
1
echo “<br>”;
echo strcmp("Hello world!","HELLO WORLD!");
?>
String Functions
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
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
ltrim() Removes whitespace or other characters from the left side of a string
rtrim() Removes whitespace or other characters from the right side of a string
strchr() Finds the first occurrence of a string inside another string (alias of strstr())
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)