Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

201.

How to get last character of string in php?

Answer»

To FIND the last CHARACTER of a PHP string, use the substr() function in the following way:

Example

substr("best INTERVIEW QUESTION", -1);

OUTPUT:
n

202.

List the most commonly use string functions in PHP?

Answer»

Here is a list of the most commonly used PHP STRING FUNCTIONS while programming:

  • substr() function: This function is used in PHP to give you ACCESS to a substring having a specific start and endpoints within the string.
    Syntax:
    string substr(string string, int start[, int length] );
  • strlen() function: This function is used in PHP to check the character length of a string.
    Syntax:
    echo strlen(“string”)
  • str_replace() function: This function is used in PHP as a find and replace function within any string.
    Syntax: str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) : mixed
  • trim() function: This function is used in PHP to trim all the whitespace from the starting point to the endpoint in any
    string.
    Syntax: trim ( string $str [, string $character_mask = " \t\n\r\0\x0B" ] ) : string
  • strpos() function: This function is used in PHP to find the first occurrence of a substring within a string.
    Syntax: strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) : int
    In the above syntax, it actually COMMANDS PHP to find the first occurrence of the substring needle in the string haystack.
  • strtolower() function: This function is used in PHP to convert all the characters in a string to lowercase.
    Syntax: strtolower ( string $string ) : string
  • strtoupper() function: Well, obviously this function is used to convert all the characters in a string to the upper case.
    Syntax: strtoupper ( string $string ) : string

In the above COMMAND, it returns with part of the haystack string starting from the point of the needle to the end of the haystack.

203.

How to repeat a string to a specific number of times in php?

Answer»

In PHP, the str_repeat() function, which is INBUILT is used to REPEAT a string for a specific number of times. This str_repeat() function creates new strings by repeating the GIVEN string ‘n’ number of times as specified.

Here is the syntax of the str_repeat() function: str_repeat(string, repeat)

Example

Here is an example demonstrating the same:
echo str_repeat("BIQ ", 2);

Output

BIQ BIQ

204.

How to get second last element of array in php?

Answer»

The count() FUNCTION is assuming that the INDEXES of your array GO in order; by WAY of the usage of stop and PREV to go the array pointer, you get the genuine values. Try the use of the count() method on the array above and it will fail.

Example

$array = array(5,6,70,10,36,2);
echo $array[count($array) -2];

// OUTPUT : 36

205.

How to store fetch data in array in php?

Answer»
206.

How to print array values in php?

Answer»

To print all the values inside an array in PHP, use the for each loop.

Here’s an example to demonstrate it:

$colors = array("RED", "Green", "BLUE", "Yellow", "ORANGE");

// Loop through colors array foreach($colors as $value){ ECHO $value . "
"; }

Output:

Red
Green
Blue
Yellow
Orange

207.

How to get array key value in php?

Answer»

The array_keys() FUNCTION is used in PHP to RETURN with an ARRAY containing keys.
Here is the syntax for the array_keys() function: array_keys(array, value, strict)

Here is an example to demonstrate its use

$a=array("VOLVO"=>"AAAAAAAA","BMW"=>"BBBBBBB","Toyota"=>"CCCCCC");
print_r(array_keys($a));

Output:

Array ( [0] => Volvo [1] => BMW [2] => Toyota )

208.

How to get common values from two array in php?

Answer»

To select the common data from two arrays in PHP, you need to use the array_intersect() function. It compares the value of two given arrays, MATCHES them and RETURNS with the common values. Here’s an example:

$a1=ARRAY("a"=>"red","b"=>"green","c"=>"BLUE","d"=>"YELLOW");
$a2=array("e"=>"red","f"=>"black","g"=>"purple");
$a3=array("a"=>"red","b"=>"black","h"=>"yellow");

$result=array_intersect($a1,$a2,$a3); print_r($result);

Output:

Array ( [a] => red )

209.

How to change array index in PHP?

Answer»

$array = array('1' => 'best', '2' => 'interview', '3' => 'question')
$array[3] = $array[5];
unset($array[3]);

// OUTPUT
array('1' => 'best', '2' => 'interview', '5' => 'question')

210.

How to add key and value in array in php?

Answer»

