Showing posts with label PHP Interviews Questions. Show all posts
Showing posts with label PHP Interviews Questions. Show all posts

Saturday, October 27, 2018

phpMyPassion

Top PHP7 Interview Questions And Answers for Freshers & Experienced

This article shows up top frequently asked PHP7 interview questions and answers for PHP Developers, This list of PHP interview questions and answers will be helpful for PHP programmers.


Top PHP7 Interview Questions And Answers for Freshers & Experienced


Question #1 - How array_walk function works in PHP?


It is used to update the elements/index of original array.
How: in array_walk, two parameter are required. 
  1. original array
  2. An callback function, with use of we update the array. 
Question #2 - How to achieve Multilevel inheritance in PHP7?


//base class
class a{}

//parent class extend the base class
class bextends a{}

//chid class extend the parent class
class c extends b{}

Question #3 - How to get 2nd highest salary of employee, if two employee may have same salary?


select salary from employee group by salary order by salary limit 1,1

Question #4 - require_once(), require(), include().What is difference between them?


require() includes and evaluates a specific file, while require_once() does that only if it has not been included before (on the same page). So, require_once() is recommended to use when you want to include a file where you have a lot of functions for example. This way you make sure you don't include the file more times and you will not get the "function re-declared" error.
Question #5 - What type of inheritance supports by PHP7?


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 #6 - How do you call a constructor for a parent class?

parent::constructor($value);
Question #7 - How to find duplicate email records in users table?

SELECT u1.first_name, u1.last_name, u1.email FROM users as u1
INNER JOIN (
    SELECT email FROM users GROUP BY email HAVING count(id) > 1
    ) u2 ON u1.email = u2.email;

Question #8- What is the difference between Session and Cookie?
Both are used to store user information on server. The main difference between sessions and cookies is that sessions are stored on the server, and cookies are stored on the user's computers in the text file format. Cookies can't hold multiple variable while session can hold multiple variables..We can set expiry for a cookie,The session only remains active as long as the browser is open.Users do not have access to the data you stored in Session,Since it is stored in the server.Session is mainly used for login/logout purpose while cookies using for user activity tracking
Question #9- How to pass data in header while using CURL?
$url='http://www.web-technology-experts-notes.in';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'headerKey: HeaderValue ',
  'Content-Type: application/json',
  "HTTP_X_FORWARDED_FOR: xxx.xxx.x.xx"
));
echo curl_exec($ch);
curl_close($ch);

Question #10 - What is Final Keyword in PHP7?

PHP7 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final keyword.
Question #11 - How can we prevent SQL-injection in PHP?


Sanitize the user data before Storing in database.
While displaying the data in browser Convert all applicable characters to HTML entities using htmlentities functions.
Question #12 - How to set header in CURL?

curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml")); 

Question #13 - What are different type of sorting functions in PHP7?

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 #14 - How to redirect https to http URL and vice versa in .htaccess?

Redirect https to http
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Redirect http to https


RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

Question #15 - What are benefits of .htaccess?

  • Routing the URL
  • Mange Error Pages for Better SEO
  • Redirection pages
  • Detect OS (like Mobile/Laptop/Ios/Android etc)
  • Set PHP Config variable
  • Set Environment variable
  • Allow/Deny visitors by IP Address
  • Password protection for File/Directory
  • Optimize Performance of website
  • Improve Site Security 

Question #16 - How we can retrieve the data in the result set of MySQL using PHP7?


  • mysqli_fetch_row()
  • mysqli_fetch_array()
  • mysqli_fetch_object()
  • mysqli_fetch_assoc()

Question #17 - What is the use of explode() function ?


This function is used to split a string into an array. Syntax : array explode( string $delimiter , string $string [, int $limit ] ); 
Question #18 - What is the difference between explode() and split() functions?


Split function splits string into array by regular expression. Explode splits a string into array by string.
Both function are used to breaks a string into an array, the difference is that Split() function breaks split string into an array by regular expression and explode() splits a string into an array by string. explode() is faster than split() because it does not match the string based on regular expression.  
Question #19 - What is the use of mysqli_real_escape_string() function?


mysqli_real_escape_string() function mainly used to escapes special characters in a string for use in an SQL statement
Question #20 - Write down the code for save an uploaded file in php.


