PHP for Beginners – Part 2
Hey Guys !! In our Previous Blog PHP for Beginners – Part 1 you have learnt about the basics of variable, operators and data type, now its the time to put it in action.
This Blog includes: If, Switch, for, foreach, break, continue, ternary operator.
If Statement
In our daily life we need to make choices, choices when we eat, drink and many more countless things. Same is the case when we code we need to program in such a way where we can put the logic to adapt with the code.
The if statement executes a statement if the expression inside the parentheses evaluates to true, otherwise, the code is skipped. It may be a single statement followed by a semicolon or maybe a compound statement surrounded by curly braces. An else statement may appear immediately after the statement and have a statement of its own. It is executed only when the previous expression is false. In between an if statement and an elsestatement you may put as many elseif statements as you like. Each elseif expression is evaluated in turn, and control skips past those that are false. If an elseif statement evaluates to true, then the rest of the code in the greater if statement is skipped. That is, PHP accepts only one match.
Lets take a situation where you are hungry and you have $30 in your pocket. What would you do then get a coke and a hot dog. If you have $200 you would go to Mcdonalds or KFC. Now how do we do that through PHP.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<?php // syntax for if statement if (<expression is true>) code ; // for multi-line code or compound statements use braces if (<expression is true>){ statement; statement; } $money = 30; if ($money == 30 ){ echo "lets have coke and hot dog"; } // now lets see else clause $money = 10; if ($money > 25 ) { echo "can get lunch today"; } else { echo "lets have only bread today"; } // now check this multiple clause if (<expression is true>){ statement; statement; } else if (<expression is true>){ // if this expression is true it will execute this block and leave the rest. statement; statement; } else { // this block will execute if none of the above expressions are true statement; statement; } $money = 180; if ($money > 150 ) { echo "Lets party at Mcdonalds"; } else if ($money > 100) { echo "lets get a proper lunch today"; } else if ($money > 30 ) { echo "lets have a hot dog only"; } else { echo "got broke today, lets starve"; } ?> |
Lets now solve a problem to get a student Grade based on his/her marks. This is just a sample calculation. Hope you can do better than this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php // to get a student grade based on its marks. $marks = 68; $grade = ''; if ($marks >= 85 ) { $grade = 'A+'; } else if ($marks > 65 and $marks < 85 ) { $grade = 'A'; } else if ($marks > 40 and $marks < 65 ) { $grade = 'B'; } else if ($marks > 40 and $marks < 65 ) { $grade = 'C'; } else { $grade = 'E'; } echo "Student grade is: $grade"; ?> |
Ternary Operator
A Ternary operator is a shorthand for if statement, It takes 3 operands , condition , value for true, value for false.
1 2 3 4 5 6 7 8 9 10 11 |
<?php $student = 50; if ($student > 100) { echo "student strength is greater than hundred"; } else { echo "student strength is less than hundred"; } // using ternary operator echo ($student > 100) ? "student strength is greater than hundred" : "student strength is less than hundred"; ?> |
Switch Statement
Switch statement is similar to if statement. Switch compares a variables with many different values and executes the script on the basis of the values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
switch(<variable>) { case <value>: code; code; break; case <value>: case <value>: // for multiple case value checking having same results code; code; break; default: // just like the else clause in if statements code; code; break; // optional as this is the last statement of the switch statement } |
See how switch works.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php $money = 30; if ($money == 10 ) { echo "I have $10"; } else if ($money == 20 ) { echo "I have $20"; } else if ($money == 30 ) { echo "I have $30"; } else { echo "I have unlimited money"; } // how we use switch switch($money) { case 10: echo "I have $10"; break; case 20: echo "I have $20"; break; case 30: echo "I have $30"; break; default: echo "I have unlimited money"; break; } ?> |
When you just want to compare a variable value you can use if or switch whichever you are comfortable with, but when you have to check variable on complex operation like greater than, equals, or combining multiple condition the use if statement.
Loops
Definition: Repetition of some instruction or code continuously until some exit condition is met.
Explanation: If a part of a code or some function is continuously repeated that state is called as loop. E.g. you play your playlist over and over again.
If an exit condition is not met the loops continues forever or until the CPU memory exhaust. The state is called infinitive loop. Three steps are mandatory for efficient working of a loop. i.e initialization, condition and iteration
For loops
1 2 3 4 |
for (<initialization>; <condition>; <increment or decrement>) { statement; statement; } |
1 2 3 4 5 6 7 8 9 |
<?php for($progressBar = 0; $progressBar <= 100; $progressBar++) { echo "progressbar progress : ". $progressBar."<br>"; } for($progressBar = 100; $progressBar >= 0; $progressBar--) { echo "progressbar progress : ". $progressBar."<br>"; } ?> |
While loop
While loop works in the same way as for loop with some structural changes.
1 2 3 4 5 6 |
<initialization> while(<condition>) { statement; statement; <increment or decrement>) } |
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $progressBar = 0; while( $progressBar <= 100 ) { echo "progressbar progress : ". $progressBar."<br>"; $progressBar++; } $progressBar = 100; while( $progressBar >= 0 ) { echo "progressbar progress : ". $progressBar."<br>"; $progressBar--; } ?> |
do-while loop
do-while works in the same was as while loop except the condition is is checked at the end of each iteration instead of in the beginning.
1 2 3 4 5 6 |
<initialization> do { statement; statement; <increment or decrement>) }while(<condition>); |
1 2 3 4 5 6 7 8 |
<?php $progressBar = 0; do { echo "progressbar progress : ". $progressBar."<br>"; $progressBar++; } while($progressBar <= 100); ?> |
foreach loop
Foreach works only for arrays and objects.
1 2 3 4 5 6 7 8 9 |
foreach (array_expression as $value) { statement statement } foreach (array_expression as $key => $value) { statement statement } |
1 2 3 4 5 6 |
<?php $arrayVariable = array(10,20,30,40,50); foreach ($arrayVariable as $key => $value) { echo "key: $key value: $value"."<br>" ; } ?> |
Nested loops
loop within loop is called nested loop.
1 2 3 4 5 6 7 |
<?php for($student = 1; $student <= 3; $student++) { for($classes = 1; $classes <= 4; $classes++) { echo "Student no $student has attended class $classes <br>"; } } ?> |
Break and Continue
Break ends or breaks out the current execution for loops or constructs. Continue skips the current execution and starts with next iteration. Both are used with for, foreach, while, do-while, if or switch structure.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php for($student = 1; $student <= 10; $student++) { if ($student == 8 ) break; // breaks out of the loop echo "Addmission posible for student# $student <br>"; } for($student = 1; $student <= 10; $student++) { if ($student == 8 ) continue; // breaks out of the loop echo "Addmission posible for student# $student <br>"; } ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
for($student = 1; $student <= 3; $student++) { if ($student == 2 ) break; // breaks out from the main loop for($classes = 1; $classes <= 4; $classes++) { echo "Student no $student has attended class $classes <br>"; } } for($student = 1; $student <= 3; $student++) { if ($student == 2 ) continue; // skips the current segment and start with next iteration for($classes = 1; $classes <= 4; $classes++) { echo "Student no $student has attended class $classes <br>"; } } |
and Thats all for This Tutorial. Hope you guys enjoy this. cheers 🙂