Php Statement & Loop Samples

Only available on StudyMode
  • Topic: For loop, Foreach, Do while loop
  • Pages : 15 (989 words )
  • Download(s) : 18
  • Published : December 1, 2012
Open Document
Text Preview
IF STATEMENT

SAMPLE 1
<?php

$studentId = 'A1110601';
if ( $studentId == 'A1110601' )
{
echo ' Student ID: '.$studentId.'<br/>'.
' Welcome! Inah Ysabela Marie D. Alegre';
}

?>

SAMPLE 2
<?php

$birthdate = 'April 8, 1995';
if ( $birthdate == 'April 8, 1995' )
{
echo ' Birthdate : '.$birthdate;
}

?>

SAMPLE 3
<?php

$grade = 86;
if ( $grade == 86 )
{
echo ' Passed! Your grade is '.$grade;
}

?>

SAMPLE 4
<?php

$employee = 'Mary Jane Santos';
if ( $employee == 'Mary Jane Santos' )
{
echo $employee.' you are hired! ';
}

?>

SAMPLE 5
<?php

$university = 'University of Makati' ;

if ( $university == 'University of Makati' )
{
echo 'I am studying at '.$university;
}

?>

IF … ELSE STATEMENT

SAMPLE 1
<?php

$gender = 'Female';
if ($gender == 'Female')
{
echo ' Beautiful lady';
}
else
{
echo ' Handsome man';
}

?>

SAMPLE 2
<?php

$grade = 65;
if ($grade >= 80)
{
echo 'Congratulations, you passed!';
}
else
{
echo ' Sorry, you failed!';
}

?>

SAMPLE 3
<?php

$items = 125;
if ($items >= 100)
{
echo ' Over stock!';
}
else
{
echo ' Out-of-stock!';
}

?>

SAMPLE 4
<?php

$num = 19;
if ($num % 2 == 0)
{
echo ' Number is Even!';
}
else
{
echo ' Number is Odd!';
}

?>

SAMPLE 5
<?php

$age = 36 ;
if ($age >= 30)
{
echo ' You are an adult.';
}
else
{
echo ' You are a teenager.';
}

?>

IF … ELSE IF … ELSE STATEMENT

SAMPLE 1
<?php

$mysteryGift = ‘choice2’;
if ( $mysteryGift == ‘choice1’ )
{
echo ‘A brown teddy bear.’;
}
elseif ( $mysteryGift == ‘choice2’ )
{
echo ‘A pink rabbit.’;
}
else
{
echo ‘A white panda.’;
}

?>

SAMPLE 2
<?php

$order = ‘2 choco honey dip’;
if ( $order == ‘2 choco honey dip’ )
{
echo ‘Your bill is 40.00 ‘;
}
elseif ( $order == ‘5 munchkin’ )
{
echo ‘Your bill is 17.00 ‘;
}
else
{
echo ‘You must order first!’;
}

?>

SAMPLE 3
<?php

$favoriteFruit = ‘Lychee’;
if ( $favoriteFruit == ‘Pomegranate’ )
{
echo ‘Your favorite fruit contains around 7% fibre.’; }
elseif ( $favoriteFruit == ‘Lychee’ )
{
echo ‘Your favorite fruit contains around 1.5% fibre.’; }
else
{
echo ‘Sorry, I don't know how much fibre that fruit contains’; }

?>

SAMPLE 4
<?php

$chef = ‘Chef Cheryl’;
if ( $chef == ‘Chef Khaye’ )
{
echo ‘Try my very creamy and delicious carbonara.’;
}
elseif ( $chef == ‘Chef Cheryl’ )
{
echo ‘Grab and taste my charlie chan pasta.’;
}
else
{
echo ‘Not in the list.’;
}

?>

SAMPLE 5
<?php

$prize = ‘prize3’;
if ( $prize == ‘prize1’ )
{
echo ‘Trip to Boracay for 2weeks.’;
}
elseif ( $prize == ‘prize2’ )
{
echo ‘Trip to HongKong DisneyLand for 1week.’;
}
else
{
echo ‘Trip to Makati Park for free.’;
}

?>

WHILE LOOP

SAMPLE 1
<?php

$i=1;
while($i<=5)
{
echo ‘The number is ‘ . $i . ‘<br>‘;
$i++;
}

?>

SAMPLE 2
<?php

$brush_price = 5;
$counter = 10;

echo ‘<table border=\’1\’ align=\’center\’>‘; echo ‘<tr><th>Quantity</th>‘;
echo ‘<th>Price</th></tr>‘;
while ( $counter <= 100 ) {
echo ‘<tr><td>‘;
echo $counter;
echo ‘</td><td>‘;
echo $brush_price * $counter;
echo ‘</td></tr>‘;
$counter = $counter + 10;
}
echo ‘</table>‘;

?>

SAMPLE 3
<?php

$num = 1;
while ( $num <=10 )
{
if ($num < 5)
{
print $num . ‘ is less than 5 <br>‘;
}
else
{
print $num . ‘ is not less than 5 <br>‘;
}
$num++;
}

?>

SAMPLE 4
<?php

for ($num=1; $num <= 10; $num++ )
{
print...
tracking img