PHP Interview Questions

PHP Interview Questions

There is given PHP interview questions and answers that have been asked in many companies. Let's see the list of top PHP interview questions.

1) What is PHP?

PHP stands for Hypertext Preprocessor. It is an open source server-side scripting language which is widely used for web development. It supports many databases like MySQL, Oracle, Sybase, Solid, PostgreSQL, generic ODBC etc.

More Details...


2) What is PEAR in PHP?

PEAR is a framework and repository for reusable PHP components. PEAR stands for PHP Extension and Application Repository. It contains all types of PHP code snippets and libraries.

It also provides a command line interface to install "packages" automatically.


3) Who is known as the father of PHP?

Rasmus Lerdorf


4) What was the old name of PHP?

The old name of PHP was Personal Home Page.


5) Explain the difference b/w static and dynamic websites?

In static websitescontent can't be changed after running the script. You can't change anything on the site. It is predefined.

In dynamic websitescontent of script can be changed at the run time. Its content is regenerated every time a user visit or reload. Google, yahoo and every search engine is the example of dynamic website.

ADVERTISEMENT


6) What is the name of scripting engine in PHP?

The scripting engine that powers PHP is called Zend Engine 2.


7) Explain the difference between PHP4 and PHP5.

PHP4 doesn't support oops concept and uses Zend Engine 1.

PHP5 supports oops concept and uses Zend Engine 2.


8) What are the popular Content Management Systems (CMS) in PHP?

  • WordPress: WordPress is a free and open-source content management system (CMS) based on PHP & MySQL. It includes a plug-in architecture and template system. It is mostly connected with blogging but supports another kind of web content, containing more traditional mailing lists and forums, media displays, and online stores.
  • Joomla: Joomla is a free and open-source content management system (CMS) for distributing web content, created by Open Source Matters, Inc. It is based on a model-view-controller web application framework that can be used independently of the CMS.
  • Magento: Magento is an open source E-trade programming, made by Varien Inc., which is valuable for online business. It has a flexible measured design and is versatile with many control alternatives that are useful for clients. Magento utilizes E-trade stage which offers organization extreme E-business arrangements and extensive support network.
  • Drupal: Drupal is a CMS platform developed in PHP and distributed under the GNU (General Public License).

9) What are the popular frameworks in PHP?

  • CakePHP
  • CodeIgniter
  • Yii 2
  • Symfony
  • Zend Framework etc.

10) Which programming language does PHP resemble to?

ADVERTISEMENT

Advertisement report

PHP has borrowed its syntax from Perl and C.


11) List some of the features of PHP7.

  • Scalar type declarations
  • Return type declarations
  • Null coalescing operator (??)
  • Spaceship operator
  • Constant arrays using define()
  • Anonymous classes
  • Closure::call method
  • Group use declaration
  • Generator return expressions
  • Generator delegation
  • Space ship operator

12) What is "echo" in PHP?

PHP echo output one or more string. It is a language construct not a function. So the use of parentheses is not required. But if you want to pass more than one parameter to echo, the use of parentheses is required.

Syntax:

  1. void echo ( string $arg1 [, string $... ] )  
More details...

13) What is "print" in PHP?

PHP print output a string. It is a language construct not a function. So the use of parentheses is not required with the argument list. Unlike echo, it always returns 1.

Syntax:

  1. int print ( string $arg)  
More details...

14) What is the difference between "echo" and "print" in PHP?

ADVERTISEMENT

Echo can output one or more string but print can only output one string and always returns 1.

Echo is faster than print because it does not return any value.


15) How a variable is declared in PHP?

A PHP variable is the name of the memory location that holds data. It is temporary storage.

Syntax:

  1. $variableName=value;  
More details...

16) What is the difference between $message and $$message?

$message stores variable data while $$message is used to store variable of variables.

$message stores fixed data whereas the data stored in $$message may be changed dynamically.

More Details...

