PHP Questions

Question : Set global veriable in PHP>

$GLOBALS[‘base_url’] = ‘http://localhost/crud/’;
print gloable verible : print_r($base_url);
output : http://localhost/crud/

Question : set session in php?

session_start();
$_SESSION[“favcolor”] = “green”;

// remove all session variables
session_unset();
// destroy the session
session_destroy();

Question : set cookies in php?

setcookie(‘life’,’its my life’, 0);

setcookie() function must appear BEFORE the <html> tag.
setcookie(name, value, expire, path, domain, secure, httponly);
$cookie_name = “user”;
$cookie_value = “John Doe”;
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), “/”); // 86400 = 1 day
Get value : $_COOKIE[$cookie_name]
Delete cookie :
// set the expiration date to one hour ago
setcookie(“user”, “”, time() – 3600);

 

Question: What are different type of sorting functions in PHP?
sort() – sort arrays in ascending order. asort() – sort associative arrays in ascending order, according to the value.
ksort() – sort associative arrays in ascending order, according to the key.
arsort() – sort associative arrays in descending order, according to the value.
rsort() – sort arrays in descending order.
krsort() – sort associative arrays in descending order, according to the key.
array_multisort() – sort the multi dimension array.
usort()- Sort the array using user defined function.

Question: What are default session time and path?
Session Time: 1440 seconds
Session Path: /tmp folder in server.

Question: What type of inheritance supports by PHP?
There are following type of inheritance
Single Inheritance – Support by PHP
Multiple Inheritance – Not support
Hierarchical Inheritance – Support by PHP
Multilevel Inheritance – Support by PHP

Question: What is use of header() function in php?

There are header() function uses :

  1. Header is used to redirect from current page to another: header(“Location: newpage.php”);
  2. Header is used to send HTTP status code : header(“HTTP/1.0 404 Not Found”);
  3. Header is used to send Send a raw HTTP header : header(‘Content-Type: application/pdf’);

Question: Output of code :

