Professional Documents
Culture Documents
PHP Notes - PHP - in - Built - STRING Functions
PHP Notes - PHP - in - Built - STRING Functions
1. Chr():-
Definition and Usage
The chr() function returns a character from the specified ASCII value.
Syntax
chr(ascii)
Parameter Description
Note: The x parameter can be specified in decimal, octal, or hex values. Octal values are
defined by a leading 0, while hex values are defined by a leading 0x.
Example
<?php
echo chr(52)."<br />";
echo chr(052)."<br />";
echo chr(0x52)."<br />";
?>
4
*
R
2. Ord():-
Definition and Usage
The ord() function returns the ASCII value of the first character of a string.
Syntax
ord(string)
Parameter Description
Example
<?php
echo ord("h")."<br />";
echo ord("hello")."<br />";
?>
104
104
3. strtolower()
Definition and Usage
Convert the string in lowercase
Syntax
strtoupper($string)
Parameters
$string (required): This is a mandatory parameter of this function which specifies a string to convert
into uppercase. In simple words - it is a string which is converted into uppercase.
<?php
echo strtolower("vaishnavi.");
?>
Output:-
VAISHNAVI
4. strtoupper()
Definition and Usage
Convert the string in lowercase
Syntax
strtoupper($string)
Parameters
$string (required): This is a mandatory parameter of this function which specifies a string to convert
into uppercase. In simple words - it is a string which is converted into uppercase.
<?php
//original string
$input_str = "Hey, Vaishnavi here";
echo "<b>String before conversion: </b>". $input_str;
echo "</br>";
Output:
5. ltrim()
Definition and Usage
The ltrim() function will remove whitespaces or other predefined character from the left side
of a string.
Syntax
ltrim(string,charlist)
Parameter Description
Charlist Optional. Specifies which characters to remove from the string. If omitted, all of the
following characters are removed:
• "\0" - NULL
• "\t" - tab
• "\n" - new line
• "\x0B" - vertical tab
• "\r" - carriage return
• " " - ordinary white space
Example 1
<html>
<body>
<?php
$str = " Hello World!";
echo "Without ltrim: " . $str;
echo "<br />";
echo "With ltrim: " . ltrim($str);
?>
<body>
<html>
Example 2
<?php
$str = "\r\nHello World!";
echo "Without ltrim: " . $str;
echo "<br />";
echo "With ltrim: " . ltrim($str,’\n’);
?>
6. rtrim()
Definition and Usage
The rtrim() function will remove whitespaces or other predefined character from the right
side of a string.
Syntax
rtrim(string,charlist)
Parameter Description
charlist Optional. Specifies which characters to remove from the string. If omitted, all of the
following characters are removed:
• "\0" - NULL
• "\t" - tab
• "\n" - new line
• "\x0B" - vertical tab
• "\r" - carriage return
• " " - ordinary white space
Example 1
<html>
<body>
<?php
$str = "Hello World! ";
echo "Without rtrim: " . $str;
echo "<br />";
echo "With rtrim: " . rtrim($str);
?>
<body>
<html>
Example 2
<?php
$str = "Hello World!\r\n";
echo "Without rtrim: " . $str;
echo "<br />";
echo "With rtrim: " . rtrim($str,”\n”);
?>
7. substr()
Definition and Usage
Syntax
substr(string,start,length)
Parameter Description
length Optional. Specifies the length of the returned string. Default is to the end of the string.
Note: If start is a negative number and length is less than or equal to start, length becomes 0.
Example 1
<?php
echo substr("Hello world!",6);
?>
world!
Example 2
<?php
echo substr("Hello world!",6,2);
?>
wo
8. strcmp()
Definition and Usage
Syntax
strcmp(string1,string2)
Parameter Description
Example
<?php
echo strcmp("Hello world!","Hello world!");
?>
9. strcasecmp()
Definition and Usage
Parameter Description
Example
<?php
echo strcasecmp("Hello world!","HELLO WORLD!");
?>
10. strops()
Definition and Usage
The strpos() function returns the position of the first occurrence of a string inside another
string.
Syntax
strpos(string,find,start)
Parameter Description
Example
<?php
echo strpos("Hello world!","o");
?>
11. strrpos()
Definition and Usage
The strrpos() function finds the position of the last occurrence of a string inside another
string.
Syntax
strrpos(string,find,start)
Parameter Description
Example
<?php
echo strrpos("Hello world!","o");
?>
The output of the code above will be:
12. strstr()
Definition and Usage
The strstr() 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.
Syntax
strstr(string,search)
Parameter Description
search Required. Specifies the string to search for. If this parameter is a number, it will search for
the character matching the ASCII value of the number
Example 1
<?php
echo strstr("Hello world!","world");
?>
world!
13. stristr()
Definition and Usage
The stristr() 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.
Syntax
stristr(string,search)
Parameter Description
search Required. Specifies the string to search for. If this parameter is a number, it will search for
the character matching the ASCII value of the number
Example 1
<?php
echo stristr("Hello world!","WORLD");
?>
world!
Example 2
In this example we will search a string for the ASCII value of "o":
<?php
echo stristr("Hello world!",111);
?>
14. str_replace()
Definition and Usage
The str_replace() function replaces some characters with some other characters in a string.
Syntax
str_replace(find,replace,string,count)
Parameter Description
Example 1
<?php
echo str_replace("world","Peter","Hello world!");
?>
Example 2
In this example we will demonstrate str_replace() with an array and a count variable:
<?php
$arr = array("blue","red","green","yellow");
print_r(str_replace("red","pink",$arr,$i));
echo "Replacements: $i";
?>
Array
(
[0] => blue
[1] => pink
[2] => green
[3] => yellow
)
Replacements: 1
Example 3
In this example we will demonstrate str_replace() with less elements in replace than find:
<?php
$find = array("Hello","world");
$replace = array("B");
$arr = array("Hello","world","!");
print_r(str_replace($find,$replace,$arr));
?>
Array
(
[0] => B
[1] =>
[2] => !
)
15. strrev()
Syntax
strrev(string)
Parameter Description
Example
<?php
echo strrev("Hello World!");
?>
!dlroW olleH
With PHP, there are two basic ways to get output: echo and print.
echo and print are language constructs, and they never behave like a function. Therefore, there is no requirement
for parentheses. However, both the statements can be used with or without parentheses. We can use these
statements to output variables or strings.
16. echo()
The echo statement can be used with or without parentheses: echo or echo().
Example:-
<!DOCTYPE html>
<html>
<body>
<?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>
</body>
</html>
Output:-
PHP is Fun!
Hello world!
I'm about to learn PHP!
This string was made with multiple parameters.
Display Variables
The following example shows how to output text and variables with the echo statement:
Example:-
<html>
<body>
<?php
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4;
</body>
</html>
Output:-
Learn PHP
Study PHP at W3Schools.com
9
17. print()
The print statement can be used with or without parentheses: print or print().
Display Text
The following example shows how to output text with the print command (notice that the text can
contain HTML markup):
<!DOCTYPE html>
<html>
<body>
<?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>
</body>
</html>
Output:-
PHP is Fun!
Hello world!
I'm about to learn PHP!
Display Variables
The following example shows how to output text and variables with
the print statement:
<!DOCTYPE html>
<html>
<body>
<?php
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4;
</body>
</html>
Output:-
Learn PHP
Study PHP at W3Schools.com
9
Difference between echo and print
echo
o echo is a statement, which is used to display the output.
o echo can be used with or without parentheses.
o echo does not return any value.
o We can pass multiple strings separated by comma (,) in echo.
o echo is faster than print statement.
print
o print is also a statement, used as an alternative to echo at many times to display the output.
o print can be used with or without parentheses.
o print always returns an integer value, which is 1.
o Using print, we cannot pass multiple arguments.
o print is slower than echo statement.
https://www.javatpoint.com/php-echo-and-print-statements
Math Function
1. abs()
Definition and Usage
Syntax
abs(x)
Parameter Description
x Required. A number. If the number is of type float, the return type is also float, otherwise
it is integer
Example
<?php
echo(abs(6.7) . "<br />");
echo(abs(-3) . "<br />");
echo(abs(3));
?>
6.7
3
3
2. ceil()
Definition and Usage
The ceil() function returns the value of a number rounded UPWARDS to the nearest integer.
Syntax
ceil(x)
Parameter Description
x Required. A number
Example
In the following example we will use the ceil() function on different numbers:
<?php
echo(ceil(0.60) . "<br />");
echo(ceil(0.40) . "<br />");
echo(ceil(5) . "<br />");
echo(ceil(5.1) . "<br />");
echo(ceil(-5.1) . "<br />");
echo(ceil(-5.9))
?>
1
1
5
6
-5
-5
3. floor()
Definition and Usage
The floor() function returns the value of a number rounded DOWNWARDS to the nearest
integer.
Syntax
floor(x)
Parameter Description
x Required. A number
Example
<?php
echo(floor(0.60) . "<br />");
echo(floor(0.40) . "<br />");
echo(floor(5) . "<br />");
echo(floor(5.1) . "<br />");
echo(floor(-5.1) . "<br />");
echo(floor(-5.9))
?>
0
0
5
5
-6
-6
4. round()
Definition and Usage
Syntax
round(x,prec)
Parameter Description
Example
In this example we will round different numbers with the round() function:
<?php
echo(round(0.60) . "<br />");
echo(round(0.50) . "<br />");
echo(round(0.49) . "<br />");
echo(round(-4.40) . "<br />");
echo(round(-4.60))
?>
1
1
0
-4
-5
5. fmod()
Definition and Usage
The fmod() function divides x by y and returns the remainder (modulo) of the division.
Syntax
fmod(x,y)
Parameter Description
x Required. A number
y Required.
Example
In the following example we will use the fmod() function to return the remainder of 5/2:
<?php
$r = fmod(5,2);
echo $r
?>
6. min()
Definition and Usage
The min() function returns the number with the lowest value of two specified numbers.
Syntax
min(x,y)
Parameter Description
x Required. A number
y Required. A number
Example
In this example we will show how to use min() to return the number with the lowest value of
two specified numbers:
<?php
echo(min(5,7) . "<br />");
echo(min(-3,5) . "<br />");
echo(min(-3,-5) . "<br />");
echo(min(7.25,7.30))
?>
5
-3
-5
7.25
7. max()
Definition and Usage
The max() function returns the number with the highest value of two specified numbers.
Syntax
max(x,y)
Parameter Description
x Required. A number
y Required. A number
Example
In this example we will show how to use max() to return the number with the highest value of
two specified numbers:
<?php
echo(max(5,7) . "<br />");
echo(max(-3,5) . "<br />");
echo(max(-3,-5) . "<br />");
echo(max(7.25,7.30))
?>
8. pow()
Definition and Usage
The pow() function raises the first argument to the power of the second argument, and returns
the result.
Syntax
pow(x,y)
Parameter Description
Example
<?php
echo pow(4,2) . "<br />";
echo pow(6,2) . "<br />";
echo pow(-6,2) . "<br />";
echo pow(-6,-2) . "<br />";
echo pow(-6,5.5);
?>
16
36
36
0.0277777777778
-1.#IND
9. sqrt()
Definition and Usage
Syntax
sqrt(x)
Parameter Description
x Required. A number
Note: The sqrt() function will return -1.#IND if the parameter x is a negative number.
Example
<?php
echo(sqrt(0) . "<br />");
echo(sqrt(1) . "<br />");
echo(sqrt(9) . "<br />");
echo(sqrt(0.64) . "<br />");
echo(sqrt(-9))
?>
0
1
3
0.8
-1.#IND
10. rand()
Definition and Usage
If this function is called without parameters, it returns a random integer between 0 and
RAND_MAX.
If you want a random number between 10 and 100 (inclusive), use rand (10,100).
Syntax
rand(min,max)
Parameter Description
min,max Optional. Specifies the range the random number should lie within
Note: On some platforms (such as Windows) RAND_MAX is only 32768. So, if you require
a range larger than 32768, you can specify min and max, or use the mt_rand() function
instead.
Note: In PHP 4.2.0 and later, there is no need to seed the random generator with srand(). This
is done automatically.
Tip: The mt_rand() function generates a better random value than this function!
Example
<?php
echo(rand() . "<br />");
echo(rand() . "<br />");
echo(rand(10,100))
?>
17757
3794
97