Sunday 27 July 2014

if else statement in php

If and else statement:

                                                     Sometime we have some specific statement or instruction that we want to run when certain condition full fill. For this php provide the conditional statement like C++ .  If statement use for this purpose .

Syntax

If(condition)
Statement ;

If is a keyword that take a condition if the condition is true then the statement will be run otherwise the statement will not be run.

Example:

<?php
$x=100;
If($x==100)
echo ”congrats your condition is true”;
?>

output:

                     congrats your condition is true
You can use more than one statement that is effect by if condition for this we use {}. Every statement that will be written within {} will be run when if condition is true

Syntax

If(condition)
{
Statement 1;
Statement 2;
.
.
.
Statement n;
}

Example:

<?php
$x=100;
$z=10;
If($x==100 and $z==10)
{
echo ”the value of x is 100”."<br/>";
 echo ”congratulaion your output is diaplaying”."<br/>";
echo ”this example is written by programminglogic.net”;
}
?>

output:

                   the value of x is 100
                   congratulation your output is displaying 
                   the example is written by programminglogic.net       

If condition will be false than nothing will be happened no output will be display.  so if we want that when  conditon will false some instruction should be run then we use if else

Syntax of if ,else statement

If(condition)
Statement;
else
Statement;

Syntax for more than one statement

If(condition)
{
Statement 1;
Statement 2;
.
.
Statement n;
}
else
{
Statement 1;
Statement 2;
.
.
Statement n;
}

Example:

<?php
$x=100;
If($x<=100)
echo ”the value of x is 100”;
else
echo ”the value of x is less than or greater than 100”;
?>

output:

                   the value of x is 100

Flow chart of if ,else statement:

if else 

Nested if ,else:

                          We use if ,else  statement for conditional statement when we have multiple condition which is related to each other then we use nested if ,else . we can simply say that a nested if ,else statement is an if ,else statement with  another  if else statement inside it . we use it when we have more than one choice  . 

Syntax of nested if,else statement:

If(condition)
//Some code
elseif(condition)
//some code
Elseif(condition)
//some code
.
.
.
.
.else
//Some code
The  nested  if else  statement means that if the top condition is not true, then try checking other condition. If anyone condition is true , then ignore the rest of the available conditions. If no one condition is true then execute the else part of the statement .
flow chart:
nested if else



Php operators

Operators

                            Like c++ in php use different type of operators
  • Arithmetic operators in php
  • String operators in php
  • Assignment operators in php
  • Increment and decrements operators in php
  • Comparison operators in php
  • Logical operator in php

1-Airthmetic Operator:

                                                    Arithmetic operator use in php that is +(plus),-(subtract),*(multiply),/(divide),%(modulus)
Operator
Name
Example
+
Plus
$a+$c
-
Subtract
$a-$c
*
Multiply
$a*$c
/
Divide
$a/$c
%
Modulus
$a%$c

Here  $a%$c mean it will return reminder.

Program:

<?php
$a=5;
$c=2;
echo $a+$c;
echo “<br/>”;
echo $a-$c;
echo “<br/>”;
echo $a*$c;
echo “<br/>”;
echo $a/$c;
echo “<br/>”;
echo $a%$c;
?>

Output:

                      7
                      3
                      10
                      2.5
                      1

2-String operators:

Operator
Name
explanation
.
Concatenation
It concatenate two or more strings
.=
Concatenation assignment
It concatenate and assign the value of one string to other


Program:

<?php
$str="hello php";
$str2="programminglogic";
echo $str . $str2;
echo “<br/>”;
$str .=$str2;
echo $str;
?>

Output:

         hello php programminglogic
         hello php programminglogic

3-Assignment operator

                                          Assignment operator use in php that is "="
here i made a simple program in which i concatenate two string then its value will be assign to other new variable.

Program:

<?php
$str="hello php";
$str2="programminglogic";
$str3=$str . $str2;        //assign value
echo $str3;
?>

