Mindmajix

Conditions and Loops in PHP

CONSTANT

DEFINE:-

Define is the pre-defined key-word in php. This will take two arguments. The first argument being the constant identifier which identifier its second argument value. We can pass third argument as Boolean to make case in-sensitive

Ex:-constant.php

<?php

define(‘x’,10,true);

echo x,'<br>’;

echo X;

echo ‘<hr>’;

define(‘url’,’http://www.socialhub.com’,true);

echo uRl,’/register.php’;

echo ‘<hr>’;

define(‘SITE_NAME’,’http://www.socialhub.in-hyderabad’,’Social Hub’);

?>

<h1>About us</h1>

we at <?php echo SITE_NAME;?> are in training ……..

<h1>Contact Us</h1>

<?php echo SITE_NAME;?>

street ……

<h1>FAQ</h1>

what we do at <?php echo SITE_NAME;

?>

CONDITIONS

Conditional statements are used to perform different actions based on different conditions

IF CONDITION:-

                                    IN PHP                                                  IN PERL

If (cond)                                               if (cond) :

{                                                                    //code

//code                                                 else if (code) :

}                                                                   //code

else if (cond)else (cond) :

{//code

//code end else if ;

}

else (cond)

{          //code            }

SWITCH CONDITION:-

Syntax: –

                                      switch (cond) :                                            switch  (cond):

{                                                                                        |

Case1 :                                                                                |

//code                                                                  |

Break;                                                                                 |

Case2:

//code                                                                   |

Break;                                               end switch;

|

Default:         //code       }

Ex: –

<html>  <body>
<?php
$x=1;
switch ($x)
{
case 1:
echo “Number 1”;   break;
case 2:
echo “Number 2”;   break;
case 3:
echo “Number 3”;   break;
default:
echo “No number between 1 and 3”;
}
?>
</body>   </html>

WHILE CONDITION:-

The while loop executes a block of code while a condition is true

Syntax: -While (cond)while (cond):

{//code inc/dec

//const code inc/dec                                                     endwhile

}

Ex: –

<html>
<body>
<?php
$i=1;
while($i<=5)
{
echo “The number is ” . $i . “<br />”;
$i++;
}
?>
</body>
</html>

 

OUTPUT:-

The number is 1
The number is 2
The number is 3
The number is 4
The number is 5

DO-WHILE CONDITION:- 

The do…while statement will always execute the block of code once, it will then check the condition, and repeat the loop while the condition is true.

Syntax: –

                                         Do                        do:

{                                                   //code                                   inc/dee

//code                                            endoowhile(cond);

}

While (cond)

Example

The example below defines a loop that starts with i=1. It will then increment i with 1, and write some output. Then the condition is checked, and the loop will continue to run as long as i is less than, or equal to 5:

<html>
<body>
<?php
$i=1;
do
{
$i++;
echo “The number is ” . $i . “<br />”;
}
while ($i<=5);
?>
</body>
</html>

OUTPUT:-

The number is 2
The number is 3
The number is 4
The number is 5
The number is 6

 

FOR LOOP:-

Loops execute a block of code a specified number of times, or while a specified condition is true. The for loop is used when you know in advance how many times the script should run.

Syntax: –

For(initialize;cond;inc/dec)               for(initialize;code;inc/dec

//code                                                                                   //code

end for

Example

The example below defines a loop that starts with i=1. The loop will continue to run as long as i is less than, or equal to 5. i will increase by 1 each time the loop runs:

<html>
<body>
<?php
for ($i=1; $i<=5; $i++)
{
echo “The number is ” . $i . “<br />”;
}
?>
</body>
</html>

OUTPUT:

The number is 1
The number is 2
The number is 3
The number is 4
The number is 5

 

Ex: -conditions.php

<?php

echo ‘<h1 align=”center”>PHP way of writing</h1>’;

if(false){

echo ‘First Line’;

}          else {

echo ‘Second Line’;

}

echo ‘<hr>’;

echo ‘<h1 align=”center”>PERL way of writing</h1>’;

if(false):

echo ‘First Line’;

else:

echo ‘Second Line’;

endif;

?>

FOR EACH:-

for each is usefull for all non-sequence data of an array. Associative array ,numerical array object data of an array

The general syntax is

foreach (arrayName as arr[value]))

{

echo arr [value];

}

Another syntax is

foreach (arrayname as key=>value)

{

echo key’-‘value         (or)        echo “key-value”;   }

Ex: -foreach.php

<?php

$a = array(“Name”=>”Rajesh”,”Age”=>30);

/*foreach(arrayname as value){

echo arr[value];

}*/

foreach($a as $v){

echo $v,'<br>’;

}echo ‘<hr>’;

/*foreach(arrayname as key=>value){

echo key,’ – ‘,value;

}*/

foreach($a as $k=>$v){

echo “$k – $v<br>”;

//echo $k,’ – ‘,$v,'<br>’;

}

?>

0 Responses on Conditions and Loops in PHP"

Leave a Message

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

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.
Course Adviser

Fill your details, course adviser will reach you.