if ($_FILES["file"]["error"] == 0)
{
move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
Question #21 - How to create a text file in php7?


$filename = "/home/user/guest/newfile.txt";
$file = fopen( $filename, "w" );
if( $file == false )
{
echo ( "Error in opening new file" ); exit();
}
fwrite( $file, "This is a simple test\n" );
fclose( $file );
Question #22 - How to strip whitespace (or other characters) from the beginning and end of a string ?


We can use trim() function to remove whitespaces or other predefined characters from both sides of a string.
Question #23 - What is output of following?
$a = '10';
$b = &$a;

$b = "2$b";

echo $a;
echo $b;

Output


210
210

Question #24 - What are Traits?


Traits are a mechanism that allows you to create reusable code in PHP7 where multiple inheritance is not supported. To create a Traits we use keyword trait. 

Example of Traits
trait users {
    function getUserType() { }
    function getUserDescription() {  }
    function getUserDelete() {  }
}

class ezcReflectionMethod extends ReflectionMethod {
    use users;    
}

class ezcReflectionFunction extends ReflectionFunction {
    use users;
}

Question #25 - How to extend a class defined with Final?


No, You can't extend. A class declare with final keyword can't be extend.
Question #26 - What is full form of PSRs?

PHP Standards Recommendations. 
Question #27 - How do you execute a PHP script from the command line?


  • Get the php.exe path from server (My PHP path is : D:\wamp\bin\php\php5.5.12)
  • In environment variable, Add this path path under PATH variable.
  • Re-start the computer.
  • Now, you can execute php files from command file.
php index.php

Question #28 - What is stdClass in PHP7?


It is PHP7 generic empty class.
stdClass is used to create the new Object. For Example
$newObj = new stdClass();
$newObj->name='What is your name?';
$newObj->description='Tell me about yourself?';
$newObj->areYouInIndia=1;
print_r($newObj);

Output


stdClass Object
(
    [name] => What is your name?
    [description] => Tell me about yourself?
    [areYouInIndia] => 1
)

Question #29 - How to start displaying errors in PHP7 application ?

Add following code in PHP.

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

OR
Add following code in .htacess


php_flag display_startup_errors on
php_flag display_errors on
php_flag html_errors on
php_flag  log_errors on

Question #30 - What are different type of errors?


E_ERROR: A fatal error that causes script termination.
E_WARNING: Run-time warning that does not cause script termination.
E_PARSE: Compile time parse error.
E_NOTICE: Run time notice caused due to error in code.
E_CORE_ERROR: Fatal errors that occur during PHP's initial startup.
E_CORE_WARNING: Warnings that occur during PHP's initial startup.
E_COMPILE_ERROR: Fatal compile-time errors indication problem with script.
E_USER_ERROR: User-generated error message.
E_USER_WARNING: User-generated warning message.
E_USER_NOTICE: User-generated notice message.
E_STRICT: Run-time notices.
E_RECOVERABLE_ERROR: Catchable fatal error indicating a dangerous error 
E_ALL: Catches all errors and warnings 

Question #31 - When to use self over $this?

Use $this to refer to the current object.
Use self to refer to the current class.

Example of use of $this


class MyClass1 {
    private $nonStaticMember = 'nonStaticMember  member Called.';        
    
    public function funcName() {
        return $this->nonStaticMember;
    }
}

$classObj = new MyClass1();
echo $classObj->funcName();//nonStaticMember  member Called.


Example of use of SELF


class MyClass2 {
    private static $staticMember = 'staticMember  member Called.';        
    
    public function funcName() {
        return self::$staticMember;
    }
}

$classObj = new MyClass2();
echo $classObj->funcName(); //staticMember  member Called.

Read More

Monday, February 19, 2018

phpMyPassion

Basic PHP Interview Questions and Answers for Freshers

Here I am sharing top most asked basic PHP interview questions and answers for freshers that will helpful for freshers to get a job. All questions are related to PHP programming language so do prepare before the interview.

Question #1 - What is PHP?

PHP is an open source server side scripting language mostly used for web applications. Its easy to learn compare to other programming language. PHP also a object oriented programming language like java, .net and c++.

Question #2 - Who is the father of PHP and when it is developed?

PHP was developed in year 1994 by Rasmus Lerdorf.

Question #3 - What is the use of "echo" in php?

Mainly "echo" used to print a data on screen, for an Example: <?php echo 'This is my first website'; ?> , if we run it on browser, it will print the text on the screen.
Question #4 - How to include a file to a php page?

We include a file using "include('filepath.php') " or "require('filepath.php')" function with file path as its parameter.

Question #5 - What is the difference between PHP4 and PHP5?

Below are the difference :-

Sr. No.
PHP4
PHP5
1.
PHP4 doesn’t support oops concepts
PHP5 supports oops concept
2.
Error handling is not good
Error handling is better
3.
PHP4 doesn’t show the function parameter hint.
PHP5 supports function’s perameter
Question #6 - What's the difference between include and require?

If the file is not found by require(), it will cause a fatal error and stop the execution of the script. If the file is not found by include(), a warning will be generate, but execution will continue.

Question #7 - Differences between GET and POST methods ?

We can only send 1024 bytes through GET method but by POST method can transfer large amount of data and POST is also a secure method than GET method .

Question #8 - What is the use of 'print' in php?

Print commonly used to print text on screen like echo but print has a return type.  you can use with out parentheses with its argument list.
Example 
print('50 PHP Interview questions'); 
print 'Job Interview questions ');

