PHP Difference Between Array() and []

PHP Difference between array() and []

Following [] is supported in PHP >= 5.4:

['name' => 'test', 'id' => 'theID']

This is a short syntax only and in PHP < 5.4 it won't work.

In PHP, what is the difference between Array and array, in type declaration?

They are both the same. Some IDEs only recognize one of the forms, though. (Just like stackoverflow itself, which only stylizes Array

PHP: difference between array and variable containing an array?

Actually

$row is a run-time array variable which is re-created each time when loop (while loop) runs.So previous values will not be available. That's why you have to save it's data to another array variable which is created statically.

Is this recreation an intrinsic quality of the while loop?

Simon.B no it's not.
You can try

$row = []; 
while($row[] = $result->fetch()){};
print_r($row);

and check.

Better alternative is to use fetchAll();

But why this above apporach is not used often:-

Because above approach will load the whole array again and again into Memory when loop runs and then assign array to that.

While when you are using run-time variable and doing assignment then the whole array is not loaded each time, only assignment will done.

difference between two arrays

Note: this answer will return the values in $array2 that are not present in $array1, it will not return the values in $array1 that are not in $array2.

$diff = array_diff($array2, $array1);

array_diff()

Difference between array_push() and $array[] =

When you call a function in PHP (such as array_push()), there are overheads to the call, as PHP has to look up the function reference, find its position in memory and execute whatever code it defines.

Using $arr[] = 'some value'; does not require a function call, and implements the addition straight into the data structure. Thus, when adding a lot of data it is a lot quicker and resource-efficient to use $arr[].

PHP Variable vs Array vs Object

I would consider it unnecessary overhead. Doing what you are talking about in an object-oriented way only means that inside your class you will have done nothing more than create a bunch of variables, just as you specified in your first example.

The array is the best way to go in my opinion. You are only using one variable, and you can also integrate it into your Class. So, instead of $tpl->title, you may have $tpl->text['title'].

PHP: What's the difference between initializing an array with new vs without it?

You don't instantiate an array in PHP using:

$foo=new array(); // error in PHP

That's for Javascript:

foo=new Array(); // no error in Javascript

In PHP, new is used only for instantiating objects.

what is the difference between collection and array in php (propel)?

1.Hidration

A means to improve performance by "filling" your Class/Object with the row data just when you need it.

Instead of doing "SELECT * FROM SomeTable" from a very large table, Propel will initially fire off "SELECT ID FROM SomeTable", then inside the loop, then do "SELECT [COLUMS] FROM SomeTable WHERE ID=[CurrentID]", hence "On Demand"

2. Collection vs Array
Array is just the normal array, whereas a PropelCollection is an Object of Objects, which has a lot of stuff available such as:

  • Paging results
  • Checking for odd or even, ->isOdd(), etc.
  • Checking the number of items in it $object->count()
  • Format converting ->toYAML(), ->toCSV(), ->toXML()

Each item in the collection is a PropelObject, so you can still fetch data with ->getColumn() inside your loop. Documentation



Related Topics



Leave a reply



Submit