17) What are the ways to define a constant in PHP?

PHP constants are name or identifier that can't be changed during execution of the script. PHP constants are defined in two ways:

  • Using define() function
  • Using const() function
More details...

18) What are magic constants in PHP?

PHP magic constants are predefined constants, which change based on their use. They start with a double underscore (__) and end with a double underscore (__).

More Details...

19) How many data types are there in PHP?

ADVERTISEMENT

PHP data types are used to hold different types of data or values. There are 8 primitive data types which are further categorized in 3 types:

  • Scalar types
  • Compound types
  • Special types
More Details...

20) How to do single and multi line comment in PHP?

PHP single line comment is made in two ways:

  • Using // (C++ style single line comment)
  • Using # (Unix Shell style single line comment)

PHP multi-line comment is made by enclosing all lines within.

More details...

21) What are the different loops in PHP?

For, while, do-while and for each.


22) What is the use of count() function in PHP?

The PHP count() function is used to count total elements in the array, or something an object.


23) What is the use of header() function in PHP?

The header() function is used to send a raw HTTP header to a client. It must be called before sending the actual output. For example, you can't print any HTML element before using this function.


24) What does isset() function?

The isset() function checks if the variable is defined and not null.

ADVERTISEMENT


25) Explain PHP parameterized functions.

PHP parameterized functions are functions with parameters. You can pass any number of parameters inside a function. These given parameters act as variables inside your function. They are specified inside the parentheses, after the function name. Output depends upon dynamic values passed as parameters into the function.

More details...

26) Explain PHP variable length argument function

PHP supports variable length argument function. It means you can pass 0, 1 or n number of arguments in function. To do this, you need to use 3 ellipses (dots) before the argument name. The 3 dot concept is implemented for variable length argument since PHP 5.6.

More details...

27) Explain PHP variable length argument function.

PHP supports variable length argument function. It means you can pass 0, 1 or n number of arguments.


28) What is the array in PHP?

An array is used to store multiple values in a single value. In PHP, it orders maps of pairs of keys and values. It saves the collection of the data type.

More Details...

29) How many types of array are there in PHP?

There are three types of array in PHP:

  1. Indexed array: an array with a numeric key.
  2. Associative array: an array where each key has its specific value.
  3. Multidimensional array: an array containing one or more arrays within itself.

30) Explain some of the PHP array functions?

ADVERTISEMENT

There are many array functions in PHP:

  • array()
  • array_change_key_case()
  • array_chunk()
  • count()
  • sort()
  • array_reverse()
  • array_search()
  • array_intersect()
More details...

31) What is the difference between indexed and associative array?

The indexed array holds elements in an indexed form which is represented by number starting from 0 and incremented by 1. For example:

  1. $season=array("summer","winter","spring","autumn");    

The associative array holds elements with name. For example:

  1. $salary=array("Sonoo"=>"350000","John"=>"450000","Kartik"=>"200000");    

More Details...


32) How to get the length of string?

The strlen() function is used to get the length of the string.

More Details...

33) Explain some of the PHP string functions?

There are many array functions in PHP:

  • strtolower()
  • strtoupper()
  • ucfirst()
  • lcfirst()
  • ucwords()
  • strrev()
  • strlen()
More details...

34) What are the methods to submit form in PHP?

ADVERTISEMENT

There are two methods GET and POST.

More Details...

35) How can you submit a form without a submit button?

You can use JavaScript submit() function to submit the form without explicitly clicking any submit button.


36) What are the ways to include file in PHP?

PHP allows you to include file so that page content can be reused again. There are two ways to add the file in PHP.

  1. include
  2. require
More details...

37) Differentiate between require and include?

Require and include both are used to include a file, but if data is not found include sends warning whereas require sends Fatal error.

More Details...

38) Explain setcookie() function in PHP?

PHP setcookie() function is used to set cookie with HTTP response. Once the cookie is set, you can access it by $_COOKIE superglobal variable.

