What will be the result of $ a?

4

I was studying PHP and seeing questions, to improve my knowledge in the area.

Here is an excerpt of an issue:

  

Given a snippet of code written in the PHP language, like the one below:

$a = array("a", "b", "c", "d");
$a[] = "e";

The variable $a will contain:

  

a) and

     

b) e, a, b, c, d

     

c) it is not possible to assign a value to an array in this way

     

d) a, b, c, d, e

     

e) the array will receive a null value

So what would be the correct answer? Is it the letter C? Because it gives error, when I try to do echo $ a.

    
asked by anonymous 18.08.2017 / 21:40

2 answers

8

I believe you removed the code from here . And I also believe that you have not finished reading the document, because at the end all the answers are shown, which in this case corresponds to letter D , but let's go by parts.

The correct answer is the letter D . You can see - as stated in the PHP documentation , - that an array is an ordered data structure representing a collection of elements, and can be started in this way. Then on the first line :

$a = array("a", "b", "c", "d");

You indicate that $a is an array that will have 4 positions, with 4 other indexes (0 ... 3), being interpreted as follows:

array (size=4)
  0 => string 'a' (length=1)
  1 => string 'b' (length=1)
  2 => string 'c' (length=1)
  3 => string 'd' (length=1)

In the second line , you instruct PHP to add, in the last valid position of the array string p>

$a[] = "e";

Soon $a will contain all the letters 'a', 'b', 'c', 'd', 'e'.

array (size=5)
   0 => string 'a' (length=1)
   1 => string 'b' (length=1)
   2 => string 'c' (length=1)
   3 => string 'd' (length=1)
   4 => string 'e' (length=1)

When you can not access the array , making echo $a , it is because $a is a non-scalar, composite type, other than a simple string . In other words, you need something more to manipulate arrays .

Since you did not create the array with the positions manually, the array followed the pattern and added the data, always after the last valid position. So to access the array $a , you should do something similar to this:

echo $a[posição];

In case, as your array , has a size of 5, you can access any of the positions (starting from 0 to 4), as follows:

echo $a[0]; // imprime a

If you try to do something like echo $a , a notice will be generated, saying: Array to string conversion . Read more about this here .

    
18.08.2017 / 21:42
4

My two cents for the discussion.

The correct answer is alternative D. The result of the operation:

<?php

$a = ["a", "b", "c", "d"];

$a[] = "e";

var_dump($a);

is

array(5) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
  [3]=>
  string(1) "d"
  [4]=>
  string(1) "e"
}

This is the same as using the array_push function , inserting elements at the end of the array. In this case, only one:

<?php

$a = array("a", "b", "c", "d");
array_push($a, "e");

var_dump($a);

Result

array(5) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
  [3]=>
  string(1) "d"
  [4]=>
  string(1) "e"
}
    
18.08.2017 / 21:58