PHP is an interpreted language.
PHP Questions and Answers
PHP is an important part of the web world, and every web developer should have the basic knowledge in PHP. Common PHP questions, which should help you become a best PHP developer.
What is the difference between compiler language and interpreted language ?
Interpreted language executes line by line , if there is some error on a line it stops the execution of script.PHP is an interpreted language.
Compiler language can execute the whole script at a time and gives all the errors at a time It does not stops the execution of script ,if there is some error on some line.
What is difference between TRUNCATE and DELETE in mysql ?
1: DELETE is a DML(data manipulation language) command whereas truncate is a DDL(data definition language) command.
2 : Truncate is much faster than Delete.
3 : We can not roll back in truncate but in delete we can rollback.
4 : We can not use where clause in truncate but in delete we can use conditions using where clause.
What is MYSQL storage engines ?
A storage engine is a software module that a database management system uses to create ,read and update the data from a database.
MySQL supports several storage engines that act as handlers for different table types. MySQL storage engines include both those that handle transaction-safe tables and those that handle nontransaction-safe tables:
MySQL supports following storage engines,
» MyISAM
» InnoDB
» MERGE
» MEMORY (HEAP)
» BDB (BerkeleyDB)
» EXAMPLE
» FEDERATED
» ARCHIVE
» CSV
» BLACKHOLE
What is array_multisort() in PHP ?
array_multisort() is using to sort several arrays at once or sort multidimensional array, by one or more dimensions.
In this the associative (string) keys will be maintained but numeric keys will be reindexed.
$array1 = array(23,60,50,70);
$array2 = array(2,3,6,5);
array_multisort($array1 , $array2);
OUTPUT:
var_dump($array1);
array(4)
{
[0] => int(23)
[1] => int(50)
[2] => int(60)
[3] => int(70)
}
var_dump(array2);
array(4)
{
[0] => int(2)
[1] => int(6)
[2] => int(3)
[3] => int(5)
}
How can we get the first element of an array in php ?
We can get the first element of an array by the function current();
$arr = array(‘name’=>’Kiran’,’age’=>’24’,’profession’=>’PHP Developer’);
echo $firstvalue = current($arr);
OUTPUT: Kiran
What is end function in php ?
$arr = array(‘name’=>’Kiran’,’age’=>’24’,’profession’=>’PHP Developer’);
echo $profession = end($arr);
OUTPUT : PHP Developer
Why we use strpos() in PHP ?
strpos() using to find the first occurrence of a string in another string. It return the position of the sub-string in a string.If the searched string will not exists in the searching string it return false.
$mystring = “PHP Developer”;
$find = “Developer”;
echo $result = strpos($mystring,$find);
OUTPUT: 4
What is difference between mysql_connect() and mysql_pconnect() ?
mysql_connect() and mysql_pconnect() both are working for database connection but with little difference. In mysql_pconnect(), ‘p’ stands for persistance connection.
When we are using mysql_connect() function, every time it is opening and closing the database connection, depending on the request .
But in case of mysql_pconnect() function,
First, when connecting, the function would try to find a (persistent) connection that’s already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection.
Second, the connection to the SQL server will not be closed when the execution of the script ends. Instead, the connection will remain open for future use (mysql_close() will not close connection established by mysql_pconnect()).
mysql_pconncet() is useful when you have a lot of traffice on your site. At that time for every request it will not open a connection but will take it from the pool. This will increase the efficiency of your site. But for general use mysql_connect() is best.
mysql_pconnect() is deprecated as of PHP 5.5.0, and will be removed in the future because it has several disadvantage.
How to get the number of elements in an array using PHP ?
We have two functions to get the number of elements in an array ie count() and sizeof()
Syntax : count ($array) and sizeof($array)
$a[0] = 15;
$a[1] = 12;
$a[2] = 50;
$a[3] = 41;
$a[4] = 42;
echo $result = count($a);
OUTPUT: 4