0
PHP Course
2: Conditional Statements
PHP Course

Info@ITBigDig.com
Instructor
 Name: Mohamed Saad.
 Email: Engsaad_aly@hotmail.com
 Occupation: Web Developer In IT Big Dig.
PHP Course

I...
Contents
• PHP Array
o Introduction.

• Conditional Statements.
o if statement.
o if...else statement.
o if...else if....e...
PHP Array
• There are three types of arrays:
Indexed arrays

•Arrays with numeric index

Associative arrays

• Arrays with...
Array Function
Syntax

indexed
associative

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

Copy Code
outputdata.php

$y = array ('name' =...
Conditional Statements
1) if statement - executes some code only if a specified

condition is true.
2) if...else statement...
if statement
Syntax
o if (condition)
{
code to be executed if condition is true;
}

PHP Course

Info@ITBigDig.com
Example: if statement
HTML

PHP

Dig: Check value entered by user.
<html>
<body>

Copy Code
game.php

<form action="game.php" method="post">
Enter number between 1 and 100:<input type="text...
Getdate Function
Syntax
<?php

timestamp

print_r(getdate( ));

?>
o getdate(timestamp);
• Timestamp
o Default (time())
o ...
Key
"seconds"
"minutes"
"hours"
"mday"
"wday"
"mon"

Returned Values
0 to 59
0 to 59
0 to 23
1 to 31
0 (for Sunday)
1 thro...
<pre>
Copy Code
<?php
Conditional_statement.php
print_r(getdate());
echo '<br/>';
$mydate=getdate(date(time()));
echo"----...
localtime Function
Syntax
<?php
print_r(localtime());

?>
Is Associative
Is Associative
• If set to FALSE or not supplied then the array is
returned as a regular, numerically indexed array. If
th...
if...else Statement
Syntax
if (condition)
{
code to be executed if condition is true;
}
else
{
code to be executed if cond...
<?php

Copy PHP Code

if (empty($_POST)===false)
{
$x=$_POST['x'];
if ($x>50){
echo 'U Entered Number ',$x,' greater than ...
if...else if....else Statement
Syntax
if (condition) {
} else if (condition) {
} elss {
}
<html>
Copy Code
<body>
game.php
<form action="game.php" method="post">
Enter number between 1 and 100:<input type="text" ...
I think it`s clear now…!

PHP Course

Info@ITBigDig.com
Date Function

Syntax
<?php

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

H

24-hour format

00 through 23
Switch statement
Syntax
o switch (n)
{
case label1:
code to be executed if n=label1;
break;
case label2:
code to be execut...
Switch statement
Power Syntax
o switch (variable)
{
case (condition):
code to be executed if condition is true;
break;
cas...
<?php
$x="red";
switch ($x)
{
case "blue":
echo "Your favorite color is blue!";
break;
case "red":
echo "_________________...
H

24-hour format

00 through 23
<?php

Copy Code
switch.php

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

Description

Expected O/P

d

Day of the month,

j

Day of the month without leading
zeros

D
l

A textual repr...
For complete reference Go To
http://php.net/manual/en/function.date.
php
PHP Course

Info@ITBigDig.com
End
Conditional Statements
We hope You enjoy This Tutorial.
For any Suggestions Please Email Us
Info@ITBigDig.com

PHP Cou...
Conditional Statementfinal PHP 02
Conditional Statementfinal PHP 02
Conditional Statementfinal PHP 02
Conditional Statementfinal PHP 02
Conditional Statementfinal PHP 02
Conditional Statementfinal PHP 02
Upcoming SlideShare
Loading in...5
×

Conditional Statementfinal PHP 02

375

Published on

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.

Published in: Education, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
375
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
33
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Conditional Statementfinal PHP 02"

  1. 1. PHP Course 2: Conditional Statements PHP Course Info@ITBigDig.com
  2. 2. Instructor  Name: Mohamed Saad.  Email: Engsaad_aly@hotmail.com  Occupation: Web Developer In IT Big Dig. PHP Course Info@ITBigDig.com
  3. 3. 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 Info@ITBigDig.com
  4. 4. PHP Array • There are three types of arrays: Indexed arrays •Arrays with numeric index Associative arrays • Arrays with named keys Multidimensional arrays •Arrays containing one or more arrays
  5. 5. Array Function Syntax indexed associative •array(‘value’, ); •array(‘key’ =>’value’, );
  6. 6. <pre> <?php $x = array ('saad','nacer city','zkzk@hotmail'); print_r ($x); Copy Code outputdata.php $y = array ('name' => 'saad', 'address' => 'nacer city', 'email' => array ('zkzk@hotmail', 'zkzk@yahoo', 'zkzk@gmail')); print_r ($y); ?> </pre> PHP Course Info@ITBigDig.com
  7. 7. 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.
  8. 8. if statement Syntax o if (condition) { code to be executed if condition is true; } PHP Course Info@ITBigDig.com
  9. 9. Example: if statement
  10. 10. HTML PHP Dig: Check value entered by user.
  11. 11. <html> <body> Copy Code game.php <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> <?php if (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(); } } ?> PHP Course Info@ITBigDig.com
  12. 12. Getdate Function Syntax <?php timestamp print_r(getdate( )); ?> o getdate(timestamp); • Timestamp o Default (time()) o “U” Specifies an integer Unix timestamp
  13. 13. Key "seconds" "minutes" "hours" "mday" "wday" "mon" Returned Values 0 to 59 0 to 59 0 to 23 1 to 31 0 (for Sunday) 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.
  14. 14. <pre> Copy Code <?php Conditional_statement.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> PHP Course Info@ITBigDig.com
  15. 15. localtime Function Syntax <?php print_r(localtime()); ?>
  16. 16. Is Associative
  17. 17. 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.
  18. 18. if...else Statement Syntax if (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; } PHP Course Info@ITBigDig.com
  19. 19. <?php Copy PHP Code if (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(); } } ?>
  20. 20. if...else if....else Statement Syntax if (condition) { } else if (condition) { } elss { }
  21. 21. <html> Copy Code <body> game.php <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(); } } ?>
  22. 22. I think it`s clear now…! PHP Course Info@ITBigDig.com
  23. 23. Date Function Syntax <?php echo date("H:i:s"); ?> H 24-hour format 00 through 23
  24. 24. Switch statement Syntax 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; }
  25. 25. Switch statement Power 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; }
  26. 26. <?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 Code switch2.php ?> PHP Course Info@ITBigDig.com
  27. 27. H 24-hour format 00 through 23
  28. 28. <?php Copy Code switch.php $hours = date("H"); $hours=$hours-2; echo $hours,date(":i:s"); echo "<br/>"; ?> <?php switch ($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"; } ?> PHP Course Info@ITBigDig.com
  29. 29. Character Description Expected O/P d Day of the month, j Day of the month without leading zeros D l A textual representation of a day 2 digits Returned Values 01 to 31 1 to 31 Three letters Mon through Sun A full textual representation of the day Sunday through of the week it is lowercase “L” Saturday N ISO-8601 numeric representation of the day of the week (PHP 5.1.0) S English ordinal suffix for the day of the month, z The day of the year 1 digit 1 (for Monday) through 7 (for Sunday) 2 characters st, nd, rd or th. 0 through 365
  30. 30. For complete reference Go To http://php.net/manual/en/function.date. php PHP Course Info@ITBigDig.com
  31. 31. End Conditional Statements We hope You enjoy This Tutorial. For any Suggestions Please Email Us Info@ITBigDig.com PHP Course Info@ITBigDig.com
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×