Output:

         hello php programminglogic

4-Increment and decrement operators:

                                                                   like c++ we use same increment and decrement operator in php 
Operator
Name
Explanation
$a++
Post increment
First display value then increment
$++a
Pre-increment
It first increment then display the output
$a--
Post-decrement
First display the output then decrement
$--a
Pre-decrement
It first decrement then display the output

Program:

<?php
$s=5;
echo ++$s."<br/>";//concatenate <br/>tag for next line 
$s=5;
echo $s++."<br/>";
$s=5;
echo $s--."<br/>";
$s=5;
echo --$s."<br/>";

?>

Output:

                     6
                     5
                     5
                     4

5-comparison operator:

                                              
Operator
Name
Explanation
==
Equal
Return true if two value will be equal
===
Identical
Return true if two value will be equal but same data type.
!=
Not equal
Return true if two value will be not equal
!==
Not identical
Return true if two value will be not equal but not same data type.
<> 
Not equal
It alternate of !=
> 
Greater than
Return true if first value will be greater than second
< 
Less than
Return true if first value will be less than second
>=
Greater than or equal
Return true if first value will be greater or equal to the second
<=
Less than or equal
Return true if first value will be less than or equal second

its example will be discuss in if else statement

6-logical operators:

Operator
Name
Explanation
and /&&
And
Return true if all value will true
or  / ||
Or
Return false if all value will be false
  !
Not
Return true if false


constant in php

Constant in php

                                        The constant in php is simple value whose value does not change in whole script of php. We use define() function in php for initialize the value of constant. We do not use $ before constant  like php variable. The constant name is start with letter or underscore. Its name with be global in whole php program. For if we making a website then server name and username and password will not be change in whole website then we declare it as constant there are many more example this is more common example.

Syntax:

             Define(constant name, value, true/false);
By default its value will be false it its value will be false then it mean it is case-sensitive if its value will be true then it is case-insensitive.

Example:

<?php
define(“LOVE”, ”programming is my love”);
echo LOVE;
echo “”<br/>
define(“LOVE”, ”programming is my love”,true);
echo love;
?>

Output:

           Programming is my love 
           Programming is my love
           


                                


lcfirst(), ucfirst() and ucwords() in php

lcfirst() Function:

                                    lcfirst() function that is use to convert the first character of string in lowercase letter. It is also very useful function in php many example of this function in php one of them comparison the value from data base.

Syntax:

          lcfirst(string);

Example:

<?php
$str=”HELLO PHP”;
echo lcfirst($str);
?>

Output:

         hELLO PHP

ucfirst() Function:

                                    ucfirst() function that is use to convert the first character of string in uppercase letter. It is also very useful function in php many example of this function in php one of them comparison the value from data base.

Syntax:

          ucfirst(string);

Example:

<?php
$str=”hello PHP”;
echo ucfirst($str);
?>

Output:

         Hello PHP

ucwords() Function:

                                    ucwords() function that is use to convert the first character of each word in string to uppercase letter. It is also very useful function in php many example of this function in php one of them comparison the value from data base.

Syntax:

          ucwords(string);

Example:

<?php
$str=”hello php”;
echo ucwords($str);
?>

Output:


         Hello Php


strtolower() and strtoupper() in php

strtolower() Function:

                                    strtolower() function that is use to convert the string in lowercase letters. It is also very useful function in php many example of this function in php one of them comparison the value from data base.

Syntax:

          strtolower(string);

Example:

<?php
$str=”HELLO PHP”;
echo strtolower($str);
?>

Output:

         hello php

strtoupper() Function:

                                    strtoupper() function that is use to convert the string in uppercase letters. It is also very useful function in php many example of this function in php one of them comparison the value from data base.

Syntax:

          strtoupper(string);

Example:

<?php
$str=”hello php”;
echo strtoupper($str);
?>

Output:


         HELLO PHP