Syntax:

  1. bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path     
  2. [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )    
More details...

39) How can you retrieve a cookie value?

  1. echo $_COOKIE ["user"];  
More Details...

40) What is a session?

ADVERTISEMENT

PHP Engine creates a logical object to preserve data across subsequent HTTP requests, which is known as session.

Sessions generally store temporary data to allow multiple PHP pages to offer a complete functional transaction for the same user.

Simply, it maintains data of an user (browser).

More Details...

41) What is the method to register a variable into a session?

  1. <?php  
  2. Session_register($ur_session_var);  
  3. ?>  

42) What is $_SESSION in PHP?

A session creates a file in a temporary directory on the server where registered session variables and their session id are stored. This data will be available to all pages on the site amid that visit.

The area of the temporary record is controlled by a setting in the php.ini document called session.save_path.

At the point when a session is begun following things happen -

  1. PHP first makes two duplicates of one of a kind session id for that particular session of the client which is an arbitrary string of 32 hexadecimal numbers, for example, 3c7foj34c3jjhkyepop2fc937e3443.
  2. One copy of unique session id automatically sent to the user?s computer for the sake of synchronization in future ahead, and one copy is being maintained at server side till the session is running.
  3. Whenever you want to access the page of website or web app, then session id of the current user will be associated with the HTTP header, and that will be compared by the session id which is being maintained at the server. After completing the comparison process, you can easily access the page of the website or web app
  4. A session ends when the user closes the browser, or after leaving the site, the server will terminate the session after a predetermined period, commonly 30 minutes duration.

43) What is PHP session_start() and session_destroy() function?

ADVERTISEMENT

PHP session_start() function is used to start the session. It starts new or resumes the current session. It returns the current session if the session is created already. If the session is not available, it creates and returns new sessions.

More details...

44) What is the difference between session and cookie?

The main difference between session and cookie is that cookies are stored on user's computer in the text file format while sessions are stored on the server side.

Cookies can't hold multiple variables, on the other hand, Session can hold multiple variables.

You can manually set an expiry for a cookie, while session only remains active as long as browser is open.


45) Write syntax to open a file in PHP?

PHP fopen() function is used to open file or URL and returns resource. It accepts two arguments: $filename and $mode.

Syntax:

  1. resource fopen ( string $filename , string $mode [, bool $use_include_path = false [, resource $context ]] )  
More details...

46) How to read a file in PHP?

PHP provides various functions to read data from the file. Different functions allow you to read all file data, read data line by line, and read data character by character.

PHP file read functions are given below:

  • fread()
  • fgets()
  • fgetc()
More details...

47) How to write in a file in PHP?

ADVERTISEMENT

PHP fwrite() and fputs() functions are used to write data into file. To write data into a file, you need to use w, r+, w+, x, x+, c or c+ mode.

More details...

48) How to delete file in PHP?

The unlink() function is used to delete a file in PHP.

  1. bool unlink (string $filename)      

More Details...


49) What is the method to execute a PHP script from the command line?

You should just run the PHP command line interface (CLI) and specify the file name of the script to be executed as follows.


50) How to upload file in PHP?

The move_uploaded_file() function is used to upload file in PHP.

  1. bool move_uploaded_file ( string $filename , string $destination )    

More Details...


51) How to download file in PHP?

The readfile() function is used to download the file in PHP.

  1. int readfile ( string $filename )     

More Details...


52) How can you send email in PHP?

The mail() function is used to send email in PHP.

ADVERTISEMENT

  1. bool mail($to,$subject,$message,$header);    

More Details...


53) How do you connect MySQL database with PHP?

There are two methods to connect MySQL database with PHP. Procedural and object-oriented style.

More Details...

54) How to create connection in PHP?

The mysqli_connect() function is used to create a connection in PHP.

  1. resource mysqli_connect (server, username, password)       

More Details...


