conditional statementfinal php 02
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
PHP Course2: Conditional Statements
PHP Course [email protected]
Instructor Name: Mohamed Saad.
Email: [email protected]
Occupation: Web Developer In IT Big Dig.
PHP Course [email protected]
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]
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
Array FunctionSyntax
•array(‘value’, );indexed
•array(‘key’ =>’value’, );
associative
<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]
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.
if statementSyntax
o if (condition) { code to be executed if condition is true; }
PHP Course [email protected]
Example: if statement
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
<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]
Getdate Function
o getdate(timestamp);
• Timestamp
o Default (time())
o “U” Specifies an integer Unix timestamp
Syntax
<?php
print_r(getdate( ));
?>
timestamp
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.
<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]
localtime FunctionSyntax
<?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 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.
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]
<?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
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;
}
<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
Date FunctionSyntax
<?php
echo date("H:i:s");
?>
11:55:44
H 24-hour format 00 through 23
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;}
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;}
<?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]
H 24-hour format 00 through 23
<?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]
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
For complete reference Go To
http://php.net/manual/en/function.date.php
outsourcing
http://php.net
PHP Course [email protected]
We hope You enjoy This Tutorial.
For any Suggestions Please Email Us
PHP Course [email protected]
EndConditional Statements