PHP for Beginners – Part 2

Want create site? Find Free WordPress Themes and plugins.

Hey guys! In my last 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.

Scope of 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 as per requirement.

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 else statement 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 is skipped.

Let’s take a situation where you are hungry and you have $30 in your pocket you get a coke and a hot dog, If you have $200 you would go to KFC. Let’s apply this logic with PHP programming.

Let us now solve aother problem to get a student Grade based on his/her marks. This is just a simple 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.

Working example of switch case.

When do we choose between if and switch statement?

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 then 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 just different syntax.

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 are called nested loops.

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.

NOTE: Do not just read and move on. Please run the codes in your system and play with them. This is the only way you will memorize and learn.

Did you find apk for android? You can find new Free Android Games and apps.

About Author

Entrepreneur & Dreamer I am a passionate Software Professional, love to learn and share my knowledge with others. Software is the hardware of my life.

Leave A Reply

CommentLuv badge

This site uses Akismet to reduce spam. Learn how your comment data is processed.