55) How to create database connection and query in PHP?

Since PHP 4.3, mysql_reate_db() is deprecated. Now you can use the following 2 alternatives.

  • mysqli_query()
  • PDO::_query()
More details...

56) How can we increase execution time of a PHP script?

By default, the maximum execution time for PHP scripts is set to 30 seconds. If a script takes more than 30 seconds, PHP stops the script and returns an error.

You can change the script run time by changing the max_execution_time directive in the php.ini file.

When a script is called, set_time_limit function restarts the timeout counter from zero. It means, if default timer is set to 30 sec, and 20 sec is specified in function set_time_limit(), then script will run for 45 seconds. If 0sec is specified in this function, script takes unlimited time.


57) What are the different types of errors in PHP?

ADVERTISEMENT

There are 3 types of error in PHP.

  1. Notices:These are non-critical errors. These errors are not displayed to the users.
  2. Warnings:These are more serious errors, but they do not result in script termination. By default, these errors are displayed to the user.
  3. Fatal Errors:These are the most critical errors. These errors may cause due to immediate termination of script.

58) How to stop the execution of PHP script?

The exit() function is used to stop the execution of PHP script.


59) What are the encryption functions in PHP?

CRYPT() and MD5()


60) What is htaccess in PHP?

The .htaccess is a configuration file on Apache server. You can change configuration settings using directives in Apache configuration files like .htaccess and httpd.conf.


61) Explain PHP explode() function.

The PHP explode() function breaks a string into an array.


62) Explain PHP split() function.

The PHP split() function splits string into an array by regular expression.


63) How can we get IP address of a client in PHP?

  1. $_SERVER["REMOTE_ADDR"];  

64) What is the meaning of a Persistent Cookie?

A persistent cookie is permanently stored in a cookie file on the browser's computer. By default, cookies are temporary and are erased if we close the browser.

ADVERTISEMENT


65) What is the use of the function 'imagetypes()'?

imagetypes() gives the image format and types supported by the current version of GD-PHP.


66) What are include() and require() functions?

The Include() function is used to put data of one PHP file into another PHP file. If errors occur, then the include() function produces a warning but does not stop the execution of the script, and it will continue to execute.

The Require() function is also used to put data of one PHP file to another PHP file. If there are any errors, then the require() function produces a warning and a fatal error and stops the script.


67) What is Cookies? How to create cookies in PHP?

A cookie is used to identify a user. A cookie is a little record that the server installs on the client's Computer. Each time a similar PC asks for a page with a program, it will send the cookie as well. With PHP, you can both make and recover cookie value.

Some important points regarding Cookies:

  1. Cookies maintain the session id generated at the back end after verifying the user's identity in encrypted form, and it must reside in the browser of the machine
  2. You can store only string values not object because you can't access any object across the website or web apps
  3. Scope: - Multiple pages.
  4. By default, cookies are temporary and transitory cookie saves in the browser only.
  5. By default, cookies are URL particular means Gmail isn't supported in Yahoo and the vice versa.
  6. Per site 20 cookies can be created in one website or web app
  7. The Initial size of the cookie is 50 bytes.
  8. The Maximum size of the cookie is 4096 bytes.

68) What is the Importance of Parser in PHP?

ADVERTISEMENT

PHP parser parses the PHP developed website from the opening to the closing tag. Tags indicate that from where PHP code is being started and ended. In other words, opening and closing tags decide the scope of PHP scripting syntax of closing tag in PHP

<?php syntax of opening tag in PHP
?> syntax of closing tag in PHP


69) How can we create a database using PHP and MySQL?

The necessary steps to create a MySQL database using PHP are:

  • Establish a connection to MySQL server from your PHP script.
  • If the connection is successful, write a SQL query to create a database and store it in a string variable.
  • Execute the query.

0 Comments:

Post a Comment

Need Help ? Make A Comment Below & We'll Help You Out ?.....:)