0% found this document useful (0 votes)
18 views

Data Types Operator-php

Data type in computer systems

Uploaded by

Vincy Ram15
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Data Types Operator-php

Data type in computer systems

Uploaded by

Vincy Ram15
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

PHP Data Types, Operators and

Statement
PHP Data Types

• Variables can store data of different types, and


different data types can do different things.
• PHP supports the following data types:
– String
– Integer
– Float (floating point numbers - also called double)
– Boolean
– Array
– Object
– NULL
– Resource
String
• A string is a sequence of characters, like "Hello world!".
• A string can be any text inside quotes. You can use
single or double quotes:
• <?php
$x = "Hello world!";
$y = 'Hello world!';

echo $x;
echo "<br>";
echo $y;
?>
Integer
• An integer data type is a whole number, both positive and negative.
• Rules for integers:
• An integer must have at least one digit.
• An integer must not have a decimal point
• An integer can be either positive or negative.

• Integers can be specified in: decimal (base 10), hexadecimal (base 16), octal (base
8), or binary (base 2) notation Ex.010 octal , 0x10 (Hexa decimal)
• Integer can be assigned to variable or they can be used in expression.
• Ex:
• $var1 =10;
• $var2 = $var1 +10;

• var_dump() function returns the data type and value:


• <?php
$x = 5985;
var_dump($x);
?>
Float
• A float (floating point number) is a number with a decimal
point or a number in exponential form.
• Precision 14 decimal digits.
• Scientific Notation
• Ex.
• $a = 3.14
• $b = 5.5 e3
• $c = 7e-10
• The PHP var_dump() function returns the data type and
value:
• <?php
$x = 10.365;
var_dump($x);
?>
Boolean
• A Boolean represents two possible states: TRUE or FALSE.
• $x = true;
$y = false;

• value is number
• false if exactly equal to 0 and true otherwise.

• If value is string
• False if the string is empty and it is true otherwise.
Array
• Array
• An array stores multiple values in one single variable.

• In the following example $cars is an array.

• The PHP var_dump() function returns the data type and value:

• <?php
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>
Object
• An object is a data type which stores data and information on
how to process that data.

• In PHP, an object must be explicitly declared.

• First we must declare a class of object.

• For this, we use the class keyword.

• A class is a structure that can contain properties and


methods:
Object
• <?php
class Car {
function Car() {
$this->model = "VW";
}
}

// create an object
$herbie = new Car();

// show object properties


echo $herbie->model;
?>
NULL
• NULL
• Null is a special data type which can have only one value:
NULL.
• A variable of data type NULL is a variable that has no value
assigned to it.
• <?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>
Resource
• The special resource type is not an actual data
type. It is the storing of a reference to
functions and resources external to PHP.
• A common example of using the resource data
type is a database call.
Variables
• Variables are nothing but identifies which point to a memory
location in which data is stored.
• Variables in PHP are quite different from compiled language
such as C and Java.
• When a PHP variable is being defined their data type is not
declared prior their use.
• A PHP variable takes its data type from the user defined value
that is being stored in it.
• A php variable can change its type , as often as needed.
• A PHP variable named must be named / declared starting with
the $ character followed by a letter.
• The rest of the variable name can consists of a mix of both
alphabets and numbers.
Variables
• The following are three examples of valid names for variables:
• $city
• $address2
• $age_30
• The _(underscore) character can also be used in variable
names.
• It is used as a replacement for space.
• Space is a character that cannot be used when naming a PHP
variable.
Variables
• The following characters are not allowed in a variable name
and cause errors if used.
• * (asterix)
• + (plus)
• # (hash)
• @ (at the rate)
• - (minus)
• & ( ampersand)
• £ (pound)

• There is no limit on the size of variables in PHP.


