What will be the result of $a?

Asked

Viewed 132 times

3

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

Here is an excerpt from a question:

Given an excerpt of code written in PHP language, as the below:

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

The variable $a will contain :

a) and

b) and, a, b, c, d

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

d) a, b, c, d, and

e) the array will receive a null value

So what would be the correct answer? Would it be the letter C? Why is it wrong, when I try to echo $a.

  • not to echo an array so friendly, you must identify the position of the array to be printed so "echo $a[0]"

  • 1

    all right, that’s what websites are for, we’re all here to learn and improve.

2 answers

9


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


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

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

You indicate that $a is a array which will have 4 positions, with 4 other indices (0...3), and is then interpreted, thus:

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, at the last valid position of array, to string 'and':

$a[] = "e";

Soon $a will contain all 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)

About you not being able to access array, making echo $a, it is because $a is a non-scale, composite type, different from a simple string. In other words, it takes something else to manipulate arrays.

How did you not create the array with the manual positions, the array followed the pattern and was adding the data, always after the last valid position. Then 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 at 0 until 4), as follows:

echo $a[0]; // imprime a

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

4

My two cents for 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"
}

which is the same thing as using the function array_push, 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);

Upshot

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

    This is called learning. Making a mistake is normal. I hope you’ve learned some of this, so it will be worth it. Good luck!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.