PHP

How to install extension php?

$ sudo apt-get install extension_name
The above command install the specified extension in unix/linux box.

How to install intl extension php?

$ sudo apt-get install php5-intl

How to install PHP accelerator APC?

sudo apt-get install php-apc

The extension already installed then it will upgrade if any new version released.

If no latest version release after our installation then it show message like below

$ sudo apt-get install php-apc
[sudo] password for sasikala:
Reading package lists... Done
Building dependency tree
Reading state information... Done
php-apc is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 404 not upgraded.

PHP Currency Converter

To convert the currency using the 3rd party API. Used "http://www.google.com/ig/calculator?hl=en&q=from=?To

For example to convert 1 USD to INR:
http://www.google.com/ig/calculator?hl=en&q=1USD=?INR

The below functions take input and passed API and parse it.

public function SingleConversion($inputCurrency,$toCurrency)
{
$curAmt=explode(' ',$inputCurrency);
$inputCur = $curAmt[0];
$inputAmt = $curAmt[1];
$string = $inputAmt.$inputCur."=?".$toCurrency;
$converter = "http://www.google.com/ig/calculator?hl=en&q=".$string;
$this->getAPIRates($converter);
}
public function getAPIRates($url)
{
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$result = curl_exec($ch);
curl_close($ch);

$result = explode('"', $result);
$from = explode(' ', $result[1]);
$to = explode(' ', $result[3]);
echo $to;
}

Selenium Testing - Firebox - selenium IDE for web testing

Selenium test scripts are widely used for web testing. Firebox have lots of plugins to add on functionality to it. Selenium IDE is one of the most popular firebox extension for selenium test scripts.

Selenium IDE mainly used to record and play back the steps, we do in browser. It gives us a integrated developement enviroment for selenium scripting.

PHP - Difference between echo and print?

echo :
* Language constuct and behaves like statement. It won't return value.
* echo takes multiple parameter seperated by commas.
* Faster than print.

print:
* Language construct and behaves like function. It return true / false.
* print is part of precedence table.
* print take only one parameter.

PHP interview Questions - Part II

How to remove a element from array and how to delete whole array?

The particular array element can be removed by using unset with array index for example

$colors = array('red','green','blue');

To remove the green use unset($colors[1]);

To delete the whole array use unset with just array name unset($colors);

How to get the resource type?

using the get_resource_type() function.

How to assign value to more than one variable at the same time in PHP?

For example we have variables test,countInt and sizeInt, then we can assign same value to three variables like below

$test=$countInt=$sizeInt = 0;

How to check method exists in class?

using method_exists() function

How to use global variable inside the function?

1. Using the global keyword
2. other way to access global variable is $GLOBAL array

for example


$toysCount=10;
displaytoysCount();

function displaytoysCount() {
global $toysCount;

echo "Number of toys available $toysCount";

//otherway using GLOBAL array
echo "Number of toys available ".$GLOBAL["toysCount"];
}

What is static variable and its purpose?

Static variables are local variable, it intialize the variable only when we call this at the first time and retain it even if the program excution leaves the scope.

It helps to retain value of the variable. We can use this to keep track number of times the particular page called or something similar to that.

What does error_reporting(0) and error_reporting(-1) in PHP?

error_reporting(0) - turns off all the error reporting, no errors logged.

error_reporting(-1) - report all possible PHP errors.

When you use gettype for unintialized variable, what output will you get?

It will display NULL. for example

$output;
echo gettype($output); // NULL

Difference between print_r , var_dump and var_export?

print_r , var_dump used for debugging purpose. The var_export returns the php code. These helps to gothru the contents of the variables recursively in readable format.

print_r() - prints information in human readable format.

$mixedArray = array(12, array("lily", "rose", "pansy"));


Array(
[0] =>12,
[1] => Array(
[0] => lily,
[1] => rose,
[2] => pansy
)
)

var_dump() - it displays the structure information, it includes the type of the variable and values. For example

var_dump($mixedArray);

result like

array(2) {
[0]=>
int(12)
[1]=>
array(3) {
[0]=>
string(4) "lily"
[1]=>
string(4) "rose"
[2]=>
string(5) "pansy"
}
}