Variables
• Once a variable is named , it is empty until assigned a value.
To assign a value to variable;
• $city = ‘Mumbai’;
• Everything inside the single quotes will be assigned to the
variable named city.
• The named variable is on the left side of = (i.e. the assignment
operator) and the value to be held by the variable is on its
right.
• Php does not require variables to be declared before being
initialized. They can simply be populated with a value and put
into action whenever and wherever required.
• Similarly , numeric values can be assigned.
Variables
• $age = 24;
• Here, $age is assigned a value of 24. The only difference is
that the value 24 is passed without quotation marks.
• Variables declared without a dollar sign will not work. This is a
common mistake by new PHP programmers.
• Different types of values can be assigned to variables .
• PHP is case sensitive.
• Ex
• <?php
• $Name = “Ivan Bayross”;
• echo $Name;
• ?>
Variables
• In the above example a variable named $Name is declared.
• The value Ivan Bayross has been assigned but while accessing
the value held by the variable, the variable name is misspelled
(i.e. is in lowercase). This example when run displays the
following error on the VDU screen.
Numbers
• All the normal rules about number precedence apply.
• $x = 5;
• $y = 10;
• $z= 2+3*$x +5 * $y;
• echo $z;
• When a variable is referenced by its name, PHP knows that
it’s the value held in the variable that must be processed and
not the variable itself.
• This is automatically done before a new value is stored in the
variable on the left hand side of the assignment operator.
• $x =8;
• $y = 4;
• $x = (2 * $x +5) / $y;
Strings
• When assigning a string value to a variable it must be
enclosed in quotes.
• Either single quotes (‘) or double quotes (“) can be used.
• There is a vital difference between the two types of quotes.
• $firstName = ‘Ivan’;
• $wishing = “Hello everyone, my first name is $firstname .
<BR/>”;
• Echo $wishing;
• $lastName = ‘Bayross’;
• $greeting = ‘Hello everyone , my last name is $lastName”;
• echo $greetings;
Strings
• When double quotes are used, PHP performs variable
expansion, this means that PHP substitutes the value of
$firstName whenever a reference to $firstName is
encountered.
• The result is that the string stored in the variable $wishing is
Hi, my first name is Ivan.
• When a value is assigned to the variable $greeting and single
quotes are used then PHP does not perform any variable
expansion and hence $greeting ends up with Hello, my last
name is $lastName.
• This means that anything enclosed in single quotes is treated
as a string constant and is not changed in anyway by PHP
when processed.
Strings
• Double quotes are also used for expanding other special
characters, such as the newline character (\n).
• <?php
• echo “Ivan\n Bayross”;
• ?>
• Browsers usually do not render the newline character while
displaying the output.
• The newline character passed to the echo or print() function
is sent to the browser as string information and not
formatting information.
• Hence <BR> tag should be used instead to create a line break
in HTML.
Strings
• The (\) backslash is used to indicate that the next character is
special.
• $name = “Ivan Bayross is a \“ Professor \” residing in
Mumbai”;
• echo $name;
• This code produces the following output:
• Ivan Bayross is a “Professor” residing in Mumbai.
• The same applies if single quotes were used throughout the
string.
Strings
• Or
• Do the inverse, enclose the string in double quotes and use as
many single quotes in the string as required.
• echo ‘<A href = mailto:[email protected]>
• <img src = “image.gif” width =“16” Height =“16” ALT = “me”
• BORDER =“0”> </A>’;
• If the string following echo is enclosed within double quotes,
all the other double quote will require escape characters
before them.
• On the other hand, in the above string PHP would not do any
variable substitution i.e. PHP is being forced to deal with the
string as though it was a string literal.
Strings
• Consider the following examples
• $fullname = ‘Ivan Bayross is a ‘;
• $profession =“Programmer”;
• $var1 =“$fullname $profession”;
• $var2 =‘Ivan Bayross is a Programmer’;
PHP OPERATORS
• Operators are used to perform operations on variables and
values.
• PHP divides the operators in the following groups:

• Arithmetic operators
• Assignment operators
• Comparison operators
• Increment/Decrement operators
• Logical operators
• String operators
• Ternary operator ( Conditional assignment operators)
Arithmetic Operators
• The PHP arithmetic operators are used with numeric values to
perform common arithmetical operations, such as addition,
subtraction, multiplication etc.
Arithmetic operation
• <html>
• <head>
• <title>Arithmetical Operators</title>
• </head>
• <body>
• <?php
• $a = 42;
• $b = 20;
• $c = $a + $b;
• echo "Addition Operation Result: $c <br/>";

• $c = $a - $b;
• echo "Subtraction Operation Result: $c <br/>";
• $c = $a * $b;
• echo "Multiplication Operation Result: $c <br/>";

• $c = $a / $b;
• echo "Division Operation Result: $c <br/>";
• $c = $a % $b;
• echo "Modulus Operation Result: $c <br/>";

• $c = $a++;
• echo "Increment Operation Result: $c <br/>";
• $c = $a--;
• echo "Decrement Operation Result: $c <br/>";
• ?>
• </body>
• </html>
Assignment Operator
• The PHP assignment operators are used with numeric values to write a
value to a variable.
• The basic assignment operator in PHP is "=". It means that the left
operand gets set to the value of the assignment expression on the right.
• <?php
• $a = 30;
• $b = 20;
• $c = $a + $b;
• echo "Addtion Operation Result: $c <br/>";
• $c += $a;
• echo "Add AND Assigment Operation Result: $c <br/>";
• $c -= $a;
• echo "Subtract AND Assignment Operation Result: $c <br/>";
• $c *= $a;
• echo "Multiply AND Assignment Operation Result: $c <br/>";
• $c /= $a;
• echo "Division AND Assignment Operation Result: $c <br/>";
• $c %= $a;
• echo "Modulus AND Assignment Operation Result: $c <br/>";
• ?>
Comparison Operator
• The PHP comparison operators are used to compare two values (number or
string)
• <?php
• $a = 42;
• $b = 20;
• if( $a == $b ) {
• echo "TEST1 : a is equal to b<br/>";
• }else {
• echo "TEST1 : a is not equal to b<br/>";
• }
• if( $a > $b ) {
• echo "TEST2 : a is greater than b<br/>";
• }else {
• echo "TEST2 : a is not greater than b<br/>";
• }
• if( $a != $b ) {
• echo "TEST4 : a is not equal to b<br/>";
• }else {
• echo "TEST4 : a is equal to b<br/>";
• }
• ?>
Increment Decrement Operator
• The PHP increment operators are used to increment /decrement a variable's
value.
• $a = 5;

