More complex test expressions.
OR Operator
true || false true or false
This would always return TRUE. It means that any one operator is true then it returns TRUE.
AND Operator
true && false true and false
This would return false. If any any one operator is false then the returned value is FALSE.
Another example of operators:
( $x > 2 ) && ( $x < 15 )
Please note that $x is a variable here and we are testing if this variable is between 2 and 15 or not.
xor Operator
true xor true result is FALSE
! Not Operator
!true returns FALSE
--------------------------------------------------------------------------------------------------------------------------
Operators
Post Increment Operators
x = x + 1; is same as x +=1; and is also same as x++;
x++; is a post increment operator while ++x; is a pre-increment operator.
--------------------------------------------------------------------------------------------------------------------------
Order of Precedence for Selected Operators
top most is having highest precendence and lower most has lowest.
++ − − (cast)
/ *%
+−
< <= => >
== === !=
&&
||
66
= += − = /= *=%= .=
and
xor
or
--------------------------------------------------------------------------------------------------------------------------
Contants
define ("contant_name", 42);
This means that where ever we use constant_name it will automatically have the value 42
<html>
<head>
<title></title>
</head>
<body>
<?php
define ("SOME_CONSTANT",45);
print "This is a constant value ".SOME_CONSTANT;
?>
</body>
</html>
Some system defined constants
_FILE_; This returns the file name currently being read by the interpreter.
_LINE_; This returns the line number of the file being currently read by the interpreter.
PHP_VERSION; provides the PHP Version being used. This can be used to make the script run on a particular version only.
--------------------------------------------------------------------------------------------------------------------------
Controls and Loops
if Statement
if (expression)
{
some logic
}
if else Statement
if (expression)
{
some logic
}
else
{
some other logic
}
if else Statement
if (expression)
{
some logic
}
elseif
{
some other logic
}
else
{
some more other logic
}
Example:
<html>
<head>
<title></title>
</head>
<body>
<?php
$var1 = 10;
if ($var1 == 40)
{
print "This is a var1 value ".$var1;
}
elseif ($var1==10)
{
print "this is the value again ".$var1;
}
else
{
print "this is not the value";
}
?>
</body>
</html>
--------------------------------------------------------------------------------------------------------------------------
Switch Statement
switch (expression)
{
case result1:
//execute these statements if case1 is true
break;
case2 result2:
//execute these statements if case2 is true
break;
default:
//these statements are executed if none of the above conditions are true.
}
Caution Don't forget to include a break statement at the end of any code that will be executed as part of a case statement. Without break, the program flow will continue to the next case statement and ultimately to the default statement. In most cases, this will not be the behavior that you will be expecting.
Using the ? Operator
( expression )?returned_if_expression_is_true:returned_if_expression_is_false;
Example:
<html>
<head>
<title></title>
</head>
<body>
<?php
$var1 = 10;
print (($var1==10)?10:20);
?>
</body>
</html>
Result is 10
------------------------------------------------------------------------------------------------------------------------
Loops:
-----while loop-----------------------------------------------
while (expression)
{
//some statement
}
Example:
<html>
<head>
<title></title>
</head>
<body>
<?php
$var1 = 10;
while ($var1 < 20)
{
print ($var1)."<br>";
$var1++;
}
?>
</body>
</html>
----------do...while loop------------------------------------------
do {
// code to be executed
}
while (expression);
----------for statement------------------------------------------
for (variable assignment;variable expression;variable manipulation)
{
//code to be executed
}
Example:
<html>
<head>
<title></title>
</head>
<body>
<?php
$var1 = 10;
for ($var1 = 1; $var1<200;$var1++)
{
print ($var1)."<br>";
}
?>
</body>
</html>
Note: You can use break; statement to break out of loop at anytime.
--------------------------------------------------------------------------------------------------------------------------
Continue Statement:
To skip any computation you can use continue statement.
Example:
<html>
<head>
<title></title>
</head>
<body>
<?php
$var1 = 10;
for ($var1 = 1; $var1<200;$var1++)
{
if ($var1 < 10)
{
continue;
}
print ($var1)."<br>";
}
?>
</body>
</html>
ALL THE ABOVE MENTIONED LOOPS CAN BE USED IN NESTED FORMAT. SINCE AT THE VERY BEGINNING OF THE TUTORIAL I ASSUMED THAT THE READEER IS HAVING ACQUIANTANCE WITH ATLEAST ONE PROGRAMMING STATEMENT SO I AM NOT GOING TO EXPLAIN NESTED LOOPS HERE.
--------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------
No comments:
Post a Comment