under web technology subject. decision statements and looping statements in Php server-side language. very close to C language's statements.
Size: 365.13 KB
Language: en
Added: Feb 23, 2020
Slides: 15 pages
Slide Content
Decision and looping examples with PHP Kunjan Shah TY CE 2 Batch C 170410107103
Introduction to PHP PHP stands for Hypertext Preprocessor. PHP is a server side scripting languages that allows you to create dynamic web pages. It is free to download and use. You can embed PHP scripting within normal html coding. It is mainly used for form handling and database access. The extension to the PHP files are .php, .php3 or .phtml and .php4 as well.
Basics of PHP PHP code is contained within tags <?php ?> or short open: <? ?> PHP with HTML echo is a command in PHP for writing output data to the browser, and at the end of each PHP statement we require to write a ; (semicolon) as you see in the below example code. Otherwise, if you write another statement, then PHP will report a syntax error. <?php echo "Hello World"; ?> </body> Comments // for single line /* */ for multiline
DECISION MAKING STATMENTS The if, elseif ...else and switch statements are used to take decision based on the different condition. You can use conditional statements in your code to make your decisions. PHP if..else statement : PHP if statement is used to execute some code, if a condition is evaluated to true otherwise if the condition is evaluated to false, execute nothing, or execute some other code.
The If...Else Statement
The ElseIf Statement If you want to execute some code if one of the several conditions are true use the elseif statement
The Switch Statement If you want to select one of many blocks of code to be executed, use the Switch statement. The switch statement is used to avoid long blocks of if..elseif..else code. The switch statement works in an unusual way. First it evaluates given expression then seeks a label to match the resulting value. If a matching value is found then the code associated with the matching label will be executed or if none of the label matches then statement will execute any specified default code.
The Switch Statement
Looping Statements Loops in PHP are used to execute the same block of code a specified number of times. PHP supports following four loop types. for − loops through a block of code a specified number of times. while − loops through a block of code if and as long as a specified condition is true. do...while − loops through a block of code once, and then repeats the loop as long as a special condition is true. foreach − loops through a block of code for each element in an array.
The for loop statement
The while loop statement
The do...while loop statement
The foreach loop statement The foreach statement is used to loop through arrays. It loops over the array, and each value for the current array element is assigned to $value, and the array pointer is advanced by one to go the next element in the array.