Day 2 PHP

Geshan Manandhar Developer, Young Innovations Pvt. Limited 1

Variables in PHP
► Boolean:

It can be either TRUE or FALSE. ► Integer: The size of an integer is platformdependent, although a maximum value of about two billion is the usual value. ► Float: The size of a float is platformdependent, although a maximum of ~1.8e308 with a precision of roughly 14 decimal digits is a common value


Variable in PHP
► String:

A string is series of characters. In PHP, a character is the same as a byte, that is, there are exactly 256 different characters possible. ► Array: An array in PHP is actually an ordered map. A map is a type that maps values to keys.
 $names[1] = “Ram”;  $names[2] = “Shyam”; 3

Variables in PHP
► Objects:

mainly have attributes and functions.
class test { function do_test() { echo "Testing class function."; } } $an_object = new test; $an_object->do_test();

?> 4

Naming variables in PHP
► Variables

in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive. ► A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.
 $a_5=10; //valid  $#b = 20; //invalid
► Predefined

Variables ($_SERVER, $_POST)


You can define a constant by using the define() -function. Once a constant is defined, it can never be changed or undefined. <?php define("PI", 3.1415); //defining a constant called PI

$r=5; //r for radius $area = PI*$r*$r; //area calculation of circle print "Areas of circle with radius $r is ".$area."."; //user number_format() yourself. ?> 6

Arithmetic Operators

See Program for more clarification: Day02\prog06_Operators.php 7

Comparison Operators

See code of Day02\prog08_comparision_operators.php


Increment Decrement Operators
► ++

and --

Code at Day02\prog09_inc_dec_operator.php 9

Logical Operator


String Operator

used to concatenate strings ► .= used to append a string. <?php $b = ”Hello” . "World!"; // now $b contains "Hello  World!" $a = "Hello "; $a .= "World!";  // now $a contains "Hello World!" ?>


If-else if- else
<?php $a=10; $b=17; if ($a > $b) { echo "a is bigger than b"; } elseif ($a == $b) { echo "a is equal to b"; } else { echo "a is smaller than b"; } ?> 12

If-else if- else alternate
<?php $a=10; $b=17; if ($a > $b) : echo "a is bigger than b"; elseif ($a == $b) : echo "a is equal to b"; else : echo "a is smaller than b"; endif; ?> 13

Switch Example
<?php $engDay =date ("l"); switch ($engDay) { case "Friday"; print "Thank god its Friday"; break; case "Saturday"; print "Oh its week end Saturday"; break; default: print "Its just another working day of the week."; } ?>


While Loop
<?php $i = 1; while ($i <= 10) { echo $i++; } /* example 2 Alternative syntax*/ $i = 1; while ($i <= 10): echo $i; $i++; endwhile; ?> 15

Do While Loop
<?php $i = 0; do {     echo $i; } while ($i > 0); ?>


For Loop
<?php for ($i = 1; $i <= 10; $i++) { echo $i; } /*Alternate for Syntax */ for($i = 0; $i <= 10; $i++): echo "--".$i; endfor; ?> 17

For Each loop
<?php $arr = array("zero" , "one" , "two" , "three" , "four" ); /*For each example 1 */ foreach ( $arr as $value ){ print "<br>".$value; } print "<br>"; /*For each example 2 */ foreach ( $arr as $key => $value) { print "<br>At key ".$key." of the array, the value is ". $value; } ?> 18

PHP simple Function
<?php function adder($var1, $var2){ $sum = $var1+$var2; return $sum; } ?> <html> <head> <title>Function with PHP an example</title> </head>
<body> <?php $a=10; $b=15; $added = adder($a, $b); ?> The sum of <?=$a?> and <?=$b?> is <? =$added?>. </body> </html>

Lets get rolling
► Write

a program that performs all arithmetic operations with 3 variables. ► Get the month with date(“F”) function and if its December and day date(“d”) is greater than 20 and less than 26, print “Merry Christmas”. ► Print multiplication table of 5 with for loop. ► Use foreach loop for an array called names and print the names in the array with its keys.


Questions are welcome
► That’s

why god gave us two eyes, two ears but only one mouth.


Sign up to vote on this title
UsefulNot useful