mail()

It is used for sending email

Syntax mail($to,$subject,stripslashes($body)[,$headers[, '-f{$to}']]);
example:
  	
			<?php
				function sendmail($to,$subject,$body,$from='admin',$cc='',$bcc=''){
						$headers = "";
						if($from){
							$headers .= "From:$from\n";
						}
						$headers  .= "MIME-Version: 1.0\n";
						$headers .= "Content-type: text/html; charset=iso-8859-1\n";
						if($cc){
							$headers .= "Cc:$cc\n";
						}
						if($bcc){
							$headers .= "Bcc:$bcc\n";
						}
						$headers .= "Message-ID: <". time() .rand(1,1000). "@".$_SERVER['SERVER_NAME'].">". "\n";
						ini_set("sendmail_from", $from);
						$msg        =mail($to,$subject,stripslashes($body),$headers, "-f{$to}");
						return $msg;
					}
			?>
		

isset()

It determines whether a certain variable has already been declared by PHP. It returns a boolean value true if the variable has already been set, and false otherwise, or if the variable is set to the value NULL. or Determine if a variable is set and is not NULL.

Syntax bool isset ( mixed $var [, mixed $var [, $... ]] );
example:
  	
			<?php
				$var = '';
				if (isset($var)) {
						echo "This var is set so I will print.";
				}
			?>
		
Output :
This var is set so I will print.

is_null()

Finds whether a variable is NULL.

Syntax bool is_null(mixed $var);
example:
  	
			<?php
				$foo = NULL;
				$inexistent=NULL;
				var_dump(is_null($inexistent), is_null($foo));
			?>
		
Output :
bool(true)
bool(true)

empty()

Determine whether a variable is empty.

Syntax bool empty (mixed $var);
example:
  	
			<?php
				$var = 0;
				if (empty($var)) {
					echo '$var is either 0, empty, or not set at all';
				}
				if (isset($var)) {
					echo '$var is set even though it is empty';
				}
			?>
		
Output :
$var is either 0, empty, or not set at all.
$var is set even though it is empty.

htmlentities()

Whenever you allow your users to submit text to your website, you need to be careful that you don't leave any security holes open for malicious users to exploit. If you are ever going to allow user submitted text to be visible by the public you should consider using the htmlentities function to prevent them from running html code and scripts that may be harmful to your visitors.

Syntax htmlentities(string,quotestyle,character-set);
quotestyle=
* ENT_COMPAT - Default. Encodes only double quotes
* ENT_QUOTES - Encodes double and single quotes
* ENT_NOQUOTES - Does not encode any quotes
example:
  	
<?php
$str = "Jane & 'Tarzan'";
echo htmlentities($str, ENT_COMPAT);
?>
		
Output :
Jane & 'Tarzan'

urlencode()

The characters used in resource names, query strings, and parameters must not conflict with the characters that have special meanings or can't allowed in a URL. For example, a question mark character identifies the beginning of a query, and an ampersand (&) character separates multiple terms in a query. The meanings of these characters can be escaped using a hexadecimal encoding consisting of the percent character (%) followed by the two hexadecimal digits representing the ASCII encoded of the character. For example, an ampersand (&) character is encoded as %26.

Syntax string urlencode ( string $str );
example:
  	
			<?php
				$userinput = urlencode('20?/30');
				print "
".$userinput."
"; echo 'test'; ?>
Output :
20%3F%2F30
test

uniqid()

The uniqid() function generates a unique ID based on the microtime (current time in microseconds).

example:
  	
			<?php
				echo uniqid();
			?>
		
Output :
4415297e3af8c (This value is not fixed,the value will change automatically each time)

md5()

The md5() function is used to calculate the md5 hash (the hash as a 32-character hexadecimal number ) of a string.

Syntax md5(input_string, raw_output)
* input_string(Required) - The input string.
* raw_output(Optional) - Refers hex or binary output format, Returns raw 16 bit binary format if raw_output sets TRUE and return 32 bit hex format for setting FALSE (default).
example:
  	
			<?php
				$input_string = 'Good Morning';  
				echo 'Original string : '.$input_string.'
