0% found this document useful (0 votes)
142 views21 pages

Geshan Manandhar Developer, Young Innovations Pvt. Limited

This document discusses various PHP variables and data types including booleans, integers, floats, strings, arrays, and objects. It also covers PHP operators, control structures like if/else statements, loops (while, do-while, for, foreach), and functions. The document provides code examples for each concept and encourages the reader to write programs applying arithmetic operations, date functions, loops, and arrays. It concludes by inviting questions from the reader.

Uploaded by

Geshan Manandhar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
142 views21 pages

Geshan Manandhar Developer, Young Innovations Pvt. Limited

This document discusses various PHP variables and data types including booleans, integers, floats, strings, arrays, and objects. It also covers PHP operators, control structures like if/else statements, loops (while, do-while, for, foreach), and functions. The document provides code examples for each concept and encourages the reader to write programs applying arithmetic operations, date functions, loops, and arrays. It concludes by inviting questions from the reader.

Uploaded by

Geshan Manandhar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

Day 2

PHP

http://www.php.net

Geshan Manandhar
Developer,
Young Innovations Pvt. Limited
www.geshanmanandhar.com
GeshanManandhar.com 1
Variables in PHP
► Boolean: It can be either TRUE or FALSE.
► Integer: The size of an integer is platform-
dependent, although a maximum value of
about two billion is the usual value.
► Float: The size of a float is platform-
dependent, although a maximum of
~1.8e308 with a precision of roughly 14
decimal digits is a common value
GeshanManandhar.com 2
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”;

GeshanManandhar.com 3
Variables in PHP
► Objects:mainly have attributes and
functions.
<?php
class test
{
function do_test()
{
echo "Testing class function.";
}
}

$an_object = new test;


$an_object->do_test();
?>
GeshanManandhar.com 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)
GeshanManandhar.com 5
Constant
► 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.
?>
GeshanManandhar.com 6
Arithmetic Operators

See Program for more clarification: Day02\prog06_Operators.php

GeshanManandhar.com 7
Comparison Operators

See code of Day02\prog08_comparision_operators.php

GeshanManandhar.com 8
Increment Decrement Operators
► ++ and --

Code at Day02\prog09_inc_dec_operator.php

GeshanManandhar.com 9
Logical Operator

GeshanManandhar.com 10
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!"
?>

GeshanManandhar.com 11
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";
}
?>
GeshanManandhar.com 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;
?>
GeshanManandhar.com 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.";
}
?>

GeshanManandhar.com 14
While Loop
<?php
$i = 1;
while ($i <= 10) {
echo $i++;
}

/* example 2 Alternative syntax*/


$i = 1;
while ($i <= 10):
echo $i;
$i++;
endwhile;
?>
GeshanManandhar.com 15
Do While Loop
<?php
$i = 0;
do {
    echo $i;
} while ($i > 0);
?>

GeshanManandhar.com 16
For Loop
<?php
for ($i = 1; $i <= 10; $i++) {
echo $i;
}

/*Alternate for Syntax */


for($i = 0; $i <= 10; $i++):
echo "--".$i;
endfor;
?>
GeshanManandhar.com 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;
}
?>

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

GeshanManandhar.com 19
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. GeshanManandhar.com 20
Questions are welcome
► That’s why god gave us two eyes, two ears but
only one mouth.

GeshanManandhar.com 21

You might also like