Functions and types of functions in PHP

In PHP, function is a collection of logically related statements that are used to perform specific task. In PHP, there are thousands of built-in functions to perform various task.

In this, we will go through:-

  • Built-in Functions
  • String Functions
  • Numeric Functions
  • Date Functions
  • User-Defined Functions

Function Uses

  • Function is useful when it is required to perform some specific task repeatedly.
  • There is no need to write block of code every time. So it will reduce the size of the script.
  • There is no repetition of same code again and again, so it is easy to understand and solve the error.

Built-in Functions

  • These functions comes with PHP installation package.
  • PHP has thousands of built-in functions which can be called directly to perform various task.

String Functions

  • These functions allow you to manipulate string in various ways.
FunctionDescriptionExampleOutputMore Infor
strtolower()Convert all the character of string to lowercaseecho strtolower(“ ROSY ”);rosy
strtoupper()Convert all the character of string to uppercaseecho strtoupper(“ the taj mahal ”);THE TAJ MAHAL
substr()Returns specific part of string. There are 3 parameters. First parameter represent string which you want to retrieve specific part of string. Second parameter specify starting position from which you want to retrieve. Third parameter indicate number of character to retrieveecho substr(“ welcome ”, 3, 4);come
strlen()It return integer value indicating length of string. Length of string means total number of character in the string.echo strlen(“ welcome ”);7
strpos()This function accept two string as an argument & return position of first occurrence of search string inside the another string.echo strpos(“ welcome ”, “e”, 2);6
strstr()This function accept two string as an argument and search for the first occurrence of first string inside original string. If search string is found it return rest of the string from matching position.echo strstr(“ welcome ”, “co” );come
str_replace()Replace specific character of string with another specific characters. It accepts 3 arguments. First argument indicate character to be find in the original string. Second argument indicate character to be replaced. Third argument indicate original string for which replacement is performed.echo str_replace(“ e ”, “ $ ”, “ welcome ”);w$lcom$
str_rev()This function reverse the string.echo str_rev(“ welcome ”);emoclew
str_word_count()It count the number of word in the string.echo str_word_count (“ welcome ”);1
explodeconverts string into arrayecho explode(‘ ‘,’the code tutorial’);array(‘the’,’code’,’tutorial’)Read More
implodeconverts array into stringecho implode(‘ ‘,array(‘the’,’code’,’tutorial’));the code tutorialRead More
strposfind first occurrence of string in source stringecho strpos(‘We are at thecodetutorial’, ‘thecodetutorial’);10Read More

Numeric Functions

  • These functions allows you to perform various operation on numeric values.
FunctionDescriptionExampleOutput
ceil()Accepts number as an argument & return number which is round at upward to the nearest value.echo ceil(3.7);4
floor()Return number which is round at downward to the nearest value.echo floor(3.7);3
round()Return number roundest to the nearest integer.echo round(7.4);7
fmod()Accept two numbers as an argument and divide first number by second number and return reminder of divisionecho fmod(10, 3);1
pow()Accept two numbers as an argument and raise number one to the power of number two.echo pow(2, 3);8
sqrt()Accept number as an argument and return square root of number. If number is negative then it returns NAN.echo sqrt( 16 );4
rand()Used to generate random integer between range 0 to RAND_MAX where RAND_MAX has value 32768.echo rand();4815 //random number
cos()Return the cosine number.echo cos(60);0.5
sin()Return the sine number.echo sin(60);0.8660254037
tan()Return the tangent number.echo tan(60);1.7320508075