Viewed   121 times

foreach in PHP7 by default, when iterating by value, operates on a copy of the array according to: http://php.net/manual/en/migration70.incompatible.php

Does it lazily create a copy only if there are changes made to the array or a value or will it always make a copy and in essence make looping over references a performance optimization?

Also, do arrays of objects still loop over/give you references of the objects? Or will they actually also create copies for the foreach and return the objects by value?

 Answers

5

In PHP 7, if you iterate an array by value, the copy will be done lazily, only when and if the array is actually modified.

If you iterate an array by reference instead, a separation will be performed at the start of the loop. If the array is currently used in more than one place, this separation will lead to a copy.

Furthermore iterating by reference means that a) the array has to be wrapped into a reference and b) each element has to be wrapped in a reference as well. Creating a reference wrapper is an expensive operation, because it requires allocation.

Additionally iteration by reference requires us to use a modification-safe iteration mechanism. This works by registering the iterator with the array and checking for potentially affected iterators in various array modification operations.

So no, iterating by reference is certainly not an optimization, it's a de-optimization. Using references usually is.

Sunday, September 18, 2022
4

Install it via wget and create an alias in Apache. Keep track:

Change to directory /usr/share:

cd /usr/share

Change to root user:

 sudo su

Download phpMyAdmin:

wget https://files.phpmyadmin.net/phpMyAdmin/4.5.4.1/phpMyAdmin-4.5.4.1-all-languages.zip

Unzip it: (you may install unzip first)

unzip phpMyAdmin-4.5.4.1-all-languages.zip

Rename the folder:

mv phpMyAdmin-4.5.4.1-all-languages phpmyadmin

Change permissions:

chmod -R 0755 phpmyadmin

Configure apache so that it can find it correctly:

vim /etc/apache2/sites-available/000-default.conf

Anywhere after "DocumentRoot /var/www/html" insert these line:

Alias /phpmyadmin "/usr/share/phpmyadmin/"
<Directory "/usr/share/phpmyadmin/">
     Order allow,deny
     Allow from all
     Require all granted
</Directory>

Restart Apache:

service apache2 restart

And you are ready to go!

Just took a screenshot from my current installation for you to validate it works.

Friday, December 16, 2022
 
0ttt0
 
3

change in controller

     $result_criteria = $this->app_model->manualQuery("select b.nik,b.hubkel
      from biodata_karyawan bk left join bpjs b
     on b.nik = bk.nik where bk.status_karyawan = 'Aktif' and " .        $bagianWhere ." order by b.nik");

     $bc['dt_karyawan'] = $this->db->query($result_criteria)->result();

change in views as following

  foreach($dt_karyawan as $row)
 {
 if($row->hubkel=='pegawai')
 {
  $query_pegawai = "select bk.no_kk as 'no_2' ....";
   foreach($query_pegawai->result() as $data1)
   {
    echo '<tr align="center">';                                
    echo '<td>'.$no.'</td>';
    echo '<td>'.$kutip.$data1->no_2.'</td>';
    .
    .
    }
  }
 if($row->hubkel=='istri')
 { 
 .
 .
 .
 }
 }
Tuesday, November 29, 2022
 
jemmons
 
1

To safely remove from a collection while iterating over it you should use an Iterator.

For example:

List<String> names = ....
Iterator<String> i = names.iterator();
while (i.hasNext()) {
   String s = i.next(); // must be called before you can call i.remove()
   // Do something
   i.remove();
}

From the Java Documentation :

The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Perhaps what is unclear to many novices is the fact that iterating over a list using the for/foreach constructs implicitly creates an iterator which is necessarily inaccessible. This info can be found here

Tuesday, October 11, 2022
 
4

It seems that in the environment you have, the PCRE library was compiled without the PCRE_NEWLINE_ANY option, and $ in the multiline mode only matches before the LF symbol and . matches any symbol but LF.

You can fix it by using the PCRE (*ANYCRLF) verb:

'~(*ANYCRLF)S+(?=*$)~m'

(*ANYCRLF) specifies a newline convention: (*CR), (*LF) or (*CRLF) and is equivalent to PCRE_NEWLINE_ANY option. See the PCRE documentation:

PCRE_NEWLINE_ANY specifies that any Unicode newline sequence should be recognized.

In the end, this PCRE verb enables . to match any char BUT a CR and LF symbols and $ will match right before either of these two chars.

See more about this and other verbs at rexegg.com:

By default, when PCRE is compiled, you tell it what to consider to be a line break when encountering a . (as the dot it doesn't match line breaks unless in dotall mode), as well the ^ and $ anchors' behavior in multiline mode. You can override this default with the following modifiers:

(*CR) Only a carriage return is considered to be a line break
(*LF) Only a line feed is considered to be a line break (as on Unix)
(*CRLF) Only a carriage return followed by a line feed is considered to be a line break (as on Windows)
(*ANYCRLF) Any of the above three is considered to be a line break
(*ANY) Any Unicode newline sequence is considered to be a line break

For instance, (*CR)w+.w+ matches Line1nLine2 because the dot is able to match the n, which is not considered to be a line break. See demo.

Thursday, September 8, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :