0% found this document useful (0 votes)
23 views30 pages

Unit 3 PHP

Uploaded by

nascdrsridevis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views30 pages

Unit 3 PHP

Uploaded by

nascdrsridevis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

UNIT - 3

STRING FUNCTIONS
STRINGS IN PHP

● A string is considered a data type, which in general is a


sequence of multiple characters that can contain
whitespaces, numbers, characters, and special symbols.
● For example, “Hello World!”, “ID-34#90” etc.
● PHP also allows single quotes(‘ ‘) for defining a string.
● Every programming language provides some in-built
functions for the manipulation of strings.
BASICS IN STRING FUNCTIONS

strlen() Function
It returns the length of the string i.e. the count of all the characters in the string including
whitespace characters.

Syntax
strlen(string or variable name);

Example:

<?php
$str = "Hello World!";
// Prints 12 as output
echo strlen($str);

?>
strrev() Function

It returns the reversed string of the given string.

Syntax

strrev(string or variable name)

Example:

<?php
$str = "Hello World!";
echo strrev($str);
?>
trim(), ltrim(), rtrim(), and chop() Functions
It removes white spaces or other characters from the string. They have two parameters:
one string and another charList, which is a list of characters that need to be omitted.
trim(): Removes characters or whitespaces from both sides.
rtrim() & chop(): Removes characters or whitespaces from the right side.
ltrim(): Removes characters or whitespaces from the left side.
Syntax:
rtrim(string, charList)
ltrim(string, charList)
trim(string, charList)
chop(string, charList)
<?php
$str = "\nThis is an example for string functions.\n";
// Prints original string
echo $str;
// Removes whitespaces from right end
echo chop($str) . "<br>";
// Removes whitespaces from both ends
echo trim($str) . "<br>";
// Removes whitespaces from right end
echo rtrim($str) . "<br>”;
// Removes whitespaces from left end
echo ltrim($str);
?>
strtoupper() and strtolower() Function

It returns the string after changing the cases of its letters.


strtoupper(): It returns the string after converting all the letters to uppercase.
strtolower(): It returns the string after converting all the letters to lowercase.

Syntax
strtoupper(string)
strtolower(string)
Example:
<?php>
$str = "GeeksForGeeks";
echo strtoupper($str)."<br>";
echo strtolower($str);
?>
str_split() Function
It returns an array containing parts of the string.
Syntax
str_split(string, length);
Parameters
string: It specifies the string to be checked, it can also be a variable name of type string.
length: It specifies the length of each part of the string to be stored in the string, by default, it is
If the length is larger than the size of the string, then the complete string is returned.
<?php
$str = "GeeksForGeeks";
print_r(str_split($str));
echo "<br>";
print_r(str_split($str, 3));
?>
output:
Array (
[0] => G
[1] => e
[2] => e
[3] => k
[4] => s
[5] => F
[6] => o
[7] => r
[8] => G
[9] => e
[10] => e
[11] => k
[12] => s
)
Array (
[0] => Gee
[1] => ksF
[2] => orG
[3] => eek
[4] => s
)
str_word_count() Function
It is used to return information about words used in a string like the total number of words in the string, positions of
the words in the string, etc.

Syntax
str_word_count ( $string , $returnVal, $chars)
$string: This parameter specifies the string whose words the user intends to count. This is not an optional parameter.
$returnVal:The return value of str_word_count() function is specified by the $returnVal parameter. It is an optional
parameter and its default value is 0. The parameter can take the below values as required:

$chars: This is an optional parameter that specifies a list of additional characters which shall be considered as a
‘word’.
Example:

<?php
$mystring = "Geeks for Geeks is fun";
print_r(str_word_count($mystring));
?>
Output:
5
strpos() Function

This function helps us to find the position of the first occurrence of a string in another string.
Syntax
strpos(original_str, search_str, start_pos);
Parameters
Out of the three parameters specified in the syntax, two are mandatory and one is optional.

The three parameters are described below:

original_str: This is a mandatory parameter that refers to the original string in which we need to search for the
occurrence of the required string.
search_str: This is a mandatory parameter that refers to the string that we need to search.
start_pos: This is an optional parameter that refers to the position of the string from where the search must begin.
<?php
// PHP code to search for a specific string's position
// first occurrence using strpos() case-sensitive function
function Search($search, $string)
{
$position = strpos($string, $search, 5);
if (is_numeric($position))
{
return "Found at position: " . $position;
}
else
{
return "Not Found";
}
}
// Driver Code
$string = "Welcome to GeeksforGeeks";
$search = "Geeks";
echo Search($search, $string);
?>
str_replace() Function

It is used to replace all the occurrences of the search string or array of search strings by replacement
string or array of replacement strings in the given string or array respectively.

Syntax

str_replace ( $searchVal, $replaceVal, $subjectVal, $count );


Example
<?php>
$subjectVal = "Computer Science in GeeksforGeeks is fun";
$resStr = str_replace('Science', 'algorithms', $subjectVal)
$subjectVal = "Computer Science in GeeksforGeeks is fun";
$resStr = str_replace('Science', 'algorithms', $subjectVal);
print_r($resStr);
?>
Output:
Computer algorithms in GeeksforGeeks is fun
ucwords() Function

It is used to convert the first character of every word in a string to upper-case.

Syntax
string ucwords ( $string, $separator )

Example:
<?php
// Original string
$str = "Geeks for geeks is fun";
// String after converting first character of every word to uppercase
$resStr = ucwords($str);
print_r($resStr);
?>
Output:
Geeks For Geeks Is Fun
Numeric Functions
Numeric functions in PHP are the functions that
return numeric results.

Numeric php function can be used to format


numbers, return constants, perform mathematical
computations etc.
is_number:
Accepts an argument and returns true if its numeric
and false if it's not.

<?php
if(is_numeric("Name"))
{
echo "true";
}
else
{
echo "false";
}
?>
number_format
Used to formats a numeric value using digit
separators and decimal points

<?php
echo number_format(2509663);
?>

2,509,663
rand:
Used to generate a random number.

<?php
echo(rand() . "<br>");
echo(rand() . "<br>");
echo(rand(10,100));
?>
381257253
1233969698
89
round:
Round off a number with decimal points
to the nearest whole number.

<?php
echo round(3.49);
?>
3
sqrt:
Returns the square root of a number
<?php
echo sqrt(100);
?>
10
cos:
Returns the cosine

<?php
echo cos(45);
?>

0.52532198881773
sin:
Returns the sine
<?php
echo sin(45);
?>

0.85090352453412
Tan
Returns the tangent

<?
php echo tan(45);
?>
1.6197751905439
Pi:
Constant that returns the value of PI

<?
php echo pi();
?>
3.1415926535898

You might also like