conditional statementfinal php 02

37
PHP Course 2: Conditional Statements PHP Course [email protected]

Upload: it-big-dig

Post on 25-May-2015

339 views

Category:

Education


1 download

DESCRIPTION

PHP course lecture 2 Download Source file if u lazy http://www.2shared.com/file/G4LVMn5y/sources_php_02.html Contents PHP Array Introduction. Conditional Statements. if statement. if...else statement. if...else if....else statement. switch statement.

TRANSCRIPT

Page 1: Conditional Statementfinal PHP 02

PHP Course2: Conditional Statements

PHP Course [email protected]

Page 2: Conditional Statementfinal PHP 02

Instructor Name: Mohamed Saad.

Email: [email protected]

Occupation: Web Developer In IT Big Dig.

PHP Course [email protected]

Page 3: Conditional Statementfinal PHP 02

Contents• PHP Array

o Introduction.

• Conditional Statements.o if statement. o if...else statement.

o if...else if....else statement.

o switch statement.

PHP Course [email protected]

Page 4: Conditional Statementfinal PHP 02

PHP Array• There are three types of arrays:

•Arrays with numeric indexIndexed arrays

• Arrays with named keysAssociative arrays

•Arrays containing one or more arrays

Multidimensional arrays

Page 5: Conditional Statementfinal PHP 02

Array FunctionSyntax

•array(‘value’, );indexed

•array(‘key’ =>’value’, );

associative

Page 6: Conditional Statementfinal PHP 02
Page 7: Conditional Statementfinal PHP 02

<pre><?php$x = array ('saad','nacer city','zkzk@hotmail');print_r ($x);

$y = array ('name' => 'saad', 'address' => 'nacer city', 'email' => array ('zkzk@hotmail', 'zkzk@yahoo', 'zkzk@gmail'));print_r ($y);?></pre>

Copy Codeoutputdata.php

PHP Course [email protected]

Page 8: Conditional Statementfinal PHP 02

Conditional Statements

1) if statement - executes some code only if a specified

condition is true.

2) if...else statement - executes some code if a

condition is true and another code if the condition is

false.

3) if...else if....else statement - selects one of several

blocks of code to be executed.

4) switch statement - selects one or more of many

blocks of code to be executed.

Page 9: Conditional Statementfinal PHP 02

if statementSyntax

o if (condition)  {  code to be executed if condition is true;  }

PHP Course [email protected]

Page 10: Conditional Statementfinal PHP 02

Example: if statement

Page 11: Conditional Statementfinal PHP 02

Input Data from user using text-

box

Check text-box

value

Output data dependent

on conditiona

l statement

Dig: Check value entered by user.

HTML PHP

Page 12: Conditional Statementfinal PHP 02
Page 13: Conditional Statementfinal PHP 02

<html> <body> <form action="game.php" method="post"> Enter number between 1 and 100:<input type="text" name="x"/> <input type="submit" name="submit" value="xxx"/> </form> </body></html>

<?phpif (empty($_POST)===false)

{$x=$_POST['x'];

if($x>50) {echo 'U Entered Number ',$x,' greater than 50','<br>';echo 'U Entered Number '.$x.' greater than 50';exit();}

}?>

Copy Codegame.php

PHP Course [email protected]

Page 14: Conditional Statementfinal PHP 02

Getdate Function

o getdate(timestamp);

• Timestamp

o Default (time())

o “U” Specifies an integer Unix timestamp

Syntax

<?php

print_r(getdate( ));

?>

timestamp

Page 15: Conditional Statementfinal PHP 02
Page 16: Conditional Statementfinal PHP 02

Key Returned Values

"seconds" 0 to 59

"minutes" 0 to 59

"hours" 0 to 23

"mday" 1 to 31

"wday" 0 (for Sunday)

"mon" 1 through 12

"year" Examples: 1970 or 2011

"yday" 0 through 365

"weekday" Sunday through Saturday

"month" January through December

0 Unix Epoch: is a system for describing points in time-2147483648 through 2147483647.

Page 17: Conditional Statementfinal PHP 02

<pre><?php

print_r(getdate());echo '<br/>';$mydate=getdate(date(time()));echo"--------------------------------------------";echo"<br/>";echo "date: $mydate[weekday], $mydate[month] $mydate[mday], $mydate[year]";echo"<br/>";echo "time: H:$mydate[hours], M:$mydate[minutes], S:$mydate[seconds]";echo"<br/>";

if ($mydate[“mday”]>=20) {

echo"--------------------------------------------"; echo"<br/>"; echo "we are at the end of the month";

}?>

</pre>

Copy CodeConditional_statement.php

PHP Course [email protected]