'; echo '16 bit binary format : '.md5($input_string, TRUE).'
'; echo '32 bit binary format : '.md5($input_string).'
'; ?>
Output :
Original string : Good Morning
16 bit binary format :r y†” du?Û£¿ân
32 bit binary format : 72a079088694099d64753fdba3bfe26e

nl2br()

The nl2br() function inserts HTML line breaks in front of each newline (\n) in a string.

Syntax nl2br(string)
* string(Required) - The input string.
example:
  	
			<?php
				echo nl2br("One line.\nAnother line."); 
			?>
		
Output :
One line.
Another line.

header()

* The header() function sends a raw HTTP header to a client.
* It is important to notice that header() must be called before any actual output is sent.

Syntax header(string,replace,http_response_code)
* string(Required) - Specifies the header string to send.
* replace(Optional) - Indicates whether the header should replace previous or add a second header. Default is TRUE (will replace). FALSE (allows multiple headers of the same type).
* http_response_code(Optional) - Forces the HTTP response code to the specified value.
example:
  	
<?php
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache");
header("Pragma: no-cache");
?>
		

urldecode()

* Decodes URL-encoded string.

Syntax string urldecode ( string $str )
* str(Required) - The string to be decoded.
Note : Decodes any %## encoding in the given string. Plus symbols ('+') are decoded to a space character.
example:
  	
<?php
$str = "$99.99 and 10% tax";
print "Original String : " .$str;
print "
"; $encode = urlencode($str); print "After encoded : " .$encode; print "
"; $decode = urldecode($encode); print "After decoded : " .$decode; ?>
Output :
Original String : $99.99 and 10% tax
After encoded : %2499.99+and+10%25+tax
After decoded : $99.99 and 10% tax

ob_clean()

* Clean (erase) the output buffer.

Syntax void ob_clean ( void )
* This function discards the contents of the output buffer.
* No value is returned.

error_reporting()

* Sets which PHP errors are reported.

Syntax int error_reporting ([ int $level ] )
* The error_reporting() function sets the error_reporting directive at runtime.
* PHP has many levels of errors, using this function sets that level for the duration (runtime) of your script.
* If the optional level is not set, error_reporting() will just return the current error reporting level.
example:
  	
<?php
error_reporting(0);
// Turn off all error reporting
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
// Reporting E_NOTICE can be good too (to report uninitialized.
//variables or catch variable name misspellings ...)
error_reporting(E_ALL ^ E_NOTICE);
//Report all errors except E_NOTICE.
//This is the default value set in php.ini
error_reporting(E_ALL);
// Report all PHP errors (see changelog)
error_reporting(-1);
// Report all PHP errors
ini_set('error_reporting', E_ALL);
// Same as error_reporting(E_ALL);
?>

ini_set()

* Sets the value of a configuration option.

Syntax string ini_set ( string $varname , string $newvalue )
* Sets the value of the given configuration option.
* The configuration option will keep this new value during the script's execution, and will be restored at the script's ending.

preg_match()

The preg_match() function Perform a regular expression match.

Syntax int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )
* pattern(Required) - The pattern to search for, as a string.
* subject(Required) - The input string.
* matches(Required) - If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.
example:
  	
<?php
// get host name from URL
preg_match('@^(?:http://)?([^/]+)@i',"http://www.php.net/index.html", $matches);
$host = $matches[1];
// get last two segments of host name
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
echo "Domain name is: {$matches[0]}";
?>
Output :
Domain name is: php.net

preg_match_all()

Perform a global regular expression match.

Syntax int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ]]] )
Note : Searches subject for all matches to the regular expression given in pattern and puts them in matches in the order specified by flags.
* pattern(Required) - The pattern to search for, as a string.
* subject(Required) - The input string.
* matches(Required) - Array of all matches in multi-dimensional array ordered according to flags.
example:
  	