Question #9 - What is the difference between echo and print

Below are the difference between echo and print :-

  • Print has a return type value while Echo doesn't return any value.
  • Echo is faster then Print.
  • In Echo you can pass multiple argument while single argument can be pass in Print.

Question #10 - what is the use of isset() in php?

isset() function is used to ensure if a variable is set and is not NULL

Question #11 - What is the difference between $var and $$var?

Both are and just like a variable only difference is $var is only a variable while $$var is variable in a another variable. for an example -
$var = 'love';  //only a variable
$$var = 'PhpMyPassion.com';  // a variable that contain another vairable

means ${$var} where $var is equals to 'love'; so we can say $love is equals to $$var

Question #12 - What is the maximum  filesize that can be uploaded in PHP and how can we increase it?

The maximum size of a file that can be uploaded is set 2MB By default. if you want to change, you can do that by changing the maximum uploaded size value inside php.ini file set by upload_max_filesize = 5M and restart all the services.

Question #13 - How can we get the value of current session id?

You can get the current session id by using session_id() function in PHP.

Question #14: How to identify server IP address in PHP?

you can identify server IP address by using $_SERVER['SERVER_ADDR']; 


Read More

Tuesday, February 13, 2018

phpMyPassion

Top 35 Frequently Asked PHP Interview Questions And Answers for 1 year experience

According to my experience here I am sharing top 35 frequently asked PHP interview questions and answers for 1 year experiences PHP developer, This list of PHP interview questions and answers will be helpful for PHP experience developer.



Question #1 - What is the difference between $message and $$message?

They are both variables But $message is a variable with a fixed name. $$message is a variable whose name is stored in $message. For example, if $message contains "var", $$message is the same as $var.

Question #2 - How to include a file to a php page?

We include a file using "include('filepath.php') " or "require('filepath.php')" function with file path as its parameter.

Question #3 - What's the difference between include and require?

If the file is not found by require(), it will cause a fatal error and stop the execution of the script. If the file is not found by include(), a warning will be generate, but execution will continue.

Question #4 - Differences between GET and POST methods ?

We can only send 1024 bytes through GET method but by POST method can transfer large amount of data and POST is also a secure method than GET method .

Question #5 - require_once(), require(), include().What is difference between them?

require() includes and evaluates a specific file, while require_once() does that only if it has not been included before (on the same page). So, require_once() is recommended to use when you want to include a file where you have a lot of functions for example. This way you make sure you don't include the file more times and you will not get the "function re-declared" error.

Question #6: How to get the location of php.ini?

You can get the location of php.ini by using <?php phpinfo(); ?> code.


Question #7 - Differences between GET and POST methods ?


We can only send 1024 bytes through GET method but by POST method can transfer large amount of data and POST is also a secure method than GET method .

Question #8: How to identify server IP address in PHP?

you can identify server IP address by using $_SERVER['SERVER_ADDR']; 

Question #9: What is use of header() function in PHP ?

Header() function is mostly used to Redirect from one page to another -

<?php header("Location:page.php"); ?>

