
PHP Loops and Iteration: Learn About While, Do While, For, and Foreach Loops
"Explore PHP loop statements including while, do while, for, and foreach loops to execute code repeatedly based on specified conditions or number of iterations. Improve your PHP scripting skills with practical examples and detailed explanations."
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
PHP Scripting Language PHP Loops Lecturer: Akash kumar choudhary
PHP Loops PHPLoops Loops executeablockofcodeaspecifiednumberoftimes,orwhilea specified condition istrue. Oftenwhenyouwritecode,youwantthesameblockofcodetorunover andoveragaininarow.Insteadofaddingseveralalmostequallinesina scriptwecanuseloopstoperformatasklikethis. InPHP ,wehavethefollowingloopingstatements: while-loopsthroughablockofcodewhileaspecifiedconditionistrue do...while-loopsthroughablockofcodeonce,andthenrepeatstheloop aslongasaspecifiedconditionistrue for-loopsthroughablockofcodeaspecifiednumberoftimes foreach-loopsthroughablockofcodeforeachelementinanarray
PHP Loops The whileLoop Thewhileloopexecutesablockofcodewhileaconditionistrue.
PHP Loops Example Theexamplebelowdefinesaloopthatstartswithi=1.Theloopwillcontinue torunaslongasiislessthan,orequalto5.iwillincreaseby1eachtimethe loopruns:
PHP Loops The do...whileStatement Thedo...whilestatementwillalwaysexecutetheblockofcodeonce,itwill thencheckthecondition,andrepeattheloopwhiletheconditionistrue.
PHP Loops Example Theexamplebelowdefinesaloopthatstartswithi=1.Itwillthenincrementi with 1, and write some output. Then the condition is checked, and the loop willcontinuetorunaslongasiislessthan,orequalto5:
PHP Loops The forLoop Theforloopisusedwhenyouknowinadvancehowmanytimesthescript shouldrun. Parameters: init:Mostlyusedtosetacounter(butcanbeanycodetobeexecutedonceatthebeginning of theloop) condition:Evaluatedforeachloopiteration.IfitevaluatestoTRUE,theloopcontinues.Ifit evaluatestoFALSE,theloopends. increment:Mostlyusedtoincrementacounter(butcanbeanycodetobeexecutedatthe endoftheloop) Note:Eachoftheparametersabovecanbeempty,orhavemultipleexpressions(separated bycommas).
PHP Loops Example Theexamplebelowdefinesaloopthatstartswithi=1.Theloopwillcontinue torunaslongasiislessthan,orequalto5.iwillincreaseby1eachtimethe loopruns:
PHP Loops The foreachLoop Theforeachloopisusedtoloopthrougharrays. Foreveryloopiteration,thevalueofthecurrentarrayelementisassignedto $value(andthearraypointerismovedbyone)-soonthenextloop iteration,you'llbelookingatthenextarrayvalue.
PHP Loops Example Thefollowingexampledemonstratesaloopthatwillprintthevaluesofthe givenarray:
PHP Forms PHP FormsandUserInput
PHP Forms PHP FormHandling ThePHP$_GETand$_POSTvariablesareusedtoretrieveinformation fromforms,likeuserinput. ThemostimportantthingtonoticewhendealingwithHTMLformsand PHP is that any form element in an HTML page will automatically be availabletoyourPHPscripts. TheexamplebelowcontainsanHTMLformwithtwoinputfieldsanda submitbutton:
PHP Forms PHP FormHandling Whenauserfillsouttheformaboveandclickonthesubmitbutton,the formdataissenttoaPHPfile,called"welcome.php": "welcome.php" lookslike this:
PHP Forms PHP $_GETFunction Thebuilt-in$_GETfunctionisusedtocollectvaluesinaformwith method="get". InformationsentfromaformwiththeGETmethodisvisibletoeveryone (it will be displayed in the browser's address bar) and has limits on the amountofinformationtosend.
PHP Forms PHP $_GETFunction Whentheuserclicksthe"Submit" button,theURLsenttotheserver couldlooksomething likethis:
PHP Forms When touse method="get"? Whenusingmethod="get" inHTMLforms,allvariablenamesandvalues aredisplayedintheURL. Note:Thismethodshouldnotbeusedwhensending passwordsorother sensitiveinformation! However,becausethevariablesaredisplayedintheURL,itispossibleto bookmarkthepage.Thiscanbeusefulinsomecases. Note:Thegetmethodisnotsuitableforverylargevariablevalues.Itshould notbeusedwithvaluesexceeding2000characters.
PHP Forms The $_POSTFunction Thebuilt-in$_POSTfunctionisusedtocollectvaluesfromaformsent withmethod="post". InformationsentfromaformwiththePOST methodisinvisibletoothers andhasnolimitsontheamountofinformationtosend. Note:However,thereisan8Mb maxsizeforthePOST method,bydefault (canbechangedbysettingthepost_max_sizeinthephp.inifile).
PHP Forms The $_POSTFunction
PHP Forms When touse method="post"? InformationsentfromaformwiththePOST methodisinvisibleto others andhasnolimitsontheamountofinformationtosend. However,becausethevariablesarenotdisplayedintheURL,itis not possibletobookmarkthepage. ThePHP$_REQUESTFunction ThePHPbuilt-in$_REQUESTfunctioncontainsthecontentsofboth $_GET ,$_POST,and$_COOKIE. The$_REQUESTfunctioncanbeusedtocollectformdatasentwith both theGETandPOST methods.
www.w3schools.com www.beta-labs.in