Arithmetic Operations, PHP Control Structures, Logical Expressions,php tutorial: 



Arithmetic Operations example code :


<?php
$a=30;
$b=15;
$a =$a+$b; //assigning variable $a to new value 45
echo $a.'this a is after assigning new variable a'.'<br>';
$total1=$a-$b;   //assigning variable $total1 to new value $a-$b and rest of them are same
$total3=$a*$b;
$total4=$a/$b;
$total5=$a%$b;
echo $total1.'<br>'; //showing the results
echo $total3.'<br>';
echo $total4.'<br>';
echo $total5.'<br>';
?> 

PHP Control Structures example code:


<?php 

$foo=10;


if($foo == 0) {
echo "The  foo is equal to 0";
}
else if (($foo > 0) AND ($foo <= 5)) {
echo " The variables foo is between 1 and 5" ;
}
else {
echo "The variable foo is equal to ".$foo;
}

?>

Logical Expressions example code:



<html>
<head>
<title>Logical Expressions</title>
</head>
<body>A
<?php
$a = 5;
$b = 5;
if ($a > $b) {
echo "a is larger than b";
} elseif ($a == $b) { // if 1st expression is false, elseif performs a 2nd test
echo "a equals b";
} else { // if all expressions are false, else gives an alternative statement and run it
echo "a is smaller than b";
}
?>
<br />
<?php
$c = 1;
$d = 20;
if (($a > $b) AND ($c > $d)) {// && is a logical AND ,  || is a logical OR
echo "a is larger than b AND ";
echo "c is larger than d";
}
if (($a > $b) OR ($c > $d)) {
echo "a is larger than b OR ";
echo "c is larger than d";
} else {
echo "neither a is larger than b or c is larger than d";
}
?>
<br />
<?php
// ! is a logical NOT and it negates an expression
unset($a);
if (!isset($a)) { // read this as "if not isset $a" 
$a = 100;
}
echo $a;
?>
<br />
<?php
// Use a logical test to determine if a type should be set
if (is_int($a)) {
settype($a, "string");
}
echo gettype($a);
?>
</body>
</html>

0 comments:

Post a Comment

Subscribe to RSS Feed Follow me on Twitter!