Category Archives: php

Does we can find the browser capabilities in PHP ?

Yes, we can find the browser capabilities in php using get_browser() function . it is a inbuilt function
in PHP. It returns an object or an array with information about the user’s browser on success, or
FALSE on failure.
The get_browser() function has 2 parameters and are optional parameter, they are given bellow :-
 user_agent : it is utilized to specify the name of an HTTP user agent.
 return_array : it return an array instead of an object if the value is set to true.

Syntax : get_browser(user_agent, return_array)

Example :

$browser = get_browser();

Output :
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/98.0.4758.102 Safari/537.36

What is Exception Handling in PHP ?

Exception handling in php  is used to modify the normal flow of code execution if a specified error occurs. When an exception is given , the following  code will not be executed, and PHP will try to find the appropriate “catch” block. If an exception is not caught, a fatal error will be displayed.

Keywords in Exception Handling:

  • try : This is a block of code where an exception may occur.
  • catch: It represent block of code that will be executed when a particular exception has been thrown.
  • throw: It is used to throw an exception. It is also used to list the exceptions that a function throws, but doesn’t handle itself. Each “throw” must have at least one “catch”.
  • finally: It is used in place of catch block or after catch block basically it is put for cleanup activity in PHP code.

Example :

function checkMarkStatus($mark) {
  if($mark>=5) {
    throw new Exception("The Student is Pass.");
  return true;
try {
  echo 'The Student is Fail';
catch(Exception $e) {
  echo $e->getMessage();

Advantages of Exception Handling :

  • Sometimes error handling is very difficult for the developer to understand in normal coding. With try catch block it is more readable and understandable.
  • Exceptions allow you to distinguish between different types of errors, and is also great for routing.
  • The thrown exception can then be handled by a custom error handler.

How To Use Ternary Operator Instead Of if else Condition in php

Instead of writing long lines for if condition we can short it into a single line. Here we are using ternary operator as a short-hand method of if-else condition in PHP. Therefore, the ternary operator has three operands. We write the condition at the beginning, which is the first operand. Then the symbol ‘?’ separates the condition with the expression to be executed if the condition is true. Finally, the ‘ : ‘  symbol separates the true and the false expression. The syntax is written below.

Syntax :

condition ? trueExpression : falseExpression

Example :

$text =”abc”;
$word = ($text == 'bc') ? "true" : "false";
echo $word;

Special functions in PHP

Lets discuss some special functions in PHP. In PHP, we can able TO define some special functions that will be called automatically. Those functions not required function call to execute the code inside these functions.

  1. Die()
  2. Sleep()
  3. Var_dump()
  4. Print_r()
  5. Exit


  • Inbuilt php function.
  • Prints the message and terminate the php script.
  • It accepts only single parameter and which is not mandatory to be passed .

Syntax : die($message);


  • Inbuilt php function.
  • It accepts only single parameter and which is mandatory to be passed.
  • Used to delay the execution of the program for given number of seconds.
  • It returns zero on success, or FALSE on error. This function returns a non-zero value if the call was interrupted by a signal.

Syntax : int sleep(int $seconds)


  • Inbuilt php function.
  • dumps information about one or more variables.
  • includes the data type and value of the variable.
  • In case of string, it also includes the size of the string passed inside the function.
  • The array and object are explored recursively with values to show their structure.

Syntax: var_dump(var1, var2, …);

  • Inbuilt php function.
  • displays information about a variable in a human-readable way.
  • It shows the information stored in a variable, which is easily understandable to the user.
  • Used to print an array.

Syntax : print_r(variable);


  • Inbuilt php function.
  • terminates the execution of the script.
  • The shutdown functions and object destructors will always be executed even if exit() function is called.
  • act as an alias of the die() function.

Syntax :  exit(message);

Google Recaptcha in CodeIgniter

It is one of the major discussed question, how we can use google recaptcha in codeigniter.First we should need a google account to start. Login the corresponding gmail and access from your browser. Next you have to register the domain. After that you will get a site key and secret key.

Herecomes the our coding part, insert following part in header

<script src=''></script>

For showing the recpatcha section, use the below html code. Complete the code by using your data site key.

<div class="g-recaptcha" data-sitekey="ENTER YOUR SITE KEY"></div>

Add the secret key in your controller with following code.

$recaptchaResponse = trim($this->input->post('g-recaptcha-response'));
            $secret='ENTER THE SECRET KEY';
            $credential = array(
                    'secret' => $secret,
                    'response' => $this->input->post('g-recaptcha-response')
                $verify = curl_init();
                curl_setopt($verify, CURLOPT_URL, "");
                curl_setopt($verify, CURLOPT_POST, true);
                curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query($credential));
                curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($verify, CURLOPT_RETURNTRANSFER, true);
                $response = curl_exec($verify);
                $status= json_decode($response, true);
			 echo “sucesss”;
			  Echo “fail”;