1. Which function returns the number of elements in an array?
The count() function returns the number of elements in an array or object implementing Countable.
count($arr);
Get the Preplance app for a seamless learning experience. Practice offline, get daily streaks, and stay ahead with real-time interview updates.
Get it on
Google Play
4.9/5 Rating on Store
PHP · Question Set
Arrays & Strings interview questions for placements and exams.
Questions
14
Included in this set
Subject
PHP
Explore more sets
Difficulty
Mixed
Level of this set
Go through each question and its explanation. Use this set as a focused practice pack for PHP.
The count() function returns the number of elements in an array or object implementing Countable.
count($arr);
For complete preparation, combine this set with full subject-wise practice for PHP. You can also explore other subjects and sets from the links below.
implode() joins array elements into a string, and explode() splits a string into an array based on a delimiter.
$s = implode(',', $arr); $a = explode(',', $s);String interpolation means embedding variables inside double-quoted strings. PHP replaces variables with their values. It doesn't work in single quotes.
$name='John'; echo "Hello $name"; // prints Hello John
PHP arrays can be indexed using numbers, associative using keys, or multidimensional for nested data.
$arr = array('a','b'); $map = array('name'=>'John');in_array() searches for a value in an array and returns true if found, false otherwise.
in_array('apple', $fruits);array_key_exists() checks if a key exists in an array. isset() also works but returns false for null values.
array_key_exists('name', $user);strlen() returns the number of characters in a string, including spaces.
strlen('Hello');substr() extracts part of a string from a start position and optional length.
substr('Hello',1,3); // ellAn associative array uses named keys instead of numeric indexes. It is useful for mapping data like name to value pairs.
$user = array('name'=>'John','age'=>25);A multidimensional array contains one or more arrays inside it. It helps represent complex data like tables. Access elements using multiple indexes.
$matrix = [[1,2,3],[4,5,6]]; echo $matrix[1][2];
Common string functions include strlen for length, strtolower and strtoupper for case conversion, str_replace for replacement, and strpos for searching a substring.
strpos('hello world','world');You can loop through arrays using foreach or for loops. foreach is best for associative arrays. You can access both key and value easily.
foreach($arr as $key=>$val){ echo $key.'='.$val; }Arrays can be created using the array() function or square brackets from PHP 5.4 onward.
$a = array(1,2,3); $b = [1,2,3];
array_merge() joins arrays. If keys repeat, later values overwrite earlier ones for string keys.
$result = array_merge($a,$b);