Home > other >  What is string with square bracket in PHP
What is string with square bracket in PHP

Time:09-08

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?

CodePudding user response:

['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.

CodePudding user response:

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.

  •  Tags:  
  • php
  • Related