Commonly Used PHP functions for beginner

In this following article, we have some commonly used PHP functions which every beginner PHP developer must know and aware of it. Where it will help you at the time of interview as well as will enhance your coding skills.

These are few but important with a brief usage of each.

phpinfo();

Gives all information about PHP version you are using.

error_reporting(0);   //Parameter can be 0 or 1

To show the PHP errors or hide

die();

This function kills the entire script after its use.

preg_match(‘/example/’);

Search a specific string into a given string

str_word_count($string);

To count the total words into a string

print_r($array_var);

To show the array in the proper format

strlen($string);

To find the string length;

strtolower($string); & strtoupper($string);

To change the case of passed string

Strops(‘this is an example string’, ‘is’, $offset); //$offset can be a positive integer value which indicates to the function that from where we can start the search

Another function to search a specific part of the string from another string.

substr_replace($string, ‘$var’, 29, 4);

The above function will change the value of $string. It replaces the all characters from the node of 29 till next 4 positions with the $var value.

To change a part of string with another value is called “Word Censoring”

str_replace($find, $replace, $userinput);

Another function of word censoring. $find and $replace can be an array.

time() & date();

To show time and date

rand(1,6);

Will generate a random value between 1 to 6

header(‘Location:https://www.harishsharma.net’);

Redirect to specified URL

empty($string);

Return true if empty.

session_start();

To start the session.

unset($_SESSION[‘username’]);

Unset the session variable value. Mainly used while logging out.

setcookie(‘username’, ‘harish’, 5);

To set the Cookie. Last parameters are the time in second.

explode(‘,’, $data);

Create an array from a string. Create the value with the passed separator.

implode(‘ ’,$array_var);

create a string from a variable.

md5($string);

create a string into MD5 Hash code.

mail($to, $subject, $body, $headers);

E-mail function