Question #10 - 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 #11 - What is the use of 'print' in php?

Print commonly used to print text on screen like echo but print has a return type.  you can use with out parentheses with its argument list.
Example 
print('50 PHP Interview questions');  
print 'Job Interview questions ';

Question #12 - What is the difference between Session and Cookie?

Both are used to store user information on server. The main difference between sessions and cookies is that sessions are stored on the server, and cookies are stored on the user's computers in the text file format. Cookies can't hold multiple variable while session can hold multiple variables..We can set expiry for a cookie,The session only remains active as long as the browser is open.Users do not have access to the data you stored in Session,Since it is stored in the server.Session is mainly used for login/logout purpose while cookies using for user activity tracking

Question #13 - How to set cookies in PHP?

We can set cookies like Setcookie("user", "ram", time()+3600);

Question #14 - How to create a session? How to set a value in session ? How to Remove data from a session?


Create session : session_start();
Set value into session : $_SESSION['USER_ID']=1;
Remove data from a session : unset($_SESSION['USER_ID'];

Question #15 - 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.

Question #16 - What is the use of explode() function ?

This function is used to split a string into an array. Syntax : array explode( string $delimiter , string $string [, int $limit ] ); 

Question #17 - What is the difference between explode() and split() functions?

Split function splits string into array by regular expression. Explode splits a string into array by string.
Both function are used to breaks a string into an array, the difference is that Split() function breaks split string into an array by regular expression and explode() splits a string into an array by string. explode() is faster than split() because it does not match the string based on regular expression.  


Question #18 - What is the use of mysql_real_escape_string() function?

mysql_real_escape_string() function mainly used to escapes special characters in a string for use in an SQL statement

Question #19 - How to redirect a page in php?

we can redirect a page by using header("Location:index.php"); function.

Question #20 - How to find the length of a string?


we can use strlen() function to find the length of a string
Question #21 - what is the use of isset() in php?

isset() function is used to ensure if a variable is set and is not NULL

Question #22 - How to delete a file from the system

we can delete a file by using Unlink() function in php.

Question #23 - what is the difference between javascript and PHP ?

Commonly JavaScript known as Client side scripting language while PHP is a server side scripting language.

Question #24 - What is MIME? 

Full form of MIME is "Multi-purpose Internet Mail Extensions".
It is extension of e-mail protocol helps to exchanges the different kids of data files over the internet.
Data files may be audio, video, images, application programs and ASCII etc.

Question #25 - How to create a mysql connection?

we can create connection by using function mysql_connect(hostname,username,password,database);

Question #26 - How to select a database?

we can select database by using mysql_select_db($db_name); function and we can also select a database by using function mysql_query('use databaseName'); 


Question #27 - How To Select The nth Highest Record In MySQL?

The query to get the nth highest record is as follows:


SELECT 
    *
FROM
    table_name
ORDER BY column_name DESC
LIMIT n - 1, 1;

Question #28 - What is the difference between mysql_fetch_array() and mysql_fetch_assoc() ?

mysql_fetch_assoc() function Fetch data as an associative array, While mysql_fetch_array() fetches data as an associative array, a numeric array, or both

Question #29: What type of inheritance does not support by PHP ?

Only Multiple Inheritance does not support by PHP.


Question #30: What is PEAR?

PEAR stands for PHP Extension and Application Repository. PEAR is a framework and repository for re-usable PHP components.

Question #31: How to get duplicate values from array?


you can get duplicate values from an array by using array_diff_assoc($arr, $arr_unique); for an example :


<php 
         $arr = ('anil','anil','sonia','monica','sunita','sunita','raj');
         $arr_unique = array_unique($arr);
         $arr_duplicates = array_diff_assoc($arr, $arr_unique);
         print_r($arr_duplicates);
?>

It will return :

             Array(
                          [1] => anil

                          [2] => sunita
                       )


Question #32 - What is CSS?


CSS known as cascading Style Sheet. It is a best technique to style and present HTML.
Question #33 - What are the differences between ID and class in CSS? 


ID is used to identify one element , while a class is used to identify more than one element.

Question #34 - How stop the execution of a php scrip ?


We mostly use exit() function to stop the execution of a page


Question #35 - How to strip whitespace (or other characters) from the beginning and end of a string ?

Read More