Page 18: Conditional Statementfinal PHP 02

localtime FunctionSyntax

<?php

print_r(localtime());

?>

Page 19: Conditional Statementfinal PHP 02

Is Associative

Page 20: Conditional Statementfinal PHP 02

Is Associative• If set to FALSE or not supplied then the array is

returned as a regular, numerically indexed array.

If the argument is set to TRUE then localtime()

returns an associative array containing all the

different elements of the structure returned by

the C function call to localtime.

Page 21: Conditional Statementfinal PHP 02

if...else StatementSyntax

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

PHP Course [email protected]

Page 22: Conditional Statementfinal PHP 02
Page 23: Conditional Statementfinal PHP 02

<?phpif (empty($_POST)===false)

{$x=$_POST['x'];

if ($x>50){echo 'U Entered Number ',$x,' greater

than 50','<br>';exit();}

else{echo 'U Entered Number ',$x,' less than

50','<br>';exit();}

}

?>

Copy PHP Code

Page 24: Conditional Statementfinal PHP 02

if...else if....else Statement

Syntax

if (condition) {

  code to be executed if condition is true;

  } else if (condition) {

  code to be executed if condition is true;

  } elss {

  code to be executed if condition is false;

 }

Page 25: Conditional Statementfinal PHP 02
Page 26: Conditional Statementfinal PHP 02

<html><body>

<form action="game.php" method="post">Enter number between 1 and 100:<input type="text" name="x"/><input type="submit" name="submit" value="Go"/>

</form></body>

</html><?php

if (empty($_POST)===false){

$x=$_POST['x'];if($x>50){

echo 'U Entered Number ',$x,' greater than 50','<br>';exit();

}else if($x<50){echo 'U Entered Number ',$x,' less than 50','<br>';exit();

}else{

echo 'U Entered Number ',$x;exit();

}}

?>

Copy Codegame.php

Page 27: Conditional Statementfinal PHP 02

If(){}

I think it`s clear now…!

PHP Course [email protected]

Page 28: Conditional Statementfinal PHP 02

Date FunctionSyntax

<?php

echo date("H:i:s");

?>

11:55:44

H 24-hour format 00 through 23

Page 29: Conditional Statementfinal PHP 02

Switch statementSyntax

o switch (n){case label1:  code to be executed if n=label1;  break;case label2:  code to be executed if n=label2;  break;default:  code to be executed if n is different from both label1 and label2;}

Page 30: Conditional Statementfinal PHP 02

Switch statementPower Syntax

o switch (variable){case (condition):  code to be executed if condition is true;  break;case (condition):  code to be executed if condition is true;  break;default:  All conditions are false;}

Page 31: Conditional Statementfinal PHP 02
Page 32: Conditional Statementfinal PHP 02

<?php$x="red";switch ($x){

case "blue": echo "Your favorite color is blue!"; break;case "red": echo "___________________<br>"; echo "Your favorite color is red!"; break;case "green": echo "Your favorite color is green!"; break;default: echo "no matching color!";

}?>

Copy Codeswitch2.php

PHP Course [email protected]

Page 33: Conditional Statementfinal PHP 02

H 24-hour format 00 through 23

Page 34: Conditional Statementfinal PHP 02

<?php$hours = date("H");$hours=$hours-2;echo $hours,date(":i:s");echo "<br/>";

?>

<?phpswitch ($hours){

case ($hours>5 and $hours<6):echo "it is the time of the TV News <br/>";

case ($hours>=6 and $hours<8): echo "Read something<br/>";

case ($hours>=8 and $hours<10): echo "PlayStation time<br/>";

default: echo "GoodNight";

}?>

Copy Codeswitch.php

PHP Course [email protected]

Page 35: Conditional Statementfinal PHP 02

Character Description Expected O/P

Returned Values

d Day of the month, 2 digits 01 to 31

j Day of the month without leading zeros

1 to 31

DA textual representation of a day

Three letters

Mon through Sun

l A full textual representation of the day of the week it is lowercase “L”

Sunday through Saturday

NISO-8601 numeric representation of the day of the week (PHP 5.1.0)

1 digit1 (for Monday) through 7 (for Sunday)

SEnglish ordinal suffix for the day of the month,

2 characters

st, nd, rd or th.

z The day of the year 0 through 365

Page 36: Conditional Statementfinal PHP 02

For complete reference Go To

http://php.net/manual/en/function.date.php

outsourcing

http://php.net

PHP Course [email protected]

Page 37: Conditional Statementfinal PHP 02

We hope You enjoy This Tutorial.

For any Suggestions Please Email Us

[email protected]

PHP Course [email protected]

EndConditional Statements