Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the (array) syntax do?

Tags:

arrays

php

I'm currently building code in Laravel and adding the (array) part to my code fixed my laravel sync problem when there was no data being passed in the array. This is the question I learned from and got the (array) code to use.

I'm having a hard time finding the documentation for this in laravel or php and was wondering which language/framework the (array) code originates from and what it exactly does. If you could direct me to the right documentation page I would love that as well.

like image 311
Simon Suh Avatar asked Feb 21 '26 23:02

Simon Suh


1 Answers

It's just another way of creating an array by using an existing variable

$x = 1; // int
$y = (array)$x; // array[0] => 1
$z = [$x]; // array[0] => 1

I should note that the last way would be preferred (directly declaring it as an array), since it's clearer what will happen (type juggling can produce unexpected results when converting values like this).

http://php.net/manual/en/language.types.array.php#language.types.array.casting

like image 168
Machavity Avatar answered Feb 23 '26 12:02

Machavity



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!