Data Types Operator-php
Data Types Operator-php
Statement
PHP Data Types
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;
• 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.
• 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.
// create an object
$herbie = new Car();
• 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.";
• }?>