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.

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.

Ternary Operator

A Ternary operator is a shorthand for if statement, It takes 3 operands , condition , value for true, value for false.

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.

See how switch works.

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.


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

While loop

While loop works in the same way as for loop with some structural changes.

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.

foreach loop

Foreach works only for arrays and objects.

Nested loops

loop within loop is called nested loop.

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.

and Thats all for This Tutorial. Hope you guys enjoy this. cheers 🙂

Was this article/content helpful?
Thanks! Your feedback helps us to improve

Leave a Reply

Your email address will not be published. Required fields are marked *