• $c = $a++; OUTPUT a= 6 c= 5
• $c = ++$a OUTPUT a=6 c = 6
• <?php
• echo "<h3>Postincrement</h3>";
• $a = 5;

• $c = $a++;
• echo "The value of c is " . $c . "<br />\n";
• echo "The value of a is " . $a . "<br />\n";

• echo "<h3>Preincrement</h3>";
• $a = 5;
• $d = ++$a;
• echo "The value of d is " . $d . "<br />\n";
• echo "The value of a is " . $a . "<br />\n";

• echo "<h3>Postdecrement</h3>";
• $a = 5;
• $e = $a--;
• echo "The value of e is " . $e . "<br />\n";
• echo "The value of a is " . $a . "<br />\n";

• echo "<h3>Predecrement</h3>";
• $a = 5;

• $g = --$a;
• echo "The value of g is " . $g . "<br />\n";
• echo "The value of a is " . $a . "<br />\n";
• ?>
Logical Operator
• The PHP logical operators are used to combine conditional statements.
• <?php
• $a = 42;
• $b = 12;

• if( $a && $b ) {
• echo "TEST1 : Both a and b are true<br/>";
• }else{
• echo "TEST1 : Either a or b is false<br/>";
• }

• if( $a and $b ) {
• echo "TEST2 : Both a and b are true<br/>";
• }else{
• echo "TEST2 : Either a or b is false<br/>";
• }

• if( $a || $b ) {
• echo "TEST3 : Either a or b is true<br/>";
• }else{
• echo "TEST3 : Both a and b are false<br/>";
• }
• if( !$a ) {
• echo "TEST4 : a is true <br/>";
• }else {
• echo "TEST4 : a is false<br/>";
• }
• ?>
String Operator
• Concatenation operator
• PHP has two operators that are specially designed for strings.
• <?php
• $a ="Hello";
• $b = $a." world";
• echo "$b <br/> ";
• $a ="Hello";
• $a .=" world";
• echo "$a";
• ?>
Ternary Operator
• The ternary operator is a conditional operator that decreases the
length of code while performing comparisons and conditionals
• (Condition) ? (Statement1) : (Statement2);.
• Condition: It is the expression to be evaluated which returns a
boolean value.
• Statement 1: it is the statement to be executed if the condition
results in a true state.
• Statement 2: It is the statement to be executed if the condition
results in a false state.

• <?php
• $marks=40;
• print ($marks>=40) ? "pass" : "Fail";
• ?>
Conditional statement
• Types of if and switch
• If
• If….else
• If … elseif ..else
• Switch
If Statement
• If(condition)
• {
• Statements
• }
$a=10;
$b=20;
if($a>$b)
{
echo “a is big”;
}
If.. Else Statement
• if(condition) $a=10;
• { $b=20;
• True Statements if($a>$b)
• } {
• else echo “a is big”;
}
• {
else
• False statements {
• } echo”b is big”;
}
Biggest of two numbers
• <?php
• $a=10;
• $b=20;
• if($a>$b)
• {
• print $a;
• echo " a is bigger";
• }
• else
• {
• print $b;
• echo " B is bigger";
• }
• ?>
If.. Elseif.. Else Statement
• if(condition)
• { Statement1
• }
• Else if (condition2)
• { Statement2}
• else
• {
• False statements
• }
Display grades based on average marks

• <?php
• $marks = array(25, 65, 46, 98, 78, 65);
• $max_marks = sizeof($marks) * 100;
• $total = 0;
• $grade = 'F';

• // Traverse though the marks array to find the sum.
• for ($i = 0; $i < sizeof($marks); $i++)
• {
• $total += $marks[$i];
• }
• // typecast the calculation to double.
• $percentage = (($total) / $max_marks) * 100;
• // Nested if else
• if ($percentage >= 90)
• {
• $grade = 'A';
• }
Display grades based on average marks
• else
• {
• if ($percentage >= 80 && $percentage <= 89)
• {
• $grade = 'B';
• }
• else
• {
• if ($percentage >= 60 && $percentage <= 79)
• {
• $grade = 'C';
• }
• else
• {
• if ($percentage >= 33 && $percentage <= 59)
• {
• $grade = 'D';
• }
• else
• {
• $grade = 'F';
• } } } }
• echo $grade . "\n";
• ?>
Switch statement
• switch($var)
• {
• case value1:
• statement1; break;
• case value2:
• statement2; break;
• ..
• …
• default:
• default statement;
• }
Switch case
• <?php
• $today = 'Tue';
• switch ($today)
• {
• case "Mon":
• echo "Today is Monday.";
• break;
• case "Tue":
• echo "Today is Tuesday.";
• break;
• case "Wed":
• echo "Today is ash Wednesday.";
• break;
• case "Thu":
• echo "Today is Thursday.";
• break;
• case "Fri":
• echo "Today is Friday.";
• break;
• case "Sat":
• echo "Today is Saturday.";
• break;
• case "Sun":
• echo "Today is Sunday.";
• break;
• default: echo "Invalid day.";
• }?>

You might also like