Programming - PHP
6.
What will be the output of the following PHP code?
<?php $name = "Jerry"; echo substr_replace($name, 'Tom', 0, 0); ?>
Answer
The substr_replace() function is used to replace a part of a string with another string. It has the following syntax: substr_replace(string, replacement, start, length) where the start option specifies where to start replacing in the string.
If length is zero, the substr_replace() function inserts the string instead of replacing it.
In the above script, the start parameter is zero, which means that the replacement will start from the beginning. However, here the length parameter is also zero, which means that an insertion operation will be performed instead of replacement.
Answer :
Option CExplanation :
The substr_replace() function is used to replace a part of a string with another string. It has the following syntax: substr_replace(string, replacement, start, length) where the start option specifies where to start replacing in the string.
If length is zero, the substr_replace() function inserts the string instead of replacing it.
In the above script, the start parameter is zero, which means that the replacement will start from the beginning. However, here the length parameter is also zero, which means that an insertion operation will be performed instead of replacement.
7.
What will be the output of the following PHP code?
<?php $fruits = array("apple", "orange", array("grapes", "mango"), "banana"); echo (count($fruits, 1)); ?>
Answer
The function count() will return the number of elements in an array. The parameter 1 counts the array recursively i.e it will count all the elements of multidimensional arrays.
Answer :
Option DExplanation :
The function count() will return the number of elements in an array. The parameter 1 counts the array recursively i.e it will count all the elements of multidimensional arrays.
8.
What will be the output of the following PHP code?
<?php $x = true and false; var_dump($x); ?>
Answer
= operator takes precedence over the and operator in order of operations, so
Answer :
Option AExplanation :
= operator takes precedence over the and operator in order of operations, so
$x = true and false
ends up being functionally equivalent to:$x = true; //sets $x equal to true
true and false; //results in false, but has no affect on anything
9.
What will be the output of the following PHP code?
<?php $text = 'Exam'; $text[7] = 'Adda'; echo $text; ?>
Answer
Answer :
Option A10.
What will be the output of the following PHP code?
<?php $a = range(3, 9); foreach ($a as $b) { switch($b) { case 3: $b = 7; case 7: $b = 3; default: //do nothing } } echo implode('-',$a); ?>
Answer
The range(3,9) gives us an array containing all integers from 3 to 9. When we foreach over them, we can't change the values in the array, so the contents of $a remain unchanged (you need to use the array and the key to update a value during foreach).
Answer :
Option CExplanation :
The range(3,9) gives us an array containing all integers from 3 to 9. When we foreach over them, we can't change the values in the array, so the contents of $a remain unchanged (you need to use the array and the key to update a value during foreach).
Jump to page number :