$data = array() vs unset($array) - php

this is my first question.
I am doing some optimizations on a php script, improving its speed of execution...
Between :
$datas = array();
$datas['file_import'] = $file_name_reporting;
And :
unset($datas);
$datas['file_import'] = $file_name_reporting;
Can someone tell me which one is faster ?
Thank you

Your second example causes warning, because $datas is right now null and you are treating it as an array, so you have to declare it as an empty array before.
So just follow your first example - assign an empty array and then put into it some data.

array() will create an array whereas unset() will destroy a variable.

I think first method is just a overwriting but second one includes deleting, checking existence, triggering warning and creating new array

It's ridiculous to claim that either form is "faster" than the other. Both versions will execute so fast that you would need to run them millions of times inside a loop to perhaps notice a difference. Do you actually do that inside your script? If not, forget about "optimization" here (actually, it would be a good idea to forget about all optimization "by eye", as any experienced developer can tell you).
On top of that, the two versions actually do different things, in that unset will remove the name $datas from the sumbol table (and give you a notice in the next line when you attempt to add a value to an array).
Just use what feels right, and look inside heavy loops to find something to optimize.

In both cases, a new Array will be constructed. Unsetting a variable in php, will set it's value to null, only to call the array constructor on the next line. Although I agree with knittl, my suggestion would be:
$datas = array('file_import' => $file_name_reporting);
By creating a new array, you automatically 'unset' the variable, and by passing values to the array constructor, you can fill your array with whatever values you want while you're at it.

Obviously the first code will work faster because you do only two operations: explicitly create an array and add a portion of data. The second example will cause a warning because you destroy a variable and then try to use it again.
Additionally unset will not release used memory, it will only release a pointer on variable. Memory will be released when gc will be runned. To release a memory, use $datas = null; instead.

Related

Is there ever a reason to overwrite an array variable with one of its own elements?

I've recently "inherited" a PHP web app that uses a mySQL database as part of its backend. While going through the code, I came across a block that absolutely puzzled me.
$results = array();
$results[] = $mysqli_result->fetch_array();
$results = $results[0];
return $results;
So I get that the first two lines are initializing an empty array and assigning the first row of results from a previous query to that new array. But the third line doesn't make sense to me. As I understand it, fetch_array() only grabs one row at a time; I can't think of any reason to have the $results = $results[0]; line. The best I can come up with is that it's leftover code from when mysql_result was removed.
Is there any reason to have this third line?
No, there is no reason. This is just an example of a "cargo cult code". When you don't know how to do something properly, you just copy/paste some existing code without real understanding whether it is necessary or not.
Of course it makes no sense to create an array, then create a new element in this array, and then reassign this array variable with the first element. Instead, just assign a variable:
$result = $mysqli_result->fetch_array();
Or - better - since this variable is returned right after the assignment, simply return it right away, as the $result variable isn't going to be used anywhere else.
return $mysqli_result->fetch_array();

Can you get value from an array without getting the array first?

