0

Is there a difference between this syntax of array?

coming from a dd()

$array:3 = [
  0 => "email"
  1 => " email"
  2 => " email"
]

vs

$array = ['email', 'email', 'email']

I am doing this:

$email->bcc($bccEmailsArray); which is the 1st code snippet, and it doesn't work. If I put in the 2nd code snippet, it works.

7
  • 1
    What do you mean "doesn't work"? Commented Sep 13, 2018 at 18:53
  • 3
    The only practical difference I see is that the first array has values with spaces while the second does not. Commented Sep 13, 2018 at 18:54
  • 2
    Oh, and the array:3 part is not valid syntax. Commented Sep 13, 2018 at 18:54
  • The first one doesn't really look like PHP code. It looks like a print_r output. Can you show how you're using those in your code? Commented Sep 13, 2018 at 19:04
  • The first doesn't have comma delimiters... white space isn't interpreted in PHP like it is in languages such as Python Commented Sep 13, 2018 at 19:40

2 Answers 2

1

Like what others are saying your problem is just syntax. because of dd() it appears? dd() var_dump() etc are for debugging.

$array:3 = [     // :3 is not valid
  0 => "email"   //no commas
  1 => " email"  //no commas + extra spaces in emails
  2 => " email"  //no commas + extra spaces in emails
]

Correct it to :

$array = [
  0 => "email",
  1 => "email",
  2 => "email",
]

or to either of these :

$array = [0=>"email",1 => "email",2 => "email"];
$array = array(0=>"email", 1=>"email", 2=>"email");

or just to:

$array = array("email","email","email");

as this will just produce default keys:

array(3) {
  [0]=>
  string(5) "email"
  [1]=>
  string(5) "email"
  [2]=>
  string(5) "email"
}

Hence there is no difference between the two if the syntax is correct.

More information:

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks I'm brain dead today fixing it now ... it's supposed to be array()
I should have put in the post that the first example is coming from a dd()
@AustinHunter ok then you just need to remove the dd() from your code,??
0

The main difference if fact that first sample is not valid PHP code.

Part array:3 makes it invalid.

Valid examples would be

$array = [
  0 => "email",
  1 => "email",
  2 => "email"
];

and

$array = ['email', 'email', 'email'];

Beside the fact that in first example some "emails" start with space both arrays are equals. If you don't provide keys explicitly, elements will be numbered starting from 0.

For more info you can refer to documentation.

3 Comments

you should use comma
I should have put in the post that the first example is coming from a dd()
@AustinHunter can you explain then what do you mean by it doesn't work? There is error, something is happening, something is not happening?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.