GeshanManandhar.com 1
Day 2Day 2 PHP PHP
Geshan ManandharGeshan ManandharDeveloper, Developer,
Young Innovations Pvt. LimitedYoung Innovations Pvt. Limitedwww.geshanmanandhar.comwww.geshanmanandhar.com
http://www.php.net
GeshanManandhar.com 2
Variables in PHPVariables in PHP
►Boolean: It can be either Boolean: It can be either TRUETRUE or or FALSEFALSE. . ► Integer: The size of an integer is platform-Integer: The size of an integer is platform-
dependent, although a maximum value of dependent, although a maximum value of about two billion is the usual value.about two billion is the usual value.
►Float: The size of a float is platform-Float: The size of a float is platform-dependent, although a maximum of dependent, although a maximum of ~1.8e308 with a precision of roughly 14 ~1.8e308 with a precision of roughly 14 decimal digits is a common value decimal digits is a common value
GeshanManandhar.com 3
Variable in PHPVariable in PHP
►String: A string is series of characters. String: A string is series of characters. In PHP, a character is the same as a In PHP, a character is the same as a byte, that is, there are exactly 256 byte, that is, there are exactly 256 different characters possible. different characters possible.
►Array: An array in PHP is actually an Array: An array in PHP is actually an ordered map. A map is a type that ordered map. A map is a type that maps maps valuesvalues to to keyskeys. . $names[1] = “Ram”;$names[1] = “Ram”; $names[2] = “Shyam”;$names[2] = “Shyam”;
GeshanManandhar.com 4
Variables in PHPVariables in PHP
►Objects: mainly have attributes and Objects: mainly have attributes and functions.functions.
<?php<?phpclass testclass test{{ function do_test()function do_test() {{ echo "Testing class function."; echo "Testing class function."; }}}}
$an_object = new test;$an_object = new test;$an_object->do_test();$an_object->do_test();
?> ?>
GeshanManandhar.com 5
Naming variables in PHPNaming variables in PHP
►Variables in PHP are represented by a Variables in PHP are represented by a dollar sign followed by the name of the dollar sign followed by the name of the variable. The variable name is case-variable. The variable name is case-sensitive. sensitive.
►A valid variable name starts with a letter A valid variable name starts with a letter or underscore, followed by any number or underscore, followed by any number of letters, numbers, or underscores. of letters, numbers, or underscores. $a_5=10; //valid$a_5=10; //valid $#b = 20; //invalid$#b = 20; //invalid
►Predefined Variables ($_SERVER, $_POST)Predefined Variables ($_SERVER, $_POST)
GeshanManandhar.com 6
ConstantConstant
► You can define a constant by using the define() -You can define a constant by using the define() -function. Once a constant is defined, it can never be function. Once a constant is defined, it can never be changed or undefined. changed or undefined.
<?php<?phpdefine("PI", 3.1415); //defining a constant called PIdefine("PI", 3.1415); //defining a constant called PI
$r=5; //r for radius$r=5; //r for radius$area = PI*$r*$r; //area calculation of circle$area = PI*$r*$r; //area calculation of circle
print "Areas of circle with radius $r is ".$area.".";print "Areas of circle with radius $r is ".$area.".";//user number_format() yourself.//user number_format() yourself.?>?>
GeshanManandhar.com 7
Arithmetic OperatorsArithmetic Operators
See Program for more clarification: Day02\prog06_Operators.php
GeshanManandhar.com 8
Comparison OperatorsComparison Operators
See code of Day02\prog08_comparision_operators.php
GeshanManandhar.com 9
Increment Decrement Increment Decrement OperatorsOperators
► ++ and --++ and --
Code at Day02\prog09_inc_dec_operator.php
GeshanManandhar.com 10
Logical OperatorLogical Operator
GeshanManandhar.com 11
String OperatorString Operator
► . used to concatenate strings. used to concatenate strings► .= used to append a string..= used to append a string.
<?php<?php$b = ”Hello” . "World!"; // now $b contains "$b = ”Hello” . "World!"; // now $b contains "Hello World!"Hello World!"
$a = "Hello ";$a = "Hello ";$a .= "World!"; // now $a contains "Hello W$a .= "World!"; // now $a contains "Hello World!"orld!"?> ?>
GeshanManandhar.com 12
If-else if- elseIf-else if- else
<?php<?php$a=10;$a=10;$b=17;$b=17;
if ($a > $b) {if ($a > $b) { echo "a is bigger than b";echo "a is bigger than b";} elseif ($a == $b) {} elseif ($a == $b) { echo "a is equal to b";echo "a is equal to b";} else {} else { echo "a is smaller than b";echo "a is smaller than b";}}?> ?>
GeshanManandhar.com 13
If-else if- else alternateIf-else if- else alternate
<?php<?php$a=10;$a=10;$b=17;$b=17;
if ($a > $b) :if ($a > $b) : echo "a is bigger than b";echo "a is bigger than b"; elseif ($a == $b) :elseif ($a == $b) : echo "a is equal to b";echo "a is equal to b";else :else : echo "a is smaller than b";echo "a is smaller than b";endif;endif;?> ?>
GeshanManandhar.com 14
Switch ExampleSwitch Example
<?php<?php$engDay =date ("l");$engDay =date ("l");switch ($engDay)switch ($engDay){{
case "Friday";case "Friday";print "Thank god its Friday";print "Thank god its Friday";
break;break;case "Saturday";case "Saturday";
print "Oh its week end Saturday";print "Oh its week end Saturday";break;break;default: default:
print "Its just another working day of the week.";print "Its just another working day of the week.";}}?>?>
GeshanManandhar.com 15
While LoopWhile Loop
<?php<?php$i = 1;$i = 1;while ($i <= 10) {while ($i <= 10) { echo $i++; echo $i++; }}
/* example 2 Alternative syntax*//* example 2 Alternative syntax*/$i = 1;$i = 1;while while ($i <= 10):($i <= 10): echo $i;echo $i; $i++;$i++;endwhile;endwhile;?> ?>
GeshanManandhar.com 16
Do While LoopDo While Loop
<?php<?php$i = 0;$i = 0;do {do { echo $i; echo $i;} while ($i > 0);} while ($i > 0);?> ?>
GeshanManandhar.com 17
For LoopFor Loop
<?php<?phpfor ($i = 1; $i <= 10; $i++) {for ($i = 1; $i <= 10; $i++) { echo $i;echo $i;}}
/*Alternate for Syntax *//*Alternate for Syntax */for($i = 0; $i <= 10; $i++):for($i = 0; $i <= 10; $i++):
echo "--".$i;echo "--".$i;endfor;endfor;?> ?>
GeshanManandhar.com 18
For Each loopFor Each loop
<?php<?php$arr = array("zero" , "one" , "two" , "three" , "four" );$arr = array("zero" , "one" , "two" , "three" , "four" );/*For each example 1 *//*For each example 1 */foreach ( $arr as $value ){foreach ( $arr as $value ){
print "<br>".$value;print "<br>".$value;}}print "<br>";print "<br>";/*For each example 2 *//*For each example 2 */foreach ( $arr as $key => $value) {foreach ( $arr as $key => $value) {
print "<br>At key ".$key." of the array, the value is print "<br>At key ".$key." of the array, the value is ".$value;".$value;
}}?> ?>
GeshanManandhar.com 19
PHP simple FunctionPHP simple Function
<?php<?phpfunction adder($var1, function adder($var1,
$var2){$var2){$sum = $var1+$var2;$sum = $var1+$var2;return $sum;return $sum;
}}?> ?> <html><html><head><head><title>Function with PHP <title>Function with PHP
an example</title>an example</title></head></head>
<body><body><?php<?php$a=10;$a=10;$b=15;$b=15;$added = adder($a, $b);$added = adder($a, $b);?>?>The sum of <?=$a?> and The sum of <?=$a?> and
<?=$b?> is <?<?=$b?> is <?=$added?>.=$added?>.
</body></body></html></html>
GeshanManandhar.com 20
Lets get rollingLets get rolling
►Write a program that performs all Write a program that performs all arithmetic operations with 3 variables.arithmetic operations with 3 variables.
► Get the month with date(“F”) function Get the month with date(“F”) function and if its December and day date(“d”) is and if its December and day date(“d”) is greater than 20 and less than 26, print greater than 20 and less than 26, print “Merry Christmas”. “Merry Christmas”.
►Print multiplication table of 5 with for Print multiplication table of 5 with for loop.loop.
►Use foreach loop for an array called Use foreach loop for an array called names and print the names in the array names and print the names in the array with its keys.with its keys.
GeshanManandhar.com 21
Questions are welcomeQuestions are welcome
► That’s why god gave us two eyes, two ears That’s why god gave us two eyes, two ears but only one mouth.but only one mouth.