What is the difference between using array_push() and directly assigning a new key to an array in PHP?

When adding a new element to an array in PHP, you can either use the array_push() function or directly assign a new key to the array. The main difference between the two methods is that array_push() is specifically designed to add elements to the end of an array, while directly assigning a new key allows you to add elements at any position within the array. Additionally, array_push() can only add one element at a time, whereas directly assigning a new key allows you to add multiple elements in a single operation.

// Using array_push() to add a new element to the end of an array
$myArray = [1, 2, 3];
array_push($myArray, 4);

// Using direct assignment to add a new key to an array
$myArray = [1, 2, 3];
$myArray[3] = 4;