Bear with me, I'm learning.
I often see snippets like the one below:
<?p
$imageArray = get_field('image_field');
$imageAlt = $imageArray['alt'];
$imageURL = $imageArray['url'];
?>
It is pedagogical and clear and organized. But is it necessary to get the entire array before querying the array for values? Can I not define the variable in just a single line? Something like the below (which doesn't work, neither the other variants I have tried):
$imageAlt = get_field('image_field', ['alt']);
$imageURL = get_field('image_field', ['url']);
Yes, you can.
As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable. - Source
$imageAlt = get_field('image_field')['alt'];
https://eval.in/548036
The question you are asking can be answered by asking 2 questions:
Is it doable ?
Is it a good idea to do it that way ?
Is it doable ?
Yes! You do not have to store the array in a variable and re-use it later.
For instance, you could do:
$imageAlt = get_field('image_field')['alt'];
Note: This will work in PHP 5.4+ and is called: Array dereferencing.
But that is not the only consideration...
Is it a good idea to do it that way ?
No. It's not a good idea in many cases. The get_field() function, depending on your context, is probably doing a lot of work and, each time you call it, the same work is don multiple times.
Let's say you use the count() function. It will count the number of items in an array. To do that, it must iterate through all items to get the value.
If you use the count() function each time you need to validate number of items in an array, you are doing the task of counting each and every time. If you have 10 items in your array, you probably won't notice. But if you have thousands of items in your array, this may cause a delay problem to compute your code (a.k.a. it will be slow).
That is why you would want to do something like: $count = count($myArray); and use a variable instead of calling the function.
The same applies to your question.
While PHP 5.4+ allows you to directly dereference a function return value like this:
get_field('image_field')['alt']
...in this particular case I would not suggest you do so, since you're using two values from the resulting array. A function call has a certain overhead just in itself, and additionally you don't know what the function does behind the scenes before it returns a result. If you call the function twice, you may incur a ton of unnecessary work, where a single function call would have done just as well.
This is not to mention keeping your code DRY; if you need to change the particulars of the function call, you now need to change it twice...
PHP allows you to play around quite a bit:
function foo(){
return array('foo' => 1, 'bar' => 2);
}
Option 1
echo foo()['foo']; // 1
# Better do this if you plan to reuse the array value.
echo ($tmp = foo())['foo']; // 1
echo $tmp['bar']; // 2
It is not recommended to call a function that returns an array, to specifically fetch 1 key and on the next line doing the same thing.
So it is better to store the result of the function in a variable so you can use it afterwards.
Option 2
list($foo, $bar) = array_values(foo());
#foo is the first element of the array, and sets in $foo.
#bar is the second element, and will be set in $bar.
#This behavior is in PHP 7, previously it was ordered from right to left.
echo $foo, $bar; // 12
Option 3
extract(foo()); // Creates variable from the array keys.
echo $foo, $bar;
extract(get_field('image_field'));
echo $alt, $url;
Find more information on the list constructor and extract function.

Do I need to initialise every key of a multidimensional array in PHP?

stricly speaking / best practice: do I need to set up every 'deeper nested sub array' or can you just access it?
//so do I need to do this:
if(!isset($multiArray[$newKey])) $multiArray[$newKey] = array();
//before I can do this?
$multiArray[$newKey][] = 'somevalue';
Strictly speaking no, PHP will create the new array before pushing the value to it, without any problem.
Best practice? It's probably personal opinion based, I would say create the array first for readability and it makes sense logically. If another developer picks up your code, it might not be obvious that you are creating a new array, as opposed to pushing to an existing array.
It is not necessary to initialize variables in PHP however it is a very good practice. Uninitialized variables have a default value of their type depending on the context in which they are used - booleans default to FALSE, integers and floats default to zero, strings (e.g. used in echo) are set as an empty string and arrays become to an empty array.
Source : PHP Manual http://fr2.php.net/manual/en/function.array-multisort.php

Why would one want to pass primitive-type parameters by reference in PHP?

One thing that's always bugged me (and everyone else, ever) about PHP is its inconsistency in function naming and parameters. Another more recent annoyance is its tendency to ask for function parameters by reference rather than by value.
I did a quick browse through the PHP manual, and found the function sort() as an example. If I was implementing that function I'd take an array by value, sort it into a new array, and return the new value. In PHP, sort() returns a boolean, and modifies the existing array.
How I'd like to call sort():
$array = array('c','a','b');
$sorted_array = sort($array);
How PHP wants me to call sort():
$array = array('c','a','b');
sort($array);
$sorted_array = $array;
And additionally, the following throws a fatal error: Fatal error: Only variables can be passed by reference
sort(array('c','a','b');
I'd imagine that part of this could be a legacy of PHP's old days, but there must have been a reason things were done this way. I can see the value in passing an object by reference ID like PHP 5+ does (which I guess is sort of in between pass by reference and pass by value), but not in the case of strings, arrays, integers and such.
I'm not an expert in the field of Computer Science, so as you can probably gather I'm trying to grasp some of these concepts still, and I'm curious as to whether there's a reason things are set up this way, or whether it's just a leftover.
The main reason is that PHP was developed by C programmers, and this is very much a C-programming paradigm. In C, it makes sense to pass a pointer to a data structure you want changed. In PHP, not so much (Among other things, because references are not the same as a pointer).
I believe this is done for speed-reason.
Most of the time you need the array you are working on to be sorted, not a copy.
If sort should have returned a new copy of the array then for each time you call sort(); the PHP engine should have copied the array into new one (lowering speed and increasing space cost) and you would have no way to control this behaviour.
If you need the original array to be not sorted (and this doesn't happen so often) then just do:
$copy = $yourArray;
sort($yourArray);

Does foreach always create a copy on a none reference in PHP?

I'm wondering if PHP has this optimization built in. Normally when you call foreach without using a reference it copies the passed array and operates on it. What happens if the reference count to that array is only 1?
Say for example if getData returns some array of data.
foreach(getData() as $data)
echo $data;
Since the array returned by getData() only has one reference shouldn't it just be used by reference and not copied first or does php not have this optimization?
This seems like a simple optimization that could help a lot of badly written code.
I can't say for certain, but PHP normally uses "copy on write", so everything is a reference until you try to write to it, at which time a copy is made and you write to the copy.

Categories