Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
January 24, 2021 09:14 am GMT

PHP interview questions and answers for web developers

PHP is a general-purpose scripting language especially suited to web development. Here is a list of coding interview questions on PHP to help you get ready for your next technical interview in 2021.

You can check all 82 PHP tech interview questions here

1. What is the use of ini_set()?

Answer:

PHP allows the user to modify some of its settings mentioned in php.ini using ini_set(). This function requires two string arguments. First one is the name of the setting to be modified and the second one is the new value to be assigned to it.

Given line of code will enable the display_error setting for the script if its disabled.

ini_set('display_errors', '1');

We need to put the above statement, at the top of the script so that, the setting remains enabled till the end. Also, the values set via ini_set() are applicable, only to the current script. Thereafter, PHP will start using the original values from php.ini.

Source:github.com/Bootsity

2. What is the difference between == and ===?

Answer:

  • The operator == casts between two different types if they are different
  • The === operator performs a 'typesafe comparison'

That means that it will only return true if both operands have the same type and the same value.

    1 === 1: true    1 == 1: true    1 === "1": false // 1 is an integer, "1" is a string    1 == "1": true // "1" gets casted to an integer, which is 1    "foo" === "foo": true // both operands are strings and have the same value
Enter fullscreen mode Exit fullscreen mode

Source:stackoverflow.com

3. What is the return type of a function that doesn't return anything?

Answer:

void which mean nothing.

Source:github.com/Bootsity

4. What does $GLOBALS mean?

Answer:

$GLOBALS is associative array including references to all variables which are currently defined in the global scope of the script.

Source:guru99.com

5. What are the keys & values in an indexed array?

Answer:

Consider:
Array ( [0] => Hello [1] => world [2] => It's [3] => a [4] => beautiful [5] => day)

The keys of an indexed array are 0, 1, 2 etc. (the index values) and values are "Hello", "world", "It's", "beautiful", "day".

Source:github.com/Bootsity

6. What is the purpose of php.ini file?

Answer:

The PHP configuration file, php.ini, is the final and most immediate way to affect PHP's functionality. The php.ini file is read each time PHP is initialized.in other words, whenever httpd is restarted for the module version or with each script execution for the CGI version.

Source:github.com/Bootsity

7. How can you pass a variable by reference?

Answer:

To be able to pass a variable by reference, we use an ampersand in front of it, as follows:

$var1 = &$var2

Source:guru99.com

8. Is multiple inheritance supported in PHP?

Answer:

PHP supports only single inheritance; it means that a class can be extended from only one single class using the keyword 'extended'.

Source:guru99.com

9. What is stdClass in PHP?

Answer:

stdClass is just a generic 'empty' class that's used when casting other types to objects. stdClass is not the base class for objects in PHP. This can be demonstrated fairly easily:

    class Foo{}    $foo = new Foo();    echo ($foo instanceof stdClass)?'Y':'N'; // outputs 'N'
Enter fullscreen mode Exit fullscreen mode

It is useful for anonymous objects, dynamic properties, etc.

An easy way to consider the StdClass is as an alternative to associative array. See this example below that shows how json_decode() allows to get an StdClass instance or an associative array. Also but not shown in this example, SoapClient::__soapCall returns an StdClass instance.

    //Example with StdClass    $json = '{ "foo": "bar", "number": 42 }';    $stdInstance = json_decode($json);    echo $stdInstance - > foo.PHP_EOL; //"bar"    echo $stdInstance - > &gt number.PHP_EOL; //42    //Example with associative array    $array = json_decode($json, true);    echo $array['foo'].PHP_EOL; //"bar"    echo $array['number'].PHP_EOL; //42
Enter fullscreen mode Exit fullscreen mode

Source:stackoverflow.com

10. In PHP, objects are they passed by value or by reference?

Answer:

In PHP, objects passed by value.

Source:guru99.com

11. What is PDO in PHP?

Answer:

PDO stands for PHP Data Object.

It is a set of PHP extensions that provide a core PDO class and database, specific drivers. It provides a vendor-neutral, lightweight, data-access abstraction layer. Thus, no matter what database we use, the function to issue queries and fetch data will be same. It focuses on data access abstraction rather than database abstraction.

Source:github.com/Bootsity

12. Is there a difference between isset and !empty?

Answer:

empty is more or less shorthand for!isset($foo) || !$foo, and !empty is analogous to isset($foo) && $foo. empty is the same as !$foo, but doesn't throw warnings if the variable doesn't exist. That's the main point of this function: do a boolean comparison without worrying about the variable being set.

Source:stackoverflow.com

13. Differentiate between echo and print()

Answer:

echo and print are more or less the same. They are both used to output data to the screen.

The differences are:

  • echo has no return value while print has a return value of 1 so it can be used in expressions.
  • echo can take multiple parameters (although such usage is rare) while print can take one argument.
  • echo is faster than print.

Source:github.com/Bootsity

14. What is the differences between $a != $b and $a !== $b?

Answer:

!= means inequality (TRUE if $a is not equal to $b) and !== means non-identity (TRUE if $a is not identical to $b).

Source:guru99.com

15. What does the 'var' keyword mean in PHP?

Answer:

It's for declaring class member variables in PHP4, and is no longer needed. It will work in PHP5, but will raise an E_STRICT warning in PHP from version 5.0.0 up to version 5.1.2, as of when it was deprecated. Since PHP 5.3, var has been un-deprecated and is a synonym for 'public'.

Consider:

    class foo {        var $x = 'y'; // or you can use public like...        public $x = 'y'; //this is also a class member variables.        function bar() {        }    }
Enter fullscreen mode Exit fullscreen mode

Source:stackoverflow.com

Thanks for reading and good luck on your next tech interview!

Explore 3800+ dev interview question here Devinterview.io


Original Link: https://dev.to/devinterview/php-interview-questions-and-answers-for-web-developers-4j11

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To