PHP function

PHP function

PHP function introduction

PHP has around hundreds of inbuilt for functions which makes it a very powerful language. A function is a group of statements that can be executed any time you want. ALL functions in PHP start with function(). These functions can also accept parameters. They can also be used to return values.

Example 1
Function sample ()
       Echo “Sample function”;

Example 2
Function add($x,$y)
       $total = $x + $y;
       return $total;

What is the functionality of the function htmlentities?

The htmlentities() function converts characters to HTML entities.


Here, string is the required string to be converted. Quotestyle is an optional parameter which specifies how to encode single and double quotes. The following styles are available:

- ENT_COMPAT - Default. Encodes only double quotes
- ENT_QUOTES - Encodes double and single quotes
- ENT_NOQUOTES - Does not encode any quotes

character-set is an optional parameter which specifies A string to determine character-set to use. ISO, UTF are example of such character sets.

Example: This code will encode the double quotes
$str= “Jack & ‘jill’”
echo htmlentities($str, ENT_COMPAT);

Jack & ‘jill’

Explain the difference between ereg_replace() and eregi_replace().

Ereg_replace() is used for matching patters to the input string. It then replaces the matched string.

string ereg_replace ( string $pattern , string $replacement , string $string )

Eregi_replace() is used for matching patters to the input string. It then replaces the matched string. The difference is that this function is case sensitive.

What functions would you use to redirect the browser to a new page?

a. Header function in PHP redirects to a new URL


b. http_redirect() is also used to redirect to a new page or URL.

void http_redirect ( [string url [, array params [, bool session = FALSE [, int status]]]] )

Here, the URL is the path of the new page. params can be some query parameters followed by whether the session information needs to be passed and the custom response status code.

What is the functionality of md5 function in PHP?

MD5 function calculates the md5 hash of a string. It is basically used for encryption. It is also used for digital signature applications, where a large file must be "compressed" in a secure manner.


List out different arguments in php header function?

Different arguments in php header function:-

The header() function sends a raw HTTP header to a client.


String: A required parameter specifying the header string to send.

Replace: Optional parameter indicating whether the previous header needs to be replaced (TRUE) or not (FALSE)

http_response_code: Optional parameter forcing the HTTP response code to the specified value.

Explain the purpose of isset() functions by illustrating an example.

Isset() function is used to determine if a variable is set or not. It returns true if the variable exists. If multiple parameters are supplied then isset() will return TRUE only if all of the parameters are set.

&a =”sample”;
Var_dump(isset($a)); //true;

Explain readfile() function with an example.

Readfile(): The function is used to read files and write it to an output buffer.

       Echo readfile(“sample.txt”);

The function returns the size of the file, i.e. the number of bytes. It returns a FALSE on failure.

Explain fopen() and fread() functions with an example.

1. Fopen(): fopen() opens a file or URL.
Example: Opens the file in read mode
$file = fopen(“sample.txt”,”r”);

2. Fread(): This function is to read contents from an open file. The function stops when it reaches the end of the file or the specified length.
Example: Reading 10 bytes

Explain reading by line using fgets() function with an example.

Fgets(): This function returns a line from an open file. It stops to return the line when it reaches the end of file or the specified bytes.

Echo fgets(“sample.txt”);

Explain creating and changing files using file_put_contents() and fwrite().

file_put_contents(): This function writes a string to a file. There are some rules the function follows.

i. If FILE_USE_INCLUDE_PATH is set, check the include path for a copy of *filename*
ii. It creates the file if it does not exist
iii. Opens the file
iv. Lock the file if LOCK_EX is set
v. If FILE_APPEND is set, it moves to end of file.
vi. Write the data on the file
vii. Close the file and release any locks\

This function returns the number of character written into the file on success or FALSE on failure.

Echo file_put_contents(|”sample.txt”,”test”);

Output: 4

Explain the rename() function for renaming files with an example.

Rename(): The function renames a file or a directory. It returns true on a success.

Example: sample is renamed by test.

Write short note on copying files with copy() functions with an example

Copy(): The function copies a file. It returns true on success.

Copy(file, to file);

Echo Copy (“source.txt”,”dest.txt”);


If the destination file already exists, it will be overwritten.

Write short note on deleting files with unlink() with an example.

Unlink(): This function is used to delete files. It returns true on success.

Unlink (filename, context);

Here, context is a set of options that can modify the behaviour of streams.

     $file = "sample.txt";
if (!unlink($file))
      echo ("Error deleting $file");
      echo ("Deleted $file");

Explain locking files with flock() function with an example.

Flock() : This function can be used to lock and release a lock.

Flock(file, lock, block);

LOCK here specifies the type of lock.

- LOCK_SH- shared lock
- LOCK_EX – Exclusive lock
- LOCK_UN- Realease a shared or exclusive lock
- LOCK_NB- avoids clocking other processes
   Block- optional


Explain the changing file permission and ownership using PHP's chmod() function.

Chmod() : This function is used for changing permissions on a file.

Chmod(file, mode)

Mode here specifies the permissions as follows:

- The first number is always zero
- The second number specifies permissions for the owner
- The third number specifies permissions for the owner's user group
- The fourth number specifies permissions for everybody else

Possible values (to set multiple permissions, add up the following numbers)

- 1 = execute permissions
- 2 = write permissions
- 4 = read permissions

// everything for owner, read for owner's group
PHP data types
PHP supports a number of fundamental basic data types, such as integers, floats, and strings.
PHP automatics type conversion
Automatic type conversion is essential when two differently typed variables are combined in an expression
PHP arrays
Arrays are used for storing one or more values in a single variable name.
Post your comment