0% found this document useful (0 votes)
24 views26 pages

1

Uploaded by

sff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
24 views26 pages

1

Uploaded by

sff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 26
= eh ap | Unit 2 Working with Basic Building Dp Darshan Blocks of PHP Introduction to PHP © PHP isa server scripting language and a powerful tool for making dynamic and interactive web pages. * PHP stands for "PHP: Hypertext Preprocessor”. © PHP is a widely-used, open source scrit «PHP scripts are executed on the server. + PHP is free to download and use. A Brief History of PHP ‘¢ PHP was originally created by Rasmus Lerdorf in 1994, Programmer Rasmus Lerdorf initially created a set of C scripts he called "Personal Home Page Tools" to maintain his personal homepage. The scripts performed tasks such as displaying his resume and recording his web-page traffic. * These were released and extended to include a package called the Form Interpreter (PHP/Fl). While PHP originally stood for "Personal Home Page", now it is known as "PHP: Hypertext Preprocessor". © In 1997 Zeev Suraski and Andi Gutmans along with Rasmus rewrite PHP and released PHP version 3.0 in June 1998. After this release PHP becomes so much popular. «The PHP version 4.0 was launched in May 2000.This version includes session handling, output buffering and support for wide variety of web server platforms. ‘* The PHP 5.0 version released in 2004 with object oriented programming concept. ing language. Open Source In general, open source refers to any program whose source code is made available for use or modification. Open source software is usually developed as a public collaboration and made freely available. It means can be used without purchasing any license. * Open Source isa certification mark owned by the Open Source Initiative (OSI). Developers of software that is intended to be freely shared and possibly improved and redistributed by others can use the Open Source trademark if their distribution terms conform to the OSI's Open Source Definition. To summarize, the Definition model of distribution terms require that: © The software being distributed must be redistributed to anyone else without any restriction. © The source code must be made available (so that the receiving party will be able to improve or modify it) Example of Open Source: Linux, Apache, MySQL, PHP. How PHP Works? «PHP sits between your browser and the web server. When you type in the URL of a PHP website in your browser, your browser sends a request to the web server. The web server then calls the PHP script on that page. The PHP module executes the script which then sends the result in the form of HTML back to your browser which you seen on the screen 1 | Dept: cE DWPD (3350702) Prof. Payal M.Boda D Sarchan | Unit 2 Working with Basic Building Darsh an Blocks of PHP eae of ge Ae Dip Sade Relationship between Apache, MySQL and PHP (AMP Module) ‘AMP stands for Apache MySQL PHP PHP ‘+ PHP is a server side scripting that was designed for creating dynamic websites. + PHP script runs on a web server. ‘+ PHP is loosely type language because it converts a data type of variable automatically. * PHP is powerful scripting language that can be run in the command line of any computer with PHP installed ‘+ However, PHP alone isn’t enough in order to build dynamic web sites. Apache ‘* Touse PHP ona web site, you need a server that can process PHP scripts. Apache is a free web Server that, once installed on a computer, allows developers to test PHP scripts locally; this makes it an invaluable piece of your local development environment. ‘Like all web servers, Apache accepts an HTTP request and serves an HTTP response. * Apache is open source free software distributed by the Apache Software Foundation. MySQL + Additionally, dynamic websites are dependent on stored information that can be modified quickly and easily; this is the main difference between a dynamic site and a static HTML site. «However, PHP doesn’t provide a simple, efficient way to store data. Thi management system like MySQL. comes into play. «PHP provides native support for it and the database is free, open-source project. + MySQL is a relational database management system (DBMS). Essentially, this means that MySQL allows users to store information in a table-based structure, using rows and columns to organize different pieces of data. is where a relational database DWPD (3350702) Prof. Payal M.Boda = eh ap | Unit 2 Working with Basic Building Dp Darshan Blocks of PHP Creating and Saving a PHP File ‘© APHP script is executed on the server, and the plain HTML result is sent back to the browser. + APHP script starts with <2php and ends with ?> * APHP script can be placed anywhere in the document. © Syntax: ‘* The default file extension for PHP files is ".php". © APHP file normally contains HTML tags, and some PHP scripting code. * Below, we have an example of a simple PHP file, with a PHP script that uses a built-in PHP function echo" to output the text "Hello World!" on a web page title>Unit-2 Output: Hello World PHP Start and End Tags © There are four different pairs of opening and closing tags which can be used in php. * Below, we have an example of a simple PHP script which sends the text “Hello World” to the browser: 1. Standard PHP tags 2. Short tags 2 echo "Hello World”; > © Short tags are only available when they are enabled via the short_open_tag php.ini configuration file directive, or if php was configured with the enable-short-tags option. 3 | Dept: CE DWPD (3350702) Prof. Payal M.Boda = eh ap | Unit 2 Working with Basic Building Dp Darshan Blocks of PHP 3. Script tags © This syntax is removed after PHP 7.0.0. 4. ASP style tags <% echo "Hello World"; ‘%> © This syntax is removed after PHP 7.0.0. Comments in PHP ‘+ PHP also supports comment like a C, C++, Java languages. ‘* PHP supports two types of comments: Single line comment * Single line comment started with symbol //. © Ifyou have a single line comment, another option is to use a # sign. Here is an example of this: <2php # this is a single line comment ?> Multiline comment * Multiline comment started with symbol /* and end with symbol */. + So, the multiline comment starts with one line and can be expand to more than one line. <2php /* this is the Multiline Comment */ ?> PHP Echo and Print Statements + They are both used to output data to the screen. Echo * The simplest use of echo is to print a string as argument, for example: echo "Hello World"; Or equivalently: echo ("Hello World"); ‘* You can also give multiple arguments to the unparenthesized version of echo , separated by commas: echo "Hello", "World"; «The parenthesized version, however, will not accept multiple arguments: echo("Hello", "World"}; Input Output echo "Hello World” Hello World echo("Hello World’ Hello World echo "Hello", "World’ Hello World Q | Dept: CE DWPD (3350702) Prof. Payal M.Boda @ Darshan Unit 2 Working with Basic Building Blocks of PHP er ech Dp Sader echo("Hello’ Print "World"); Inv: ‘Argument ‘+ The command print is very similar to echo, with two important differences: © print can accept only one argument. © print returns a value, which represents whether the print statement succeeded. © The value returned by print will be 1 if the printing was successful and 0 if unsuccessful. * For example, the following two lines will print exactly the same thing: print("3.14159"); //p print(3.14159); a string /iprint a number Difference between Echo and Print ECHO PRINT It can accept multiple expressions. Itcannot accept multiple expressions. It is faster than print. It is slower than echo. Itcan pass multiple string separated as (,). It cannot pass multiple arguments. Itis a statement used to display the output and can be used with the parentheses echo or without the parentheses echo. It is also a statement which is used to display the output and used with the parentheses print() or without the parentheses print. It doesn’t return any value. talways returns the value 1. Installation of WAMP Server * Step-1 : Download the WAMP Server * Step-2: Select Language Select Setup Language x You will be prompted to select the language mates 2 you wish to use in WAMP. Click the arrow in @ ses the dropdown box, select your language in the list, then click OK to continue the eos ss installation process. aE 5 | Dept: CE DWPD (3350702) Prof. Payal M.Boda HD arshan eae of ge Ae Dip Sade Unit 2 Working with Basic Building Blocks of PHP © Step-3: License Agreement The next screen you are presented with is the License Agreement. Read the agreement, check the radio button next to | accept the agreement, then click Next to continue the installation, ‘* Step-4: Please read the following important information before continuing, then click Next to continue the installation. ‘© Step-5: Select Destination Location Next you will see the Select Destination Location screen. Unless you would like to install WampServer on another drive, you should not need to change anything, Click on Next. 6 | Dept: CE DWPD (3350702) 2 swat - G a - Pre recreate @ Tnstaation of Wampserver a enacts ny roe Wap Ee ert vowws vewmayers, nr mca wens vee bore at arr’ OVER a iting von lore ex G L thowrse teeta: = Prof. Payal M.Boda apy | Unit 2 Working with Basic Building Darshan Blocks of PHP * Step-6: Select Components sap winyenetan = oe Select the components you want to install; “iiSitetsedierte @® clear the components you do Not WANE tO oo. se arsna tw sean vadnten nh cine install. Click Next when you ready to Sxkieteteztlis anda oa sini iteenresthate continue. m2 © Step-7: Select Start Menu Folder Stes mera - Li Where should setup place the program’s “rst pagans enn @ shortcuts? If you would like to select a = different folder, click browse after then click EL, stonteme terre snrmsen tc rns te Next ‘rere te reed enn ie ie me = = © Step-8: Ready to Install oneal = Next you will see the Ready To Install screen. You can review your setup choices, and change any of them by clicking Back to the appropriate screen, if you choose to. Once you have reviewed your choices, click Install to continue. DWPD (3350702) Prof. Payal M.Boda Unit 2 Working with Basic Building Darshan Blocks of PHP © Step-9: Installing e WampServer will begin extractingfilesto the MIN a uonausnnmnertenar nea @O location you selected, click Yes to continue. = ‘© Step-10: Once the files are extracted, you © 7="sm-m+smtownstmnernenn inant 7 will be asked to select your default browser. + Estate neon =e Z WampServer defaults to Internet Explorer Sm" SEIS) upon opening the local file browser window. =" eetwie If your default browser isn’t IE, then look ing omsp _ the following locations forthe} ’=~" k ms corresponding .exe file: 2 se ‘ © Opera: C:\Program Files = ‘ oe (x86)\Opera\opera.exe eter) © Firefox: C:\Program Files (x86)\Mozille ==" Firefox\firefox.exe Zameen + « > ©. Safari: C:\Program Files nme . (x86)\Safari\safari.exe | = co Chrome: C:\Users\yooxxx\AppData\Local\Google\ Chrome\Application\chrome.exe © Select your default browser's .exe file, then click Open to continue. 8 | Dept: CE DWPD (3350702) Prof. Payal M.Boda apy | Unit 2 Working with Basic Building Darshan Blocks of PHP © Step-11: Warning, ap wape329 = If may be first launch not all services are "mmm @ started. So, left-click Wampmanager icon “ then Restart the services. Click Next to continue, ae eae eee eee ‘oe orice For some explanations on how Wampuerver work, :00 the © Step-12: The Installation Complete screen © “** "erem=si22 - will now appear, then click Finish to Completing the Wampserver64 Setup Wizard complete the installation. oD seme wy Son oioymenmret? WanpServer stan bets. version 2.0.0 by Wampserver 3.2.0 © Step-13: Run Wamp Server ~@O« =m dx ENG You should see the WampServer icon appear in the systray on the right side of your taskbar. If the icon is green, then everything RDO ING is working properly. If the icon is orange, then there are issues with one of the services. If the icon is red, then both Apache ~@« Sa and MySQL services aren’t running. You will need to resolve those issues before continuing. 9 | Dept: CE DWPD (3350702) Prof. Payal M.Boda apy | Unit 2 Working with Basic Building Darshan Blocks of PHP © Step-14: Once you have completed the installation process, test that your installation is working properly by going to http://localhost/ in your browser. You should see the WampServer homepage displayed. Installation of XAMPP Server ‘* Step-1 : Download the XAMPP Server © Step-2: User Access Control Warning ae * You will see a window pop up, warning you et eee about User Account Control (UAC) being Seeiet te iran henge pers rece active on your system. Click OK to continue — the installation tea © Step-3: XAMPP Setup Wizard Bsa - * Now you will see the actual setup wizard. sano Click Next to continue. 10 | Dept: ce DWPD (3350702) Prof. Payal M.Boda apy | Unit 2 Working with Basic Building Darshan Blocks of PHP Step-4: Select Component In this dialog box you have to select the components to install. Click Next to continue. Step-5: Select Installation Folder Select the installation folder. If you do not want to install XAMPP on C drive then change this folder otherwise leave it as it is and click next Step-6: Bitnami for XAMPP Bitnami provides free setup for WordPress, Drupal, Joomla, But we can skip this for now. So, uncheck the "Learn more about Bitnami" box. Click on Next. Scr [rel aeinmenansanes ea mest roa 11 Dept: CE DWPD (3350702) Prof. Payal M.Boda D Sarchan | Unit 2 Working with Basic Building Darshan Blocks of PHP ‘* Step-7: Ready to Install Bsn - x Now the setup is ready to install XAMPP. Click gesgyteinstat @ Next to start the installation process. Seto inon rey beg ears Aan a ores =e ‘* Step- 8: Installation Complete stp x Now, setup has finished installing XAMPP on competing the a etapa your computer. Click on Finish. f&) saptertnbed eng ee ot (Cicero te contain? bitnami © Step-9: Select Language Language a Select the language you wish to use in XAMPP. Check the box next to the American flag for English, or check the box next to the German flag for German. Click the radio button, then click on Save. ® ; xu CED 12 | Dept: ce DWPD (3350702) Prof. Payal M.Boda apy | Unit 2 Working with Basic Building Darshan Blocks of PHP © Step-10: XAMPP should start now and you SV» sresass cnressnons) os will see the XAMPP Control Panel. were coneranet.24 a ‘And you can start Apache, MySQL and all “"" ter guy tm Ame services to click Start button. When the em | oe |e service starts, you will see a Stop button. This ee) | penn means that the service has started Sse PHP Variable and Variable Rules + Variables are "containers" for storing information. + Declaring Variables: $var_name=value; + Rules for PHP variables: © Avariable starts with the $ sign, followed by the name of the variable. © Avariable name must start with a letter or the underscore character. © Avariable name cannot start with a number. © Avariable name can only contain alpha-numeric character and underscore (A-2, 0-9, and _}. © Variable names are case-sensitive (Sage and $AGE are two different variables) Example: Stxt = "Hello world!";_// variable $txt will hold the value Hello world! $x=5; // variable $x will hold the value S Sy = 10.5; // variable $y will hold the value 10.5 Data Types «PHP supports following data types: Scalar data «Scalar data means data that contains only a single value Scalar Data Type Description Example Integer ‘A whole number 15 Float A floating - point number 8.23 String A series of characters “Hello, world!” Boolean Represents either true or false TRUE Compound data © Compound data is data that can contain more than one value. Compound Data Type | Description Array ‘An ordered map (contains names or numbers mapped to values) Object A type that may contain properties and methods 13 | Dept: cE DWPD (3350702) Prof. Payal M.Boda = eh ap | Unit 2 Working with Basic Building Dp Darshan Blocks of PHP Special data * Finally, PHP supports two special data types, so called because they don't contain scalar or compound data as such, but have a specific meaning: Special Data Type | Description Resource or database — Contains a reference to an external resource, such as a file May only contain null as a value, meaning the variable explicitly does not contain any value Null Why PHP is known as loosely typed language? © PHP automatically converts the variable to the correct data type, depending on its value. In other languages such as C, C++, and Java, the programmer must declare the name and type of the variable before using it. Changing Types (casting) Gettype ‘This function is use to get the type of the variable that means this function will return the type of the PHP variable. * Syntax: gettype(arg) + The above function will return a string representing the type of arg: integer, float, string, array, and object, NULL or unknown type. Example: Output:- integer string settype «The settype() function is used to set the type of a variable. Syntax: settype(var_name, var_type) * var_name: The variable being converted. + var_type: Type of the variable. Possible values: boolean, integer, float, string, array, object, null. Example: $a=65; settype($a, "string"); 14 | Dept: ce DWPD (3350702) Prof. Payal M.Boda apy | Unit 2 Working with Basic Building Darshan Blocks of PHP casting + Ifyou want to change type temporary, i., want to use inside an expression, you can use type casting, * Syntax: (type)Svariable «Where type is a type of variable you wish to cast to. Example: $varl= 10; //integer $Svar2= (string) $varl; // $varl is now set to 10 (string) PHP Operators * Operators are used to perform operations on variables and values. Arithmetic Operators The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication ete. Operator | Name Example _| Result + ‘Addition $x+Sy___ | Sum of $xand $y - ‘Subtraction $x- Sy Difference of $x and Sy si Multiplication Sx* Sy Product of $x and Sy / Division Sx/ $y Quotient of $x and $y % Modulus $x.% Sy Remainder of $x divided by Sy ad Exponentiati Sxm* Sy Result of raising $x to the Sy'th power (PHP 5.6) Logical Operators © The PHP logical operators are used to combine conditional statements. Operator | Name | Example | Result and, && And _| Sxand $y | True if both $x and Sy are true or I Or _| $xor$y_| True ifeither $x or $y is true 1 Not | ISx True if Sx is not true Assignment Operators * The PHP assignment operators are used with numeric values to write a value to a variable. ‘© The basic assignment operator in PHP is "=", It means that the left operand set to the value of the assignment expression on the right. Assignment | Same as_| Descri x=y x=y___| The left operand set to the value of the expression on the right ‘Addition Subtraction Multiplication xy Division 15 | Dept: CE DWPD (3350702) Prof. Payal M.Boda apy | Unit 2 Working with Basic Building Darshan Blocks of PHP x%=Y x=x%y | Modulus String Operators ‘+ PHP has two operators that are specially designed for strings. Operator Name Example _| Result Concatenation ‘Stxt1. $txt2_| Concatenation of Stxt1 and $txt2 Concatenation StxtL.= Stet2 | Appends Stxt2 to Stet assignment Increment / Decrement Operators © The PHP increment operators are used to increment a variable's value. * The PHP decrement operators are used to decrement a variable's value. Operator Name Description +45x Pre-increment _| Increments $x by one, then returns $x Sx Post-increment | Returns $x, then increments $x by one Pre-decrement | Decrements $x by one, then returns $x Post-decrement | Returns $x, then decrements $x by one Conditional Operator ‘There is one more operator called conditional operator. This first evaluates an expression for a true or false value and then execute one of the two given statements depending upon the result of the evaluation. The conditional operator has this syntax Operator __| Description Example ? Conditional If Condition is true? Then value X : Otherwise value Y Expression Examp! $y_ | Returns true if $x is not equal to $y Net identical ‘x sy Returns true if $x is not equal to $y, or they are not of the same type Greater than | $x>$y _| Returns true if $x is greater than Sy Less than Sx<$y__| Returns true if $xis less than $y Greater than or $x>=$y | Returns true if $x is greater than or equal to $y equal to Less than or < Sx <= $y | Returns true if $x is less than or equal to Sy equal to Bitwise Operators ‘* Bitwise operators allow evaluation and manipulation of specific bits within an integer. Operator | Name Example | Result & And Sx & Sy _| Bits that are set in both $x and Sy are set, i Or $x | Sy__| Bits that are set in either Sx or Sy are set. * Xor Sx” $y _| Bits that are set in $x or Sy but not both are set. = Not ~s Bits that are set in $x are not set, and vice versa, « Shiftleft | $x <<$y__| Shift the bits of $x $y steps to the left. > Shift right | $x>>$y__| Shift the bits of $x $y steps to the right. Precedence of PHP Operators © Operator precedence determines the grouping of terms in an expression. ‘* This affects how an expression is evaluated. * For example x = 7 + 3 * 2; Here x is assigned 13, not 20 because operator * has higher precedence than + 50 it first get multiplied with 32 and then adds into 7. Higher precedence operators will be evaluated first. Category Operator Asso Unary Te Right to left Multiplicative *7% Left to right Additive + Left to right | Relational <>= Left to right Equality Left to right Logical AND BR Left to right Logical OR i Left to right Conditional 2 Right to left Assignment 7 %6= _ | Right to left PHP Constants an identifier (name) for a simple value. The value cannot be changed during the script. DWPD (3350702) Prof. Payal M.Boda = eh ap | Unit 2 Working with Basic Building Dp Darshan Blocks of PHP # Avalid constant name starts with a letter or underscore (no $ sign before the constant name). * To create a constant, use the define() function «Syntax: define(name, value, case-insensitive) © name: Specifies the name of the constant © value: Specifies the value of the constant © case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false. Example 1: © The example below creates a constant with a case-sen: Output: PHP is a scripting language Example 2: © The example below creates a constant with a case-insensitive name. ing language"); Output: PHP is a scripting language constant() function ‘* The constant() function returns the value of a constant. * Syntax: constant(constant) © Constant: Required. Specifies the name of the constant. Example: Output: Hello you! How are you today? PHP Magic Constants (Predefined Constants) PHP pro es a large number of predefined constants to any script which it runs. * There are five magical constants that change depending on where they are used. For example, the value of __LINE__ depends on the line that it's used on in your script. These special constants are case-insensitive and are as follows: 18 | Dept: cE DWPD (3350702) Prof. Payal M.Boda D Sarchan | Unit 2 Working with Basic Building Darshan Blocks of PHP ‘A few "magical" PHP constants are given below: Name Deser UNE The current line number of the file. _FILE__ The full path and filename of the file. _FUNCTION___ | The function name. This constant returns the function name as it was, declared. _CLASS_ The class name. This constant returns the class name as it was declared. __METHOD___| The class method name. The method name is returned as it was declared. Flow control statements * Conditional statements are used to perform different actions based on different conditions © In PHP we have the following conditional statements: © if statement - executes some code only if a specified condition is true © if..else statement - executes code if a condition is true and another code if the condition is false Iseif....else statement - specifies a new condition to test, if the first condition is false © switch statement - selects one of many blocks of code to be executed if Statement + The if statement is used to execute some code only if a specified condition is true. * Syntax: if (condition) { code to be executed if condition is tru } Example: Output: Number is Even 19 | Dept: CE DWPD (3350702) Prof. Payal M.Boda = eh ap | Unit 2 Working with Basic Building Dp Darshan Blocks of PHP if..else Statement * Use the if....else statement to execute some code if a condition is true and another code if the condition is false * Syntax: if (condition) Ht code to be executed if condition is true; } else xt code to be executed if condition is false; } Example: <2php $number = 20; if (Snumber % 2 == 0) { echo "Number is Even"; } else { echo "Number is Odd"; } Es Output: Number is Even if...elsei! Ise Statement «Use the if...elseif..else statement to specify a new condition to test, if the first condition is false. Syntax: if (condition) { code to be executed if condition is true; } elseif (condition) { code to be executed if condition is true; } else 20 | Dept: CE DWPD (3350702) Prof. Payal M.Boda D Sarchan | Unit 2 Working with Basic Building Darshan Blocks of PHP { code to be executed if condition is false; } Example: $b && $a > Sc) { echo "a is max’; } elseif ($b > $c) { echo "a is max’; } else { echo "cis max"; } > Output: cis max switch Statement «The switch statement is used to perform different actions based on different conditions, ‘Use the switch statement to select one of many blocks of code to be executed. Syntax: switch (n) { case label1: code to be executed if n=label1; break; case label2: code to be executed if n=label2; break; case label3: code to be executed if n=label3; break; default: code to be executed if n is different from all labels; } How it works: First we have a single expression n (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each case in the structure. + Ifthere is a match, the block of code associated with that case is executed. ‘* Use break to prevent the code from running into the next case automatically. * The default statement is used if no match is found, 21 | Dept: CE DWPD (3350702) Prof. Payal M.Boda = eh ap | Unit 2 Working with Basic Building Dp Darshan Blocks of PHP Example: Output: Saturday Loops * You want the same black of code to run over and over again. Instead of adding several almost equal code-lines in a script, we can use loops to perform a task like this. * Loops execute a block of code while the specified condition is true. «In PHP, we have the following looping statements: © while - loops through a block of code as long as the specified condition is true. © do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true. © for- loops through a block of code a specified number of times. © foreach - loops through a block of code for each element in an array. while Loop + The while loop executes a block of code as long as the specified condition is true. Syntax: while (condition is true) Ht code to be executed; 22 | Dept: CE DWPD (3350702) Prof. Payal M.Boda D Sarchan | Unit 2 Working with Basic Building Darshan Blocks of PHP Example: "; Sut; > Output: The number is: The number is: The number is: The number is: The number is: wewne do...while Loop ‘© The do...while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true. «Syntax: do € code to be executed; J while (condition is true}; Example: "; Sx; }while ($x. > Output: The number is: The number is: The number is: The number is: The number is: weone 23 | Dept: CE DWPD (3350702) Prof. Payal M.Boda = eh ap | Unit 2 Working with Basic Building Dp Darshan Blocks of PHP for Loop + PHP for loops execute a block of code a spe 1d number of times. * The loop is used when you know in advance how many times the script should run. * Syntax: for (init counter; test counter; increment counter) it code to be executed; } «Parameters: © init counter: Initialize the loop counter value. © test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends. © increment counter: Increases the loop counter value. Example: "; 24 | Dept: CE DWPD (3350702) Prof. Payal M.Boda = eh ap | Unit 2 Working with Basic Building Dp Darshan Blocks of PHP > Output: red green blue yellow Break and Continue ‘+ The standard way to exit from a looping structure is for the main test condition to become false. ‘The special commands break and continue offer an optional side exit from all the looping constructs, including while, do-while, and for. «The break command exits the innermost loop construct that contains it. + The continue command skips to the current iteration of the loop that contains it. Example (Break): The following code prints the number Oto 5. 5) break; echo $i SisSitd; ) » Output: 0123456 Example (Continue): The following code prints the number 0 to 9, except 5. Output: 012346789 25 | Dept: CE DWPD (3350702) Prof. Payal M.Boda Darshan eae of ge Ae Dip Sade Unit 2 Working with Basic Building Blocks of PHP Difference between Break and Continue BREAK CONTINUE | Break is used to terminate the execution of the loop. Continue is not used to terminate the execution of loop. It breaks the iteration. It skips the iteration. When this statement is executed, control will come out from the loop and executes the statement immediate after loop. When this statement is executed, it will not come out of the loop but moves/jumps to the next iteration of loop. Break is used with loops as well as switch case. Continue is only used in loops, it is not used in | switch case. Example : Example : > Output 012 Output :01245 26 | Dept: CE DWPD (3350702) Prof. Payal M.Boda

You might also like