$str1 = 'yabadabadoo';
$str2 = 'yaba';
echo  $haspostion = strpos($str1,$str2)) {     ;

Output : 0

Explanation : it show 1st occurence of “yaba” in string which is on position 0 show output will be 0. To awoid this problem we have to check it with

if(strpos($str1, $str2) !== false){

//….you code is here

}

 Question: Run Php code on windows CMD

C:\xampp\htdocs>php -r “print_r($_SERVER);”

Question: Explaine array_merge()

$a1=array(“red”,”green”);
$a2=array(“blue”,”yellow”);
print_r(array_merge($a1,$a2));

Output:

Array ( [0] => red [1] => green [2] => blue [3] => yellow )

The array_merge() function merges one or more arrays into one array.

Tip: You can assign one array to the function, or as many as you like.

Note: If two or more array elements have the same key, the last one overrides the others.

Note: If you assign only one array to the array_merge() function, and the keys are integers, the function returns a new array with integer keys starting at 0 and increases by 1 for each value (See Example 2 below).

Tip: The difference between this function and the array_merge_recursive() function is when two or more array elements have the same key. Instead of override the keys, the array_merge_recursive() function makes the value as an array.

Exam:

$array1 = array(“color” => “red”, 2, 4);
$array2 = array(“a”, “b”, “color” => “green”, “shape” => “trapezoid”, 4);
$result = array_merge($array1, $array2);

print_r($result);

Array([color] => green [0] => 2 [1] => 4 [2] => a [3] => b [shape] => trapezoid [4] => 4)

Question : Explaine array_search() ?

array_search(value,array,strict)

The array_search() function search an array for a value and returns the key.

$a=array(“a”=>”yellow”,”b”=>”white”,”c”=>”pink”);
echo array_search(“yellow”,$a);

Output : a

Question : Explaine array_combine() ?

The array_combine() function creates an array by using the elements from one “keys” array and one “values” array. > array_combine(keys,values);

$fname=array(“Peter”,”Ben”,”Joe”);
$age=array(“35″,”37″,”43”);

$c=array_combine($fname,$age);
print_r($c);

Output : array(“Peter”=>”35″,”Ben”=>”37″,”Joe”=>”43”);

Question : Explaine array_slice()

$arr = array(“hello”, “good”, “fine”, “good”, “fine”, “hello”, “bye”);
print_r(array_slice($arr,0,-2));

Array ( [0] => hello [1] => good [2] => fine [3] => good [4] => fine )

Question : Explaine array_uniqe()

$arr = array(“hello”, “good”, “fine”, “good”, “fine”, “hello”, “bye”);
print_r(array_unique($arr));

Array ( [0] => hello [1] => good [2] => fine [6] => bye );

$arr = array(“2”, “2”, “3”, “3”, “4”, “4”, “5”);
print_r(array_unique($arr));

Array ( [0] => 2 [2] => 3 [4] => 4 [6] => 5 )

Question : Explaine array_diff()

$a1=array(“a”=>”1″,”b”=>”1″,”c”=>”blue”,”d”=>”yellow”);
$a2=array(“e”=>”1″,”f”=>”1″,”g”=>”1″,”d”=>”yellow”);

$result=array_diff($a1,$a2);
print_r($result);

Array ( [c] => blue )

Question : What is the difference between the WHERE and HAVING clauses?

WHERE : clause is used to filter records from a result. The filtering occurs before any groupings are made.
HAVING : clause is used to filter values from a group (i.e., to check conditions after aggregation into groups has been performed).

<!–Question : Given a table dbo.users where the column user_id is a unique identifier, how can you efficiently select the first 100 odd user_id values from the table?
(Assume the table contains well over 100 records with odd user_id values.)

SELECT TOP 100 user_id FROM dbo.users WHERE user_id % 2 = 1 ORDER BY user_id –>

Question : Query to find Nth highest salary from a salary table?

SELECT salary  FROM tbl_salary ORDER BY salary DESC LIMIT 1 OFFSET (n – 1);

TINYINT is always 8 bits and can store 28 distinct values.

SMALLINT is always 16 bits and can store 216 distinct values.

INT is always 32 bits and can store 232 distinct values.

BIGINT is always 64 bits and can store 264 distinct values.

Question : Find 7 between 1 to 1000?
$count = 0;
for($a = 0; $a <1000; $a ++){

if(stristr($a, ‘7’) && strstr($a, ‘7’)){
echo $a; echo ”
“; $count++;
}
}
echo $count;

Question : Get array value withot foreach

Array
(
    [0] => XL
    [1] => gold
)

Question : Swap two variables without using third variable?

$a =’aaa’;
$b = ‘6’;
list($a,$b) = array($b,$a);
echo $a.’
‘;
echo $b;

Question : What is Indexing in Mysql?

Question : Find function execution time?

Question : What structure should be followed to make a multilingual website?

Question : What is CI routing?

Question : What is commomne between Cookies and Session ?

Question : How valid mail in CI?

Question : What is difference between For, Foreach,While loop in php, Can I use associative array in for loop?

Question : What is ng-int in Anguler JS?

Question : Can I do Binding in mysql and if can than what is it?

https://www.toptal.com/php/interview-questions

https://www.toptal.com/cakephp/interview-questions

http://www.codeproject.com/Articles/618484/Latest-jQuery-interview-questions-and-answers

https://www.codementor.io/php/tutorial/php-interview-questions-sa5mple-answers

How can you enable error reporting in PHP?

Check if “display_errors” is equal “on” in the php.ini or declare “ini_set(‘display_errors’, 1)” in your script.
Then, include “error_reporting(E_ALL)” in your code to display all types of error messages during the script execution

count() : No of element in array

The value of the variable input is a string 1,2,3,4,5,6,7. How would you get the sum of the integers contained inside input?

echo array_sum(explode(‘,’,$input));

Delete array value without foreach :

$messages = [312, 401, 1599, 3, …];

if(($key = array_search($del_val, $messages)) !== false) {

unset($messages[$key]);

}

array_walk : Apply a user supplied function to every member of an array

bool array_walk ( array &$array , callable $callback [, mixed $userdata = NULL ] )

strstr() : Find the 1st occurence in the string and return remaing string with find word

Example : echo strstr(“Hello world! 123″,”world”);</p>

output : world! 123

stristr() :Finds the first occurrence of a string inside another string (case-insensitive)

strpos() : Find the postion in string
Example : echo strpos(“I love php, I love php too!”,”php”);
Output: 7

strpos() : Finds the position of the first occurrence of a string inside another string (case-sensitive)

stripos() : Finds the position of the first occurrence of a string inside another string (case-insensitive)

strripos() : Finds the position of the last occurrence of a string inside another string (case-insensitive)\

substr : Return “world” from the string
Example : echo substr(“Hello world”,-1);
Output : d

 

strrev() : Reverse the string

strrev(“Hello World!”) : !dlroW olleH

Advertisements