note 85739 added to control-structures.do.while

From: Date: Mon, 15 Sep 2008 17:44:59 +0000
Subject: note 85739 added to control-structures.do.while
Groups: php.notes 
Request: Send a blank email to php-notes+get-144428@lists.php.net to get a copy of this message
I'm guilty of writing constructs without curly braces sometimes... writing the do--while seemed a bit odd without the curly braces ({ and }), but just so everyone is aware of how this is written with a do--while... a normal while: <?php while ( $isValid ) $isValid = doSomething($input); ?> a do--while: <?php do $isValid = doSomething($input); while ( $isValid ); ?> Also, a practical example of when to use a do--while when a simple while just won't do (lol)... copying multiple 2nd level nodes from one document to another using the DOM XML extension <?php # open up/create the documents and grab the root element $fileDoc = domxml_open_file('example.xml'); // existing xml we want to copy $fileRoot = $fileDoc->document_element(); $newDoc = domxml_new_doc('1.0'); // new document we want to copy to $newRoot = $newDoc->create_element('rootnode'); $newRoot = $newDoc->append_child($newRoot); // this is the node we want to copy to # loop through nodes and clone (using deep) $child = $fileRoot->first_child(); // first_child must be called once and can only be called once do $newRoot->append_child($child->clone_node(true)); // do first, so that the result from first_child is appended while ( $child = $child->next_sibling() ); // we have to use next_sibling for everything after first_child ?> ---- Server IP: 64.71.164.2 Probable Submitter: 68.60.149.50 ---- Manual Page -- http://www.php.net/manual/en/control-structures.do.while.php Edit -- https://master.php.net/note/edit/85739 Del: integrated -- https://master.php.net/note/delete/85739/integrated Del: useless -- https://master.php.net/note/delete/85739/useless Del: bad code -- https://master.php.net/note/delete/85739/bad+code Del: spam -- https://master.php.net/note/delete/85739/spam Del: non-english -- https://master.php.net/note/delete/85739/non-english Del: in docs -- https://master.php.net/note/delete/85739/in+docs Del: other reasons-- https://master.php.net/note/delete/85739 Reject -- https://master.php.net/note/reject/85739 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#144428) next »