PHP Interview Questions

php interview

A constructor allows you to initialize an object's properties upon creation of the object. If you create a __construct() function, PHP will automatically call this function when you create an object from a class. Notice that the construct function starts with two underscores (__)! } function get_name() { return $this->name; } } $apple = new Fruit("Apple"); echo $apple->get_name(); ?>
A destructor is called when the object is destructed or the script is stopped or exited. If you create a __destruct() function, PHP will automatically call this function at the end of the script. Notice that the destruct function starts with two underscores (__)! The example below has a __construct() function that is automatically called when you create an object from a class, and a __destruct() function that is automatically called at the end of the script:

Explode()

  • The PHP explode() function breaks a string into an array. The explode() function is used to split a string into an array and implode() function is used to make a string by combining the array elements.
  • output: Array ( [0] => Hello [1] => world. [2] => It's [3] => a [4] => beautiful [5] => day. )
  • more info: https://www.w3schools.com/php/func_string_explode.asp

Implode()

 
  • The array_filter() function filters the values of an array using a callback function.
  • This function passes each value of the input array to the callback function. If the callback function returns true, the current value from input is returned into the result array. Array keys are preserved.
  • Syntax array_filter(array, callbackfunction, flag)
  • More info: https://www.w3schools.com/php/func_array_filter.asp
  • The array_map() function sends each value of an array to a user-made function and returns an array with new values, given by the user-made function.
  • Syntax array_map(myfunction, array1, array2, array3, ...)
  • More: https://www.w3schools.com/php/func_array_map.asp
Traits are declared with the trait keyword:

Syntax

trait TraitName { // some code... } ?>
To use a trait in a class, use the use keyword:

Syntax

class MyClass { use TraitName; } ?>
Let's look at an example:

Example

trait message1 { public function msg1() { echo "OOP is fun! "; } } class Welcome { use message1; } $obj = new Welcome(); $obj->msg1(); ?>
https://www.w3schools.com/php/php_oop_static_methods.asp
In PHP, there are three types of arrays:
  • Indexed arrays - Arrays with a numeric index $fruits = array("Apple", "Banana", "Orange");
  • Associative arrays - Arrays with named keys $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
  • Multidimensional arrays - Arrays containing one or more arrays

Comments are closed.