Functions are the real power of PHP language. There are more than 1000 built-in functions in PHP language. And you can create your own functions as you require. Below are most of the essential PHP functions that every developer should know. These are the functions that are used many times during development.
What is meant by functions in PHP | Definition of function in PHP?
A function is a block (combination) of statements (code) that can be used repeatedly in a program. We write a function one time and call that function many times whatever is required.
There are more important String Functions that are mostly used by developers during development.
Why do we use functions in PHP?
- Make code reusable, repeated processes are done as a function to avoid rewriting code each time.
- Make code more readable and easy to debug and test.
- We use functions to save repetitive functionality needed throughout the program.
Read more from Quora about why functions in PHP are used.
List of Inbuilt Functions in PHP You Should Know as a Developer
#1. substr
substr function is used to extract a substring from a string. This means separating some part of string from the main string.
Extracts a part of a string starting from a specified position and with a specified length.
Syantax: substr(string, start_position, length)
<?php
echo substr("Quiz Lancer",0,10); // Result will be "Quiz Lance". Here 0 is start_position and 10 is a lenght. "Q" is at 0th position, "u" is in 1st position, so on. Result from 0th position to 10 length from start position. So result is "Quiz Lance". Blank space also considers as an index. There is a space between z and L, so it counts as an index. See next examples for more understanding.
echo substr("Quiz Lancer",1,7); // Result will be "uiz Lan".
echo substr("Quiz Lancer",0,5); // Result will be "Quiz ".
echo substr("Quiz Lancer",6,6); // Result will be "ancer".
?>
How to get first 5 characters of string in PHP?
echo substr(“QuizLancer”,0,5); // Result will be “QuizL”. By using the substr function, we can return some characters from a string. Here 0 is the starting position of the string and 5 is the length which means how many characters of the string we want to separate.
#2. str_replace
str_replace function replaces some characters with another character in a string.
Syntax
str_replace (find, replace, string, count)
Example
<?php
$string = "Quiz Lancer is an educational web site for developers who want to become web developer.";
echo str_replace("web","Backend, Frontend",$string);
?>
Result:
Quiz Lancer is an educational Backend, Frontend site for developers who want to become Backend, Frontend developer.
In this example, Backend, Frontend string is replaced with web word. Web word found 2 times in a string, so the Backend, Frontend word replaced 2 times.
#3. subpos
The strpos()
function is a built-in string function in PHP. It is used to find the position of the first occurrence of a substring in a given string. Means that find one string into another string.
The syntax for strpos()
function is as follows:
strpos(strpos(string,find,start))
- string: String for search
- find: String for find
- start: Specifies where to begin the search
Example:
<?php
echo strpos("I LOVE QUIZ, I LOVE QUIZ TOO!","QUIZ");
?>
Result: 7
“QUIZ” word started at position 7th, so the result is 7. Why 7, because I is in the 0th position, blank space is 1st, L is in 2nd, O is 3rd, V is 4th, E at 5th, blank space is in 6th and Q is in the 7th position. In this position string was found, so the result is 7.
Solve the below Quiz for understanding String concepts in PHP which is most important for Full Stack Developers.
Top 5 PHP String Functions You Should Know as a Developer
#4. strtotime
The strtotime() function is a built-in function in PHP that converts date or time-related strings into real date/time format. This function can parse virtually any datetime-related English text to a Unix timestamp. The Unix timestamp means the number of seconds.
Syntax
strtotime(time, now);
Example
<?php
echo(strtotime("now") . "<br>");
echo(strtotime("13 April 2023") . "<br>");
?>
Result
1681394826
1681344000
How to change date format using strtotime in PHP?
$todaysdate = "13-04-2023";
$infullmonthformat = date("d-F-Y",strtotime($todaysdate));
echo $infullmonthformat; // Result will be 13-April-2023
How to convert one date format into another in PHP?
$todaysdate = date('l, F d y h:i:s'); // Returns Thursday, April 13 23 02:24:33
$infullmonthformat = date("d-F-Y H:i:s",strtotime($todaysdate));
echo $infullmonthformat; // Returns 13-April-2023 02:24:33
#5. isset() function in PHP?
isset() function is used to check if a variable is set or not. The syntax of the isset() function is as follows:
$name = "QuizLancer";
if (isset($name))
{
echo "The variable is set.";
}
else
{
echo "The variable is not set.";
}
Result: The variable is set.
The result is variable is set, because a variable name, value is set. $name = “QuizLancer”;
What is the difference between isset and empty in PHP?
The isset() function is an inbuilt function in PHP that is used to determine if the variable is declared and its value is not equal to NULL.
The empty() function is an inbuilt function in PHP that is used to check whether a variable is empty or not.
#6. count() function in PHP
count function returns the number of elements in an array or the number of properties in an object.
$array = [1, 2, 3, 4, 5, 6];
$count = count($array);
echo $count; // Output: 6
Output is 6 because there are 6 elements in an array.
Important Questions for Interview on String
#7. array_push()
The array_push() function adds one or more elements to the end of an array.
Let’s see example of array_push function.
$stack = ["Maharashtra", "Gujarat"]; // Output: Array ([0] => Maharashtra[1] => Gujarat[2])
array_push ($stack, "Karnataka", "Madhya Pradesh");
print_r($stack); // Output: Array ([0] => Maharashtra[1] => Gujarat [2] => Karnataka [3] => Madhya Pradesh)
In the above example, we pushed Karnataka and Madhya Pradesh to the array, that’s why after pushing to the array there are 4 elements in an array.
#8. in_array() Function in PHP
The in_array() function is a built-in PHP function that allows us to check whether a given value exists within a given array.
It is specifically useful when we want to determine if a specific element or value is present in an array. It returns a Boolean value (either true or false) to indicate whether the element or value is found or not.
Syntax:
in_array(needle, haystack, strict)
In above Syntax:
- needle: The value we want to search for within the array.
- haystack: The array in which we want to search for the value.
- strict (optional): A boolean parameter that, if set to true, performs a strict type check as well.
What will be the Result?
- If the value (needle) is found in the array (haystack), the function returns true. Means result will be true.
- If the value is not found in the array, it returns false. Means result will be false.
Let’s see one example
$words= ["quiz", "lancer", "website"];
$result = in_array ("lancer", $words);
In this above example, the in_array() function is used to check if the value “lancer” exists in the $words array. $words is the name of an array, our all values are stored in an array. Since “lancer” is present in the array, the variable $result will be assigned the value true. This means the result will be true because “lancer” value is present in an array.
Please be attentive here, if we search like as below
$words= ["quiz", "lancer", "website"];
$result = in_array ("development", $words);
Now the result will be false, because we are searching “development” value in an $words array. And “development” value is not present in an $words array. Hope you are getting fine now. Right? Follow Telegram Channel for daily such type of knowledge.
Solve Quiz of functions in php
Follow Quiz Lancer Telegram Famous Channel for solving quizzes daily, which are helpful to you to increase your knowledge.
Solve the below Quiz and check your knowledge of PHP. These quizzes will help you improve your PHP knowledge. Solving quizzes will increase your confidence for the interview. You can crack the interview and also get a job.
You can read about functions from w3school website.
FAQ on Functions in PHP
Can a function return a value of any type?
Yes, a function in PHP can return a value of any type, including scalar types (such as integer, float, string, boolean), arrays, objects, and even null.
Which function is used to convert a string in PHP?
str_split() Function
This is an in-built PHP method that is used to convert a string into an array by breaking the string into smaller sub-strings of uniform length and storing them in an array.