In PHP, pushing a VALUE inside an ARRAY automatically creates a numeric key for itself. When you are adding a value-key pair, you actually already have the key, so, pushing a key again does not make sense. You only NEED to set the value of the key inside the array. Here’s an example to correctly PUSH a [air pf value and key inside an array:

$data = array(
   "name" => "Best Interview Questions"
);

array_push($data['name'], 'Best Interview Questions');

Now, here to push “cat” as a key with “wagon” as the value inside the array $data, you need to do this:

$data['name']='Best Interview Questions';

211.

How to get single value from array in php?

Answer»

In PHP, if you want to ACCESS a single VALUE from an array, be it a numeric, indexed, multidimensional or associative array, you NEED to use the array index or key.
Here’s an example using a multidimensional array:

Example

$superheroes = array(
array(
"name" => "Name",
"character" => "BestInterviewQuestion",
),
array(
"name" => "CLASS",
"character" => "MCA",
),
array(
"name" => "ROLL",
"character" => "32",
)
);
ECHO $superheroes[1]["name"];

 

Output:

CLASS

212.

What is associative array in PHP?

Answer»

Associative arrays are used to store key and VALUE pairs. For example, to SAVE the marks of the unique subjects of a SCHOLAR in an array, a numerically listed array would no longer be a nice choice.

Example

/* Method 1 */
$student = array("key1"=>95, "key2"=>90, "key3"=>93);

/* Method 2 */
$student["key1"] = 95;
$student["key2"] = 90;
$student["key3"] = 93

213.

Which array function checks if the particular key exists in the array?

Answer»

In PHP arrays, the array_key_exists() FUNCTION is used when the USER wants to check if a specific key is present INSIDE an array. If the function returns with a TRUE VALUE, it is present.

Here’s its syntax: array_key_exists(array_key, array_name)

Also, here is an example of how it works

Example

$array1 = array("Orange" => 100, "Apple" => 200, "Grapes" => 300, "Cherry" => 400);
if (array_key_exists("Grapes",$array1))
{
echo "Key exists";
}
else
{
echo "Key does not exist";
}

Output:

key exists

214.

What is the difference between array_merge() and array_merge_recursive() in php?

Answer»
array_merge()array_merge_recursive()
This function is used to join one or more arrays into a single array.Used to merge multiple arrays in such a manner that values of one array are appended to the end of the previous array.
Syntax: array_merge($array1, $array2, $array3...);Syntax: array_merge_recursive($array1, $array2, $array3...);
If we pass a single array, it will return re-indexed as a NUMERIC array whose INDEX STARTS from zero.If we pass a single array, it will return re-indexed in a continuous manner.
Example Example of an array_merge() function:

$arrayFirst = array("Topics" => "Maths","SCIENCE", "Computers");
$arraySecond = array("Class-V", "Class-VI", "Section"=>"C");
$resultArray = array_merge($arrayFirst, $arraySecond);
print_r($resultArray);

Output

Array ( [Topics] => Maths [0] => Science [1] => Computers [2] => Class-V [3] => Class-VI [Section] => C )

Example of an array_merge_recursive() function with all different keys:

$a1=array("a"=>"Best", "b"=>"Interview");
$a2=array("z"=>"Question");
print_r(array_merge_recursive($a1, $a2));

Output

: Array
(
[a] => Best
[b] => Interview
[z] => Question
)

215.

What are the difference between array_keys() and array_key_exists() in php?

Answer»

array_key_exists() : It is used to checks an ARRAY for a particular key and returns TRUE if the key exists and FALSE if the key does not exist.

array_keys() : This FUNCTION returns an array containing the keys. It TAKES three parameters out of which one is mandatory and other two are optional.
Syntax : array_keys(array,value,strict)

216.

How to check a variable is array or not in php?

Answer»

We can check a variable with the help of is_array() FUNCTION in PHP. It's return true if variable is an ARRAY and return FALSE otherwise.

Example

$var = array('X','Y','Z');
if (is_array($var))
echo 'It is an array.';
else
echo 'It is not an array.';

 

217.

What is the different between count() and sizeof() in php?

Answer»

Both are used to count elements in a array.sizeof() function is an alias of count() function used in PHP. count() function is FASTER and BUTTER than sizeof().

ALSO Read: Basic Knowledge of PHP which helps in cracking InterviewExample

$array = array('1','2','3','4');
$size = count($array);

//OUTPUT : 4

218.

How do you remove duplicates from an array?

Answer»

We can use the PHP array_unique() FUNCTION to remove the duplicate vlaues form an array.

Example

$array = array("a"=>"best","b"=>"interviewquestion","c"=>"best");
print_r(array_unique($array));

// OUTPUT : Array ( [a] => best [b] => interviewquestion)

219.

How to get elements in reverse order of an array in php?

Answer»

For this we can use array_reverse() function.

Example

$array = array("1"=>"BEST","2"=>"Interview","3"=>"Question");
print_r(array_reverse($array));

// OUTPUT Array ( [3] => Question [2] => Interview[1] => Best)

220.

What is the use of array_search() in php?

Answer»

array_search() is a inbuilt FUNCTION of PHP which is used to search a PARTICULAR value in an array and if the value is FOUND then it returns its corresponding its key.

Example

$array = array("1"=>"My", "2"=>"Name", "3"=>"is", "4"=>"BestInterviewQuestion");
ECHO array_search("BestInterviewQuestion",$array);

// OUTPUT 4

221.

What is explode() in php?

Answer»

PHP EXPLODE() function is USED to BREAK a string into an array.

Also Read: What is the difference between PHP5 and php7Example

$string = "My Name Is BestInterviewQuestion";
print_r (explode(" ",$string));

// OUTPUT : Array ( [0] => My [1] => Name [2] => Is [3] => BestInterviewQuestion )

222.

What is implode() in php?

Answer»

PHP implode() function is used join ARRAY elements with a string.In other words we can say it returns a string from the elements of an array.Example

$array = array('My','NAME','Is','BestInterViewQuestion');
echo implode(" ",$array)


// OUTPUT : My Name Is BestInterViewQuestion

223.

What is the use of is_array() and in_array()?

Answer»

is_array() : It is an inbuilt function USED in PHP. It is used to CHECK WHETHER a variable is an array or not.

in_array() : It is used to check whether a given value exists in an array or not. It returns TRUE if the value is exists in array, and returns FALSE otherwise.

224.

How to display array structure and values in PHP?

Answer»

You can either use the PHP var_dump() or print_r() FUNCTION to check the structure and VALUES of an ARRAY. The var_dump() method gives more information than print_r().

Example

$LISTS = array("BEST", "Interview", "Questions");
// Print the array
print_r($lists);
var_dump($lists);

225.

What is the difference between associative array and indexed array?

Answer»
Associative ArraysIndexed or Numeric Arrays
This is a type of arrays which used named specific KEYS to assign and store values in the database.This type of arrays store and assign values in a numeric fashion with count STARTING from zero.
Example Example of an associative array:

$name_two["i am"] = "zara";
$name_two["anthony is"] = "Hello";
$name_two["ram is"] = "Ananya";
echo "Accessing elements directly:\n";

echo $name_two["i am"], "\n";
echo $name_two["anthony is"], "\n";
echo $name_one["Ram is"], "\n";

Output:

Accessing elements directly:
zara
Hello
Ananya

Example of an indexed or numeric array:

$name_two[1] = "SONU Singh";
echo "Accessing the array elements directly:\n";
echo $name_two[1], "\n";

Output:

Sonu Singh

226.

Explain different sorting function in PHP?

Answer»
  • SORT() - It is USED to sort an ARRAY in ASCENDING order
  • rsort() - It is used to sort an array in descending order
  • asort() - It is used to sort an associative array in ascending order, ACCORDING to the value
  • ksort() - It is used to sort an associative array in ascending order, according to the key
  • arsort() - It is used to sort an associative array in descending order, according to the value
  • krsort() - It is used to sort an associative array in descending order, according to the key
227.

How to insert an new array element in array?

Answer»

$originalArray = array( 'RAM', 'SITA', 'luxman', 'hanuman', 'ravan' );
$newArray = array( 'kansh' );
array_splice( $originalArray, 3, 0, $newArray);
// OUTPUT is ram sita luxman hanuman kansh ravan

Also Read: Top 10 PROGRAMMING LANGUAGES in 2020
228.

How to get specific key value from array in php?

Answer»

To check key in the array, we can use array_key_exists().

$item=array("NAME"=>"UMESH","CLASS"=>"mca");
if (array_key_exists("name",$item))
{
   echo "Key is exists";
}
ELSE
{
   echo "Key does not exist!";
}

 

 

229.

How to get total number of elements used in array?

Answer»

We can use the count() or sizeof() function to GET the number of ELEMENTS in an ARRAY.

Example

$array1 = array("1","4","3");
echo count($array1);

OUTPUT : 3

230.

What is the use of array_count_values() in php?

Answer»

It is an inbuilt FUNCTION in PHP. It is one of the most simple functions that is used to count all the values inside an array. In other words we can SAY that it is used to CALCULATE the frequency of all of the elements of an array.

Example

$array = array("B","Cat","Dog","B","Dog","Dog","Cat");
print_r(array_count_values($array));

// OUTPUT : Array ( [B] => 2 [Cat] => 2 [Dog] => 3 )

231.

How to use array_merge() and array_combine() in PHP?

Answer»

array_combine()

array_combine() : It is used to create a new ARRAY by using the KEY of one array as keys and using the value of another array as values. The most important thing is using array_combine() that, NUMBER of values in both arrays should be same.

$name = array("best","interview","question");
$index = array("1","2","3");
$result = array_combine($name,$index);

array_merge()

array_merge() : It merges one or more than one array such that the value of one array appended at the END of first array and if the arrays have same strings key then the later value OVERRIDES the previous value for that key .

$name = array("best","interview","question");
$index = array("1","2","3");
$result = array_merge($name,$index);

232.

How to use array_pop() and array_push() in PHP?

Answer»

1. array_pop()

It is used to delete or remove the last element of an ARRAY.

Example

$a=array("BLUE","black","skyblue");
array_pop($a);

OUTPUT : Array ( [0] => blue[1] => black)

2. array_push()

It is used to Insert one or more ELEMENTS to the end of an array.

Example

$a=array("apple","BANANA"); array_push($a,"mango","pineapple");

OUTPUT : Array ( [0] => apple[1] => banana[2] => mango[3] => pineapple)

Also Read: PHP Strings
233.

How many types of array supported in php?

Answer»

PHP SUPPORTS THREE TYPES of arrays:-

  • INDEXED Array
  • Associative array
  • Multi-dimensional Array
234.

How to get first element of array in php?

Answer»

There are various methods in PHP to get the first ELEMENT of an array. Some of the techniques are the use of reset function, array_slice function, array_reverse, array_values, foreach loop, etc.

Example

SUPPOSE we have an array like
$arrayVar = array('best', 'interview', 'question', 'com');

1. With direct accessing the 0th index:
echo $arrayVar[0];

2. With the help of reset()
echo reset($arrayVar);

3. With the help of foreach loop
foreach($arrayVar as $val) {
    echo $val;
    break; // exit from loop
}

235.

What is array filter in PHP?

Answer»

The array_filter() method filters the VALUES of an array the USAGE of a callback function. This method passes each value of the input array to the callback function. If this callback function returns true then the CURRENT value from the input is returned into the result array.

Syntax:

array_filter(array, callbackfunction, flag)

  • array(Required)
  • callbackfunction(Optional)
  • flag(Optional)
Also Read: How to Use TRAITS in PHPExample

function test_odd_number(int $VAR)
{
   return($var & 1);
}
$array=array(1,3,2,3,4,5,6);
print_r(array_filter($array,"test_odd_number"));

236.

How to exclude a route with parameters from CSRF verification?

Answer»

You can cut out a ROUTE from CSRF VERIFICATION by using adding the route to $except property at VerifyCsrfToken middleware.

Example

protected $except = [
     'admin/*/EDIT/*'
];

237.

What are string and array helpers functions in laravel?

Answer»

Laravel INCLUDES a number of global "HELPER" STRING and array functions. These are GIVEN below:-

Laravel Array Helper functions
  • Arr::add()
  • Arr::has()
  • Arr::last()
  • Arr::only()
  • Arr::pluck()
  • Arr::prepend() etc
Laravel String Helper functions
  • Str::after()
  • Str::before()
  • Str::camel()
  • Str::CONTAINS()
  • Str::endsWith()
  • Str::containsAll() etc
238.

How to check if value is sent in request?

Answer»

To CHECK the email VALUE is sent or not in request, you can USE $request->has('email')

Example

if($request->has('email')) {
     // email value is sent from request
} else {
    // email value not sent from request
}

239.

How to check Ajax request in Laravel?

Answer»

You can use the FOLLOWING SYNTAX to CHECK ajax REQUEST in laravel.
if ($request->ajax()) {
     // Now you can write your CODE here.
}

240.

How to get user details when he is logged in by Auth?

Answer» EXAMPLE

USE Illuminate\Support\Facades\AUTH;

$userinfo = Auth::USER();

print_r($userinfo );

241.

What is Guarded Attribute in a Model ?

Answer»

It is the REVERSE of fillable. When a GUARDED SPECIFIES which fields are not mass assignable.

Example

class User extends MODEL {
     protected $guarded = ['user_type'];
}

 

242.

What is fillable in laravel model?

Answer»

It is an array which contains all those fields of table which can be create directly new record in your Database table.

Example

class User EXTENDS MODEL {
        PROTECTED $fillable = ['username', 'password', 'phone'];
}

243.

What is Gulp in laravel?

Answer»

Gulp in Laravel is simply one IMPLEMENTATION that TARGETS to make it comfortable for the users who have RECENTLY acquainted themselves with the gulp in Laravel to be capable to MANAGE their gulp file through ADDING the modules that work efficiently.

244.

What is mix in Laravel?

Answer»

Mix in Laravel renders one fluent API to define WEBPACK creation steps for the application of Laravel that uses various common CSS as WELL as JavaScript PREPROCESSORS. With the help of one easy method chaining, the user is able to fluently define the asset pipeline.

Example

mix.js('resources/js/app.js', 'public/js') // You can ALSO USE your custom folder here
.sass('resources/sass/app.scss', 'public/css');

245.

What is Dusk in Laravel 5?

Answer»

DUSK in Laravel RENDERS ONE expressive and easy-to-use type of browser automation along with testing API. This By default does not demand to install the JDK or Selenium on the device. Instead, it uses one STANDALONE installation of ChromeDriver. However, the users are free for utilizing any of the other compatible drivers of Selenium as per their wishes.

246.

What is Horizon in Laravel 5?

Answer»

Horizon in Laravel is the queue manager. It provides the USER with FULL control of the queues, it renders the means for CONFIGURING how the jobs are PROCESSED and generates analytics, plus performs various tasks related to queue from within one nice DASHBOARD.

247.

What is broadcasting in laravel?

Answer»

The Laravel 5.1 framework comprises functionality named BROADCASTING events. This new functionality makes it quite EASY to build real-time applications in PHP. And with this, an APP will be able to publish the events to a number of real-time cloud-based PubSub solutions, such as Pusher, or Redis. Also, with this functionality called the broadcasting events which is built into the Laravel 5.1, it now became easier creating real-time applications for the PHP developers. This LATEST real-time capability unbars numerous possibilities that were AVAILABLE only to applications written for the other platforms such as Node.js.

248.

Why do we use events and listeners in Laravel?

Answer»

We use events and listeners in the laravel because events give the BASIC observer implementation that allows any user to SUBSCRIBE and listen to multiple events that are triggered in the web APPLICATION. EVERY listeners are STORED in the app/Listeners folder and event class in the laravel is stored in the app/Events folder .

249.

What is Blade laravel?

Answer»

BLADE is very SIMPLE and powerful templating engine that is provided with LARAVEL. Laravel uses "Blade TEMPLATE Engine".

250.

How to add multiple AND conditions in laravel query?

Answer»

We can add multiple AND operator at in a SINGLE where() conditions as well as we can also add separate where() for particular AND condition.

Example DB::table('client')->where('status', '=', 1)->where('name', '=', 'bestinterviewquestion.com')->get();

DB::table('client')->where(['status' => 1, 'name' => 'bestinterviewquestion.com'])->get();