Sunday 20 September 2015


Conditional Statement in php

Conditional statement is used to perform different actions for different decisions in a specific program. Conditional statements are the set of commands used to perform different actions based on different conditions.
Here we will look at two structures: if...else and switch statements. Both perform in the same way the same task.
There are two way to use if else

1-if....else statement
Use this statement if you want to execute a set of code when a condition is true and another if the condition is false.
2-else if statement
Is used with the if...else statement to execute a set of code if one of several condition are true
Syntax:1
if(condition)
{
body of if
}
else
{
body of else
}
Rules:If condition is true then if body executed otherwise else body is excuted.
Example:1
<?php
$age=17;
if($age<18)
{
echo "you are under age";
}
else
{
echo "Good Luck";
}
?>
Output :you are under age

Syntax:2
if(condition)
{
if block
}
else if(condition)
{
second if block
}
else
{
another block
}
Example:2
<?php
$age=18;
if($age<18)
{
echo "you are under age";
}
else if($age<30)
{
echo "you are medium age";
}
else
{
echo "you are in proper age";
}
?>
Output :you are medium age

Switch Statement

If you want to select one of many blocks of code to be executed, use the Switch statement. The switch statement is used to avoid long blocks of if..elseif..else code.
Syntax:
switch (expression)
{
case label1:
//code to be executed if expression = label1;
break;
case label2:
//code to be executed if expression = label2;
break;
default:
//code to be executed if expression is different it mins no match found for any label

}
Rules to Excute:
  • A single expression (most often a variable) is evaluated once
  • The value of the expression is compared with the values for each case in the structure
  • If there is a match, the code associated with that case is executed
  • After a code is executed, break is used to stop the code from running into the next case
  • The default statement is used if none of the cases are true

  • Example:
    <html>
    <body>
    <?php
    $x=2;
    switch ($x)
    {
    case 1:
      echo "you are choosing Number 1";
      break;
    case 2:
      echo "you are choosing Number 2";
      break;
    case 3:
      echo "you are choosing Number 3";
      break;
    default:
      echo "No number between 1 and 3";
    }
    ?>
    </body>
    </html>

    Output:you are choosing Number 2

    Sunday 7 June 2015

    PHP Mysql Connectivity

    PHP Mysql Connectivity


    Database connectivity with php code is very simple.
    For this you have to write the following code.
    Syntax-:

    // creating connection
    $con=mysql_connect(“localhost”,”root”,” ”) or die(“connection error”);
    // selecting database
    mysql_select_db(“database name”,$con) or die(“database error”);
    In the above code in the first line mysql_connect() is a query which is used to create connection and accept three parameter .
    1. Host Name-(localhost)
    2. User Name-(root)
    3. Password-(which is blank on local server)

    Note-The above code is for local server , if you use the actual server you have to use your database username and password. 

    And in the second line mysql_select_db() is a query which is used to select the database and accept two parameter first is database name (required) and second is connection variable(optional).
    Example-:
    Here my database name is my_db and table name is my_tbl using this creating a program to insert data in the table my_tbl;
    And suppose I have two field in the table name and age respectively.
    <?php
    $name="Satyam";
    $age=12;
     
    $con=mysql_connect(“localhost”,”root”,” ”) or die(“connection error”);
    mysql_select_db(my_db,$con);
    
    $q=mysql_query(“insert into my_tbl values(‘$name’,’$age’)”);
    
    if($q)
    {
    echo “Record inserted successfully”;
    }
    else
    {
    echo “Error”.mysql_error();
    }
    
    ?>
    

    Monday 4 November 2013

    Advance Php Training in Delhi

    Object Oriented Programming(OOPS) New Feature of PHP



    Introduction

    Starting with PHP 5, the object model was rewritten to allow for better performance and more features. This was a major change from PHP 4. PHP 5 has a full object model.
    Among the features in PHP 5 are the inclusions of visibilityabstract and final classes and methods, additional magic methodsinterfacescloning andtypehinting.
    PHP treats objects in the same way as references or handles, meaning that each variable contains an object reference rather than a copy of the entire object. 

    Sunday 26 May 2013