dejitay836
dejitay836

Reputation: 85

What is string with square bracket in PHP

I saw the documentation of WordPress with string[], what is this means?

It said "An array of...", but then the label for this is string[] instead of array.

I know the string in PHP is either 'Hello' or "Hello", what is ['Hello']? Is this really a string?

Upvotes: 1

Views: 39

Answers (2)

Barmar
Barmar

Reputation: 780984

type[] is the documentation convention that means "array whose elements are all type, so string[] means "array of strings". ["Hello", "World"] is an example value.

Upvotes: 1

Hartmut Holzgraefe
Hartmut Holzgraefe

Reputation: 2765

['Hello'] is an array with just one element that happens to be of type string. So if you e.g. do

  $a = ['Hello'];
  echo $a[0]."\n";

the output will be Hello.

string[] is basically saying: an array that only has strings stored in its element, no other types like e.g. int

So e.g.

  ['abc','xyz']

would be a valid string[] while

  ['abc', 123, 'xyz', 456]

would not be as it contains both string and int elements.

Upvotes: 0

Related Questions