var_export() - displays parsable string represenation of the variable. It is similar to var_dump except it return valid PHP code.

var_export()

result would be

array(
0=>12,
1=>array(
0=>'lily',
1=>'rose',
2=>'pansy',
),);

In case of resource it is good use print_r than the var_dump.

What is the purpose of making register_globals off and if it is off how you will access the form variable?

It helps to improve the security of applications.

It doesn't mean this setting alone making our application secure. There are lot of practice like intializing the variable, use superglobals $_GET, $_POST etc regardless of register_global on/off to access the variables, checks for uninitialized variable etc will help.

If the register_global is set to off, then you can access the form elements using superglobal arrays $_POST, $_GET.

In PHP, how do you check particular element exists in the array or not?

in_array() function helps to achieve this.


$companies = array('apple','bell');
in_array("bell",$companies); // return true
in_array("dell",$companies); // return false

How the super class or parent class functions are called in the child class?

The parent keyword used to refer the parent class. The keyword self refers the current class. The parent class functions are called using parent::parent_functionname(); The same way the constants,static variables also accessed.

for example

class Emp{
function getEmp() {
return "select * from emp ";
}
}

class EmpDep1 extends Emp {

function getEmp() {
return parent::getEmp()." where dept_id = 1 ";
}
}

$obj = new EmpDep1();
$obj->getEmp();

PHP interview Question - Part 1

In this article i will be writing some of the PHP interview question.

1. Difference between require and require_once?
Both are identical except the require_once(), check if the file already included and if so, just not inlcude it again.

2. How to connect to mysql?
mysql_connect : mysql_connect(server,username,password,[newlink=false], [clientflag=0]);
open or reuse the mysql server connection. By default when second time mysql_connect called with same parameter then if existing link available then that link identifier will be return. So no new connection established.

If you want to establish new connection each time, make the newlink value to true. So it will return new link each time. (This available from version 4.2.0)
3. What is variable variable and how to use it?

Variable variable use the variable value as variable. for example

$var = "cat";
$$var = "drinks the milk";

$var is variable, which holds "cat" as value. $cat holds the value "drinks the milk"

echo $$var; or
echo $cat;

both gives same output "drinks the milk"

4. What is the use of @ in front of function or expression?

If we prepend the @ operator in front of the expression or function then any error message generated by expression or function will be ignored. We can use
this when we don't want to show the error to user browser

5. How do you delete the cookie in PHP?

To delete the cookie just set the expire date to past. for example
setCookie("username","",time()-3600);

6. When do you use ===?

=== is to identify whether the two variable values and their types are same. For example $a === $b, it will return true if $a and $b values are same and $a and $b of same type (that is identical).

7. Difference betwen include and require?
In case of failure require() throws the fatal error and stops the scripts. include() construct throws warning and continue execute the script.

8. What is the difference between echo and print?
echo:
echo returns no value.
echo is a language construct. But can't be used in expression since it wont behave like function.
echo can hav multiple expression. For example : echo 1*2, 2*2;

print:
print return int value.
It is also a language construct, but it behaves like function so it can be used in expresssion to evaluate. for example: ($var)?print 'true':print 'false';
print can't take mutliple value.

9. what mysql_pconnect do in addition with mysql_connect?

mysql_connect : mysql_connect(server,username,password,[newlink=false], [clientflag=0]);
open or reuse the mysql server connection. By default when second time mysql_connect called with same parameter then if existing link available then that link identifier will be return. So no new connection established.

If you want to establish new connection each time, make the newlink value to true. So it will return new link each time.

mysql_pconnect:
mysql_pconnect(server,username,password[,client_flags]);
It will return persistent MySQL server Connection.

Difference Between mysql_connect and mysql_pconnect :
1. mysql_pconnect - first check for connection already exists for the same server, username and password, if it is avalable it will return that one. other wise open new one.
2. At the end of the script the mysql_pconnect remain open for future use.
3. mysql_close() - won't close pconnect connection.

10. When to use fgets and fgetss?
fgets and fgetss gets line file pointer. In addition, the fgetss do stripping the html/php tags and null bytes

I will write more in future. Please give your comments on it.

Syndicate content