UNIT - II Chapter 3 - Function
UNIT - II Chapter 3 - Function
Example:
<?php
?>
<?php
?>
Returning Values from Functions:
Functions can also return values to the part of program from where it is called. The return keyword is used to return
value back to the part of program, from where it was called. The returning value may be of any type including the
arrays and objects. The return statement also marks the end of the function and stops the execution after that and
returns the value.
Example:
<?php
?>
PHP allows us two ways in which an argument can be passed into a function:
Pass by Value: On passing arguments using pass by value, the value of the argument gets changed within a
function, but the original value outside the function remains unchanged. That means a duplicate of the original value
is passed as an argument.
Pass by Reference: On passing arguments as pass by reference, the original value is passed. Therefore, the original
value gets altered. In pass by reference we actually pass the address of the value, where it is stored using ampersand
sign(&).
Example:
<?php
// pass by value
function valGeek($num) {
$num += 2;
return $num;
}
// pass by reference
function refGeek(&$num) {
$num += 10;
return $num;
}
$n = 10;
valGeek($n);
echo "The original value is still $n \n";
refGeek($n);
echo "The original value changes to $n";
?>
3.4 Recursive function:
In simple terms, recursion occurs when a function calls itself. As you’d imagine, such a process would repeat
indefinitely if not stopped, so the recursion needs to have some sort of end condition — much like a loop. This end
condition is known as the base case, and the part of the function that calls itself is known as the recursive case. Here’
s a quick overview of how a recursive function operates:
The recursive function is called by the calling code
If the base case, or end condition, is met, the function does any processing required, then exits
Otherwise, the function does any processing required, then calls itself to continue the recursion
Example:
<?php
function display($number) {
if($number<=5){
echo "$number <br/>";
display($number+1);
}
}
display(1);
?>
<?php
// PHP code to get the Fibonacci series
// Recursive function for fibonacci series.
function Fibonacci($number){
// Driver Code
$number = 10;
for ($counter = 0; $counter < $number; $counter++){
echo Fibonacci($counter),' ';
}
?>
Output:
0 1 1 2 3 5 8 13 21 34
Using the iterative way:
At first, we initialize the first and second number to 0 and 1. Following this, we print the first and second number.
Then we send the flow to the iterative while loop where we get the next number by adding the previous two number
and simultaneously we swap the first number with the second and the second with the third.
<?php
// PHP code to get the Fibonacci series
function Fibonacci($n){
$num1 = 0;
$num2 = 1;
$counter = 0;
while ($counter < $n){
echo ' '.$num1;
$num3 = $num2 + $num1;
$num1 = $num2;
$num2 = $num3;
$counter = $counter + 1;
}
}
// Driver Code
$n = 10;
Fibonacci($n);
?>
Output:
0 1 1 2 3 5 8 13 21 34
strlen($str)
This function returns the length of the string or the number of characters in the string including
whitespaces.
<?php
$str = "Welcome to Department of Information Technology ";
// using strlen in echo method
echo "Length of the string is: ". strlen($str);
?>
str_word_count($str)
This function returns the number of words in the string. This function comes in handly in form field
validation for some simple validations.
<?php
$str = "Welcome to Department of Information Technology";
// using str_word_count in echo method
echo "Number of words in the string are: ". str_word_count($str);
?>
strrev($str)
This function is used to reverse a string.
Let's take an example and see,
<?php
$str = "Welcome to Department of Information Technology ";
// using strrev in echo method
echo "Reverse: ". strrev($str);
?>
strpos($str, $text)
This function is used to find the position of any text/word in a given string. Just like an array, string also
assign index value to the characters stored in it, starting from zero.
<?php
?>
This function is used to replace a part of the string with some text. While using this function, the first
argument is the part of string that you want to replace, second argument is the new text you want to
include, and the last argument is the string variable itself.
<?php
echo $str;
?>
ucwords($str)
This function is used for formatting the string. This function converts first letter/character of every word
in the string to uppercase.
<?php
echo ucwords($str);
?>
strtoupper($str)
To convert every letter/character of every word of the string to uppercase, one can use strtoupper()
method.
<?php
echo strtoupper($str);
?>
strtolower($str)
<?php
echo strtolower($str);
?>
str_repeat($str, $counter)
This function is used to repeat a string a given number of times. The first argument is the string and the
second argument is the number of times the string should be repeated.
<?php
$str = "INFORMATION";
?>
strcmp($str1, $str2)
This function is used to compare two strings. The comparison is done alphabetically. If the first string is
greater than second string, the result will be greater than 0, if the first string is equal to the second
string, the result will be equal to 0 and if the second string is greater than the first string, then the result
will be less than 0.
<?php
$str1 = "rsml";
$str2 = "rsml.com";
?>
This function is used to take out a part of the string(substring), starting from a particular position, of a
particular length.
The first argument is the string itself, second argument is the starting index of the substring to be
extracted and the third argument is the length of the substring to be extracted.
<?php
?>
trim($str, charlist)
This function is used to remove extra whitespaces from beginning and the end of a string. The second
argument charlist is optional. We can provide a list of character, just like a string, as the second
argument, to trim/remove those characters from the main string.
<?php
echo trim($str2,"Heo");
?>
nl2br($str)
This function is used to change line break or \n to the HTML tag for line break, which is <br>.
This function is very useful to format string data to display on HTML pages because when a multiline
form data is submitted, it has \n included in the strnig for line breaks, but when you display it on your
HTML page, the line breaks will not get rendered because HTML doesn't understand \n.
<?php
?>