PHP Interview Questions and Answers for Freshers 2019
by Mohammed, on Apr 2, 2018 2:32:46 PM
Q1. What is PHP?
Ans: PHP is a server-side scripting language. It is the most widely used web technology to create dynamic web pages. There are many PHP based frameworks and Open sources available for free to use. Some examples include WordPress, Drupal, Laravel, etc. We can embed PHP with HTML and can further write server-side code with PHP for web development.Q2. Which is the latest version of PHP?
Ans: Stable release: 7.1.4 / 13 Apr 2017; http://php.net/downloads.php
Q3. What are the extensions of PHP File?
Ans: .php, .phtml, .php3, .php4, .php5, .php7, .phps .
Q4. What is the difference between include(), include_once() and require_once()
Ans: The include() statement includes and evaluates a specified line i.e. it will include a file based in the given path. require() does the same thing except upon failure it will generate a fatal error and halt the script whereas include() will just give a warning and allow the script to continue. require_once() will check if the file already has been included and if so it will not include the file again.
Q5. Differences between GET, POST and REQUEST methods?
Ans: GET and POST are used to send information from client browser to web server. In case of GET, the information is sent via the GET method in name/value pair and is URL encoded. The default GET has a limit of 512 characters. The POST method transfers the information via HTTP Headers. The POST method does not have any restriction in data size to be sent. POST is used for sending data securely and ASCII and binary type’s data. The $_REQUEST contains the content of both $_GET, $_POST and $_COOKIE.
Q6. Explain how PHP sends output to Browser? OR How a file is processed when it sends output to the browser?
1. Open website http://www.tutorialmines.net
2. Request sent to a server of http://www.tutorialmines.net
3. Call PHP Files.
4. PHP Scripts are loaded into memory and compiled into Zend opcode.
5. These opcodes are executed and the HTML generated.
6. The same HTML is sent back to Browser.
Q7. What are the different errors in PHP?Ans: There are 4 basic types of error.
- Parse Error – Commonly caused due to syntax mistakes in codes e.g. missing semicolon, mismatch brackets.
- Fatal Error – These are basically run time errors which are caused when you try to access what can’t be done. E.g. accessing a dead object, or trying to use a function that hasn’t been declared.
- Warning Error – These occur when u try to include a file that is not present or delete a file that is not on the server. This will not halt the script; it will give the notice and continue with the next line of the script.
- Notice Error – These errors occur when u try to use a variable that hasn’t been declared, this will not halt the script, It will give the notice and continue with the next line of the script.
Q8. What are the various methods/ways to pass data from one web page to another web page?Ans: The different ways to pass the data as follows:
- URL parameters
Q9. What is session and why do we use it?
Ans: The session is a superglobal variable that preserves data across subsequent pages. Session uniquely defines each user with a session ID, so it helps to make a customized web application where user tracking is needed.
Q10. What is a cookie and why do we use it?
Ans: A cookie is a small piece of information stored in a client browser. It is a technique used to identify a user using the information stored in their browser (if already visited that website). Using PHP we can both set and get COOKIE.
Q11. What function do we use to find the length of string, and length of the array?
Ans: For finding the length of the string we use strlen() function and for an array, we use count() function.
Q12. What are the traits in php?
Ans: These are PHP mechanisms for code re-usability to ease developers for using the code in different class hierarchies.
Q13. List out the predefined classes in PHP?
Q14. How can we change the value of a constant?
Ans: We cannot change the value of a constant.
Q15. What is the difference between unset() and unlink() function.
Ans: unset() is used to destroy a variable whereas unlink() is used to destroy a file.
Q16. How to print the current date and time.
Ans: Get current date and time
<?php echo date ( 'Y-m-d H:i:s' ) ; ?>
Q17. How to encrypt and decrypt the password in php?
password_verifywhich are respectively used as a hashing and checking method. MD5 was used before but now has become obsolete.
Q18. What is the difference between explode() and split() functions?
Ans: Both are used to split a string to an array, the basic difference is that split() uses a pattern for splitting and explode()uses a string. explode() is faster than split() as it does not match the string based on a regular expression. Also, split() is deprecated as of 5.3.0. So using this function is discouraged.
Q19. What is PDO classes?
Ans: The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP. It is a data-access abstraction layer, so no matter what database we use the function to issue queries and fetch data will be same. Using PDO drivers we can connect to the database like DB2, Oracle, PostgreSQL, etc.
Q20. What are the Formatting and Printing Strings available in PHP?
printf()- Displays a formatted string
sprintf()-Saves a formatted string in a variable
fprintf() -Prints a formatted string to a file
number_format()-Formats numbers as strings
Q22. How to get the IP address in php?
Ans: The simplest way is to use
$_SERVER["REMOTE_ADDR"]; but there are many variables like proxies, server, client, system, current, and public that may require specific changes.
Q23. What are Constructors and Destructors?
Ans: CONSTRUCTOR: PHP allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.
DESTRUCTORS: PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++. The destructor method will be called as soon as all references to a particular object are removed or when the object is explicitly destroyed or in any order in shutdown sequence
Q24. What is the difference between ID and class in CSS?
Ans: The difference between an ID and Class is that an ID can be used to identify one element, whereas a class can be used to identify more than one.
Q25. for image work which library?
Ans: We will need to compile PHP with the GD library of image functions for this to work. GD and PHP may also require other libraries, depending on which image formats you want to work with.
Q26. How do we get the current session ID?
Q27. How do we destroy a session.
Ans: Destroy a session
Q28. What is an associative array?
Ans: Associative arrays are arrays that use named keys that you assign to them.
Q29. How can we show and hide an element via jquery?
Ans: Suppose we want to show and hide elements of a div with id div1.
Q30. How can we add change font size using jquery?
Ans: Suppose we want to change the font size of and div with id div1 from 12px to 18px.
Change font size using jquery
Q31. How to Print Fibonacci Series in PHP?
Q32. How to Print Factorial of a number in PHP?
Q33. How to Print numbers from 1 to 10 in While loop?
Q34. What are super global variables.
Ans: The superglobal variables as follows:
- $_COOKIE – It Stores all the information in an associative array of variables passed to the current script via HTTP Cookies.
- $_GET – Send data to the server which is visible in the URL of the browser. It is not safe to send sensitive using the GET method.
- $_POST – Send data to the server which is not visible in the URL. When the user submits the form with the method set as the POST Method.
- $_REQUEST – Its combined array containing values from the $_GET, $_POST and $_COOKIE global variables.
- $_ENV – Get/Set an associative array of variables passed to the current script via the environment method...
- $_FILES – Get an associative array of items uploaded to the current script via the HTTP POST method. Used to upload the file, image, and videos, etc on the server. It contains all the information i.e name, extension, temporary name, etc.
- $_SERVER – It saves all information related to headers, paths, and script locations.
- $_SESSION – It contains session variables available to the current script.
- $GLOBALS – PHP super global variable which is used to access global variables from anywhere in the PHP script. Contains all the global variables associated with the current script.
Q35. What are the advantages of PHP MySQL?
Ans: It is a stable, reliable and powerful solution with advanced features like the following:
- Data Security
- On-Demand Scalability
- High Performance
- Round-the-clock Uptime
- Comprehensive Transactional Support
- Complete Workflow Control
- Reduced Total Cost of Ownership
- The Flexibility of Open Source
Related Interview Questions...