Examveda
Examveda

Which of the following are correct ways of creating an array?
1. state[0] = “karnataka”;
2. $state[] = array(“karnataka”);
3. $state[0] = “karnataka”;
4. $state = array(“karnataka”);

A. 3 and 4

B. 2 and 3

C. Only 1

D. 2, 3 and 4

Answer: Option A

Solution(By Examveda Team)

A variable name should start with $ symbol which is not present in i) and you need not put the square brackets when you use the array() constructor.

Join The Discussion

Comments ( 1 )

  1. John Williams
    John Williams :
    1 year ago

    Answer 2 is legit also. You would be creating an array with single string value 'karnataka' as a value inside the array $state

Related Questions on Arrays

What will be the output of the following PHP code ?

A. Array ( [a] => red [b] => green [c] => blue )

B. Array ( [a] => red [b] => green [c] => blue [d] => yellow )

C. Array ( [e] => red [f] => green [g] => blue )

D. Array ( [a] => red [b] => green [c] => blue [d] => yellow [e] => red [f] => green [g] => blue )

What will be the output of the following PHP code?

A. Array ( [0] => Array ( [1] => Volvo [2] => BMW ) [1] => Array ( [1] => Toyota [2] => Honda ) [2] => Array ( [1] => Mercedes [2] => Opel ) )

B. Array ( [1] => Array ( [1] => Volvo [2] => BMW ) [2] => Array ( [1] => Toyota [2] => Honda ) [3] => Array ( [1] => Mercedes [2] => Opel ) )

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

D. Array ( [0] => Array ( [0] => Volvo [1] => BMW ) [1] => Array ( [0] => Toyota [1] => Honda ) [2] => Array ( [0] => Mercedes [1] => Opel ) )