Free WebSchools.com

Google




Blog ForumsLive chat WebhostingLink TO UsSEO ToolsResources


HOME

PHP Introduction
Php Installation
PHP Basic Scripts
PHP variables
PHP operators
PHP Conditional statement
PHP functions
PHP Arrays

PHP Objects & classes
PHP with Forms
PHP Files
PHP With Database
PHP session
PHP cookies
PHP Regular Expressions
PHP Server Environment
PHP Graphics
PHP Advance

PHP CONDITIONAL STATEMENT



most scripts evalute conditions and change their behaviur accordingly.the facility to make desisions makes your PHP pages dynamic ,capable of changing their output according to circumstances.like most programming language PHP allows you to do this with an if statement.



IF statement

  • if...else statement - use this statement if you want to execute a set of code when a condition is true and another if the condition is not true
  • elseif statement - is used with the if...else statement to execute a set of code if one of several condition are true

example 1

if (expression) { //code to execute if the expression evaluates to true }

example 2

if (condition) code to be executed if condition is true; else code to be executed if condition is false;

IF example

output
i am in good mood

IF-ELSE example

output
not happy but sad

PHP 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.
Syntax
switch (expression)
{
case label1:
  code to be executed if expression = label1;
  break;  
case label2:
  code to be executed if expression = label2;
  break;
default:
  code to be executed
  if expression is different 
  from both label1 and label2;
} 

how this work?


If the first expression does not evaluate the the first block of code ingnored.The elseif clause then causes another expression to be evaluated.once again ,if this expresion evaluates to true,then the second block of code is execuated.otherwise the block of code associated with the else clause is executed.you can include as many as else if clauses as you want.if you dont want default action,you can omit the else clause.

PHP Loops



Looping statements in PHP are used to execute the same block of code a specified number of times.Loop statements are designed to enable you to achieve repetitive tasks.Almost without exception,a loop continues to operate until a condition is achieved,or you explicitly choose to exit the loop.

WHILE Statement

the while statement looks similar to like this
while(expression) { //do something }

As long as a while statement experssion evaluates to true, the code block is executed over and over again.
OUTPUT
1
2
3
4
5

DO......WHILE Statement



Do..while statement looks little like a while statemnt turned on its head.the essential difference between the two is that the code block is executed before the truth test and not after it.
example do{ //code to executed } while(expression);
output 1
2
3
4
5

PHP for statement



The for statement is used to repeat someting you want to execute a statement or a list of statements.
< b>Syntax
for (initialization; condition; increment) { code to be executed; }
output
welcome to webschools!
welcome to webschools!
welcome to webschools!
welcome to webschools!
welcome to webschools!