<?php
preg_match_all("|<[^>]+>(.*)]+>|U","example: 
this is a test
",$out, PREG_PATTERN_ORDER); echo $out[0][0] . ", " . $out[0][1] . "\n"; echo $out[1][0] . ", " . $out[1][1] . "\n"; ?>
Output :
<b>example: </b>, <div align=left>this is a test</div>
example: , this is a test
Note :So, $out[0] contains array of strings that matched full pattern, and $out[1] contains array of strings enclosed by tags.

pathinfo()

* The pathinfo() function returns an array that contains information about a path.
* The following array elements are returned:

Syntax pathinfo(path,options)
* path(Required) - Specifies the path to check.
* options(Optional) - Specifies which array elements to return. Default is all.
Possible values:
      PATHINFO_DIRNAME - return only dirname
      PATHINFO_BASENAME - return only basename
      PATHINFO_EXTENSION - return only extension
example:
  	
<?php
print_r(pathinfo("/testweb/test.txt"));
?>
Output :
Array ( [dirname] => /testweb [basename] => test.txt [extension] => txt [filename] => test )
example 1:
  	
<?php
print_r(pathinfo("/testweb/test.txt",PATHINFO_BASENAME));
?>
Output :
test.txt

parse_str()

* The parse_str() function parses a query string into variables.

Syntax parse_str(string,array)
* string(Required) - Specifies the string to parse.
* array(Optional) - Specifies the name of an array to store the variables. This parameter indicates that the variables will be stored in an array.
Note: If the array parameter is not set, variables set by this function will overwrite existing variables of the same name.
Note: The magic_quotes_gpc setting in the php.ini file affects the output of this function. If enabled, the variables are converted by addslashes() before parsed by parse_str().
example:
  	
<?php
parse_str("id=23&name=Kai%20Jim");
echo $id."
"; echo $name; ?>
Output :
23
Kai Jim
example 1:
  	
<?php
parse_str("id=23&name=Kai%20Jim",$myArray);
print_r($myArray);
?>
Output :
Array ( [id] => 23 [name] => Kai Jim )

htmlspecialchars()

* The htmlspecialchars() function converts some predefined characters to HTML entities.
* The predefined characters are:
       & (ampersand) becomes &amp;
       " (double quote) becomes &quot;
       ' (single quote) becomes &#039;
       < (less than) becomes &lt;
       > (greater than) becomes &gt;

Syntax htmlspecialchars(string,quotestyle,character-set)
* string(Required) - Specifies the string to parse.
* quotestyle(Optional) - Specifies how to encode single and double quotes.
The available quote styles are:
         ENT_COMPAT - Default. Encodes only double quotes.
         ENT_QUOTES - Encodes double and single quotes.
         ENT_NOQUOTES - Does not encode any quotes.
* character-set(Optional) - A string that specifies which character-set to use.
example:
  	
<?php
$str = "Jane & 'Tarzan'";
echo htmlspecialchars($str, ENT_COMPAT);
echo "
"; echo htmlspecialchars($str, ENT_QUOTES); echo "
"; echo htmlspecialchars($str, ENT_NOQUOTES); ?>
Output :
* The browser output of the code above will be:
Jane & 'Tarzan'
Jane & 'Tarzan'
Jane & 'Tarzan'
* If you select "View source" in the browser window, you will see the following HTML:
<html>
<body>
Jane &amp; 'Tarzan'
Jane &amp; &#039;Tarzan&#039;
Jane &amp; 'Tarzan'
</body>
</html>

Questions

  1. What is the use of md5() and uniqid() ?
  2. When to use urlencode() and urldecode() ?
  3. What are required parameters of mail() ?
  4. Given string is "Kapil Dev, Sunil Gavaskar, Sachin Tendulkar, Sourabh Ganguly, Rahul Dravid"
    Write the code how many times the string "il" appears ?

After editing Click here:
Output:
Hello World