26 May 2012

Objected Oriented Programming in PHP

                        I hope you all know about what is object oriented programming and what is procedure oriented programming. if you know those i can skip that section that what is the difference between object oriented and procedure oriented programming.

I hope the following simple example help you to understand more about object oriented programming in php
lets go through that......

I'm going to give you a student registration and display the registered users in front page and we can update the details

Pages that the project contains...

1. index.php
2. Registration.php
3.Edit.php

and there is a folder named class which contains the file including our class and a configuration file such as

1. registration_class.php
2. config.php



i hope you all get the structure of our folder

      STUDENT
            |
            |--------> index.php
            |
            |--------> Registration.php
            |
            |--------> Edit.php
            |
            |-------->class
                             |
                             |----------> registration_class.php
                             |
                             |----------> config.php


Then we can go through each page copy and paste all the pages below and set the file as shown above and run the project ok?



index.php

<?php
    require_once(realpath(dirname(__FILE__)."/class/registration_class.php"));
    $obj_reg    =    new registration();
    $table        =    "tbl_registration";
    $res        =   $obj_reg->selectall($table);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Index</title>
<style type="text/css" media="screen">
    .head{
        width:auto;
        height:25px;
        font-family: Georgia, "Times New Roman", Times, serif;
        font-size: 18px;
        font-weight:300px;
        background-color:#9CF;
        color:#30F;
    }
</style>
</head>

<body>
<div id="Menu" style="text-align:right;margin-bottom:20px; margin-top:20px" >
<a href="Registration.php" style="text-decoration:none; font-size:24px;">Resistration</a>
</div>
<table width="100%" border="0">
  <tr>
    <th class="head" scope="col">Sl</th>
    <th class="head" scope="col">Name</th>
    <th class="head" scope="col">House</th>
    <th class="head" scope="col">Street</th>
    <th class="head" scope="col">City</th>
    <th class="head" scope="col">District</th>
    <th class="head" scope="col">State</th>
    <th class="head" scope="col">Phone</th>
  </tr>

  <?php
  $i=1;
  while($row=mysql_fetch_array($res)){?>
 
  <tr>
    <td height="27" align="center"><?php echo $i ?></td>
    <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <a href="Edit.php?bintId=<?php echo $row['pk_bint_user_id']; ?>" style="text-decoration:none;color:#000">
            <?php echo $row['vchr_name']; ?>
        </a>
    </td>
    <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $row['vchr_house']; ?></td>
    <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $row['vchr_street']; ?></td>
    <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $row['vchr_city']; ?></td>
    <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $row['vchr_district']; ?></td>
    <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $row['vchr_state']; ?></td>
    <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $row['vchr_phone']; ?></td>
  </tr>
 
  <?php
  $i++;

  } ?>
  <tr>
      <td height="33"></td>
    <td></td>
    <td colspan="4" style="text-decoration:blink;color:#F00;font-size:24px;font-weight:bold" align="center"><?php if(mysql_num_rows($res)==0){ echo "No Records Found!"; } ?></td>
    <td></td>
    <td></td>
  </tr>

</table>

</body>
</html>


Registration.php

 <?php
require_once(realpath(dirname(__FILE__)."/class/registration_class.php"));
$obj_reg    =    new registration(); // object creation for registration
if(isset($_POST['submit'])){
    $name       =    mysql_real_escape_string($_POST['txtname']);
    $house      =    mysql_real_escape_string($_POST['txthousename']);
    $street     =    mysql_real_escape_string($_POST['txtstreet']);
    $city       =    mysql_real_escape_string($_POST['txtcity']);
    $dist       =    mysql_real_escape_string($_POST['txtdistrict']);
    $state      =    mysql_real_escape_string($_POST['txtstate']);
    $phone      =    mysql_real_escape_string($_POST['txtphone']);
   
    $details = array(
                        "vchr_name"     =>  $name,
                        "vchr_house"     =>  $house,
                        "vchr_street"     =>  $street,
                        "vchr_city"     =>  $city,
                        "vchr_district" =>  $dist,
                        "vchr_state"     =>  $state,
                        "vchr_phone"    =>  $phone);
                       
        $table = "tbl_registration";
        $register=$obj_reg->insert($details, $table);
                if($register){
                    echo "<script> alert('Registered Sucessfuly');</script>";
                    header( 'Location:index.php' ) ;
                }else{
                    echo "<script> alert('Registration Failed');</script>";
                }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Registration</title>
</head>

<body>
<body>
<div id="Menu" style="text-align:right;margin-bottom:20px; margin-top:20px" >
    <a href="index.php" style="text-decoration:none; font-size:24px;">Home</a>
</div>
<table width="100%" border="0">
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td align="center" style="color:#36F"><h2><strong>Registration</strong></h2></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><form id="form1" name="form1" method="post" action="">
    <table width="100%" border="0" style="font-size:20px">
  <tr>
    <td width="22%">&nbsp;</td>
    <td width="28%">&nbsp;</td>
    <td width="5%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
    <td width="12%">&nbsp;</td>
  </tr>
  <tr>
    <td height="47">&nbsp;</td>
    <td>Name</td>
    <td>:</td>
    <td><input type="text" name="txtname" id="txtname" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="66">&nbsp;</td>
    <td>House Name</td>
    <td>:</td>
    <td><input type="text" name="txthousename" id="txthousename" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="52">&nbsp;</td>
    <td>Street</td>
    <td>:</td>
    <td><input type="text" name="txtstreet" id="txtstreet" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="55">&nbsp;</td>
    <td>City</td>
    <td>:</td>
    <td><input type="text" name="txtcity" id="txtcity" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="54">&nbsp;</td>
    <td>District</td>
    <td>:</td>
    <td><input type="text" name="txtdistrict" id="txtdistrict" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="50">&nbsp;</td>
    <td>State</td>
    <td>:</td>
    <td><input type="text" name="txtstate" id="txtstate" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="51">&nbsp;</td>
    <td>Phone</td>
    <td>:</td>
    <td><input type="text" name="txtphone" id="txtphone" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="54">&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><input type="submit" name="submit" id="submit" value="Submit" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>

    </form></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>

</body>
</html>


Edit.php

 <?php
    require_once(realpath(dirname(__FILE__)."/class/registration_class.php"));
    $obj_reg    =    new registration(); // object creation for registration
    $bintId        =    $_GET['bintId'];
    $table         =     "tbl_registration";
    $where        =    "pk_bint_user_id = $bintId";
    $res        =    $obj_reg->selectrow($table,$where);
  
    if(isset($_POST['Update'])){
        $name       =    mysql_real_escape_string($_POST['txtname']);
        $house      =    mysql_real_escape_string($_POST['txthousename']);
        $street     =    mysql_real_escape_string($_POST['txtstreet']);
        $city       =    mysql_real_escape_string($_POST['txtcity']);
        $dist       =    mysql_real_escape_string($_POST['txtdistrict']);
        $state      =    mysql_real_escape_string($_POST['txtstate']);
        $phone      =    mysql_real_escape_string($_POST['txtphone']);
      
        $details = array(
                            "vchr_name"     =>  $name,
                            "vchr_house"     =>  $house,
                            "vchr_street"     =>  $street,
                            "vchr_city"     =>  $city,
                            "vchr_district" =>  $dist,
                            "vchr_state"     =>  $state,
                            "vchr_phone"    =>  $phone);
                          
            $update=$obj_reg->update($details, $table ,$where);
                    if($update){
                        echo "<script> alert('Registered Sucessfuly');</script>";
                        header( 'Location:index.php' ) ;
                    }else{
                        echo "<script> alert('Updation Failed');</script>";
                    }
    }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit</title>
</head>

<body>
<body>
<div id="Menu" style="text-align:right;margin-bottom:20px; margin-top:20px" >
    <a href="index.php" style="text-decoration:none; font-size:24px;">Home</a>
</div>
<table width="100%" border="0">
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td align="center" style="color:#36F"><h2><strong>Edit Details</strong></h2></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><form id="form1" name="form1" method="post" action="">
    <table width="100%" border="0" style="font-size:20px">
  <tr>
    <td width="22%">&nbsp;</td>
    <td width="28%">&nbsp;</td>
    <td width="5%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
    <td width="12%">&nbsp;</td>
  </tr>
  <tr>
    <td height="47">&nbsp;</td>
    <td>Name</td>
    <td>:</td>
    <td><input type="text" name="txtname" id="txtname" value="<?php echo $res['vchr_name'];  ?>" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="66">&nbsp;</td>
    <td>House Name</td>
    <td>:</td>
    <td><input type="text" name="txthousename" id="txthousename" value="<?php echo $res['vchr_house'];  ?>" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="52">&nbsp;</td>
    <td>Street</td>
    <td>:</td>
    <td><input type="text" name="txtstreet" id="txtstreet" value="<?php echo $res['vchr_street'];  ?>" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="55">&nbsp;</td>
    <td>City</td>
    <td>:</td>
    <td><input type="text" name="txtcity" id="txtcity" value="<?php echo $res['vchr_city'];  ?>" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="54">&nbsp;</td>
    <td>District</td>
    <td>:</td>
    <td><input type="text" name="txtdistrict" id="txtdistrict" value="<?php echo $res['vchr_district'];  ?>" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="50">&nbsp;</td>
    <td>State</td>
    <td>:</td>
    <td><input type="text" name="txtstate" id="txtstate" value="<?php echo $res['vchr_state'];  ?>" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="51">&nbsp;</td>
    <td>Phone</td>
    <td>:</td>
    <td><input type="text" name="txtphone" id="txtphone" value="<?php echo $res['vchr_phone'];  ?>" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="54">&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><input type="submit" name="Update" id="Update" value="Update" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>

    </form></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>

</body>
</html>

class Folder



registration_class.php


<?php
include_once("config.php");
class registration
{   
    public function insert($info, $table) {
            $sql = "INSERT INTO ".$table." (";
            for ($i=0; $i<count($info); $i++) {           
                $sql .= key($info);
                if ($i < (count($info)-1)) {
                    $sql .= ", ";
                } else $sql .= ") ";
                next($info);
            }
            reset($info);
            $sql .= "VALUES (";
            for ($j=0; $j<count($info); $j++) {
                $sql .= "'".current($info)."'";
                if ($j < (count($info)-1)) {
                   $sql .= ", ";
                } else $sql .= ") ";
                next($info);
            }
            //die($sql);
            $ex=mysql_query($sql) or die("query failed ".mysql_error());
            return $ex;
    }   
   
    public function update($data, $table, $where){
            $cols = array();
            foreach($data as $key=>$val) {
                $cols[] = "$key = '$val'";
            }
            $sql = "UPDATE $table SET " . implode(', ', $cols) . " WHERE $where";
            //die($sql);
            mysql_query($sql) or die("query failed ".mysql_error());
            return true;
    }

    public function select($table,$f1,$f2,$f3,$data,$pid,$weight){
            $sel="SELECT id from $table where $f1='$data' and $f2='$pid' and $f3='$weight'";
            $exe=mysql_query($sel);
            $res=mysql_fetch_array($exe);
            $did=$res[0];
            return $did;   
    }
       
    public function selectall($table){
        $sel="SELECT * from $table";
        //echo $sel;
        $exe=mysql_query($sel);
        return $exe;   
       
    }
    public function selectrow($table,$where){
        $sel="SELECT * from $table WHERE $where ";
        //die($sel);
        $exe=mysql_query($sel);
        $res=mysql_fetch_array($exe);
        //print_r($res);
        return $res;   
       
    }
}
?>



config.php


 <?php
$con=mysql_connect("localhost","root","") or die ("no connection to the db");
mysql_select_db("student",$con) or die ("failed");
?>





21 May 2012

Mysql Interview Questions

 

Write a simple query to get the second largest number of a table column?

 

               There are many ways that we can write this,
But the simplest way in mysql is,
  • SELECT * FROM country ORDER BY id DESC LIMIT 1, 1
plus there are some other methods as well,
  • SELECT * FROM country ORDER BY id DESC LIMIT 1 OFFSET 1
  • select max(id) from country where id not in(select max(id) from country)
  • SELECT min(id) FROM (SELECT DISTINCT id FROM country ORDER BY id DESC LIMIT 2) AS t
Only in sql server
  • SELECT min([column]) AS [column]
    FROM (
    SELECT TOP 2 [column]
    FROM [Table]
    GROUP BY [column]
    ORDER BY [column] DESC
    ) a

20 May 2012


PHP Interview Questions - V



What is the use of friend function?

                  Sometimes a function is best shared among a number of different classes. Such functions can be declared either as member functions of one class or as global functions. In either case they can be set to be friends of other classes, by using a friend specifier in the class that is admitting them. Such functions can use all attributes of the class which names them as a friend, as if they were themselves members of that class.
A friend declaration is essentially a prototype for a member function, but instead of requiring an implementation with the name of that class attached by the double colon syntax, a global function or member function of another class provides the match. 


How can I set a cron and how can I execute it in Unix, Linux, and windows?
 
                 
Cron is very simply a Linux module that allows you to run commands at predetermined times or intervals. In Windows, it's called Scheduled Tasks. The name Cron is in fact derived from the same word from which we get the word chronology, which means order of time. The easiest way to use crontab is via the crontab command.

# crontab

This command 'edits' the crontab. Upon employing this command, you will be able to enter the commands that you wish to run.  


The syntax of this file is very important – if you get it wrong, your crontab will not function properly. The syntax of the file should be as follows:

minutes hours day_of_month month day_of_week command

All the variables, with the exception of the command itself, are numerical constants. In addition to an asterisk (*), which is a wildcard that allows any value, the ranges permitted for each field are as follows:


Minutes: 0-59

Hours: 0-23
Day_of_month: 1-31
Month: 1-12
Weekday: 0-6



Steps for the payment gateway processing?

                  An online payment gateway is the interface between your merchant account and your Web site. The online payment gateway allows you to immediately verify credit card transactions and authorize funds on a customer's credit card directly from your Web site. It then passes the transaction off to your merchant bank for processing, commonly referred to as transaction batching 

What is the difference between Reply-to and Return-path in the headers of a mail function?

               Reply-to: Reply-to is where to delivery the reply of the mail.

Return-path: Return path is when there is a mail delivery failure occurs then where to delivery the failure notification


How many ways can we get the value of current session id?

             session_id() returns the session id for the current session.  
  
How do I find out the number of parameters passed into function? 

            func_num_args() function returns the number of parameters passed in.

What’s the special meaning of __sleep and __wakeup? 
            __sleep returns the array of all the variables than need to be saved, while __wakeup retrieves them.
  
I want to combine two variables together:
   $var1 = 'Welcome to ';
$var2 = 'TechInterviews.com'; 
What will work faster?
Code sample 1:
$var 3 = $var1.$var2;
Or code sample 2:
$var3 = "$var1$var2";
              Both examples would provide the same result - $var3 equal to "Welcome to TechInterviews.com". However, Code Sample 1 will work significantly faster. Try it out with large sets of data (or via concatenating small sets a million times or so), and you will see that concatenation works significantly faster than variable substitution.

What is CAPTCHA?

                    CAPTCHA stands for Completely Automated Public Turing Test to tell Computers and Humans Apart. To prevent spammers from using bots to automatically fill out forms, CAPTCHA programmers will generate an image containing distorted images of a string of numbers and letters. Computers cannot determine what the numbers and letters are from the image but humans have great pattern recognition abilities and will be able to fairly accurately determine the string of numbers and letters. By entering the numbers and letters from the image in the validation field, the application can be fairly assured that there is a human client using it. 

If you have to work with dates in the following format: "Tuesday, February 14, 2006 @ 10:39 am", how can you convert them to another format, that is easier to use?

                     The strtotime function can convert a string to a timestamp. A timestamp can be converted to date format. So it is best to store the dates as timestamp in the database, and just output them in the format you like.

So let's say we have

$date = "Tuesday, February 14, 2006 @ 10:39 am";
In order to convert that to a timestamp, we need to get rid of the "@" sign, and we can use the remaining string as a parameter for the strtotime function.

So we have

$date = str_replace("@ ","",$date);
$date = strtotime($date);

now $date is a timestamp

and we can say:

echo date("d M Y",$date

How i can get ip address?

We can use SERVER var $_SERVER['SERVER_ADDR'] and getenv("REMOTE_ADDR") functions to get the IP address.



 

 

 





PHP Interview Questions - IV

 

How can we know the count/number of elements of an array?

                2 ways:
a) sizeof($array) - This function is an alias of count()
b) count($array) - This function returns the number of elements in an array.
Interestingly if you just pass a simple var instead of an array, count() will return 1. 


How many ways we can pass the variable through the navigation between the pages?

                At least 4 ways:

1. Put the variable into session in the first page, and get it back from session in the next page.

2. Put the variable into cookie in the first page, and get it back from the cookie in the next page.
3. Put the variable into a hidden form field, and get it back from the form in the next page.
4. can pass the variabe as querystring with the help of question mark

Will comparison of string "10" and integer 11 work in PHP?

               Yes, internally PHP will cast everything to the integer type, so numbers 10 and 11 will be compared.

If we login more than one browser windows at the same time with same user and after that we close one window, then is the session is exist to other windows or not? And if yes then why? If no then why?

             Session depends on browser. If browser is closed then session is lost. The session data will be deleted after session time out. If connection is lost and you recreate connection, then session will continue in the browser. that means when you close a browser the other browser also lost the session values because session variables are stored in server.

What is the difference between PHP4 and PHP5?

            = PHP4 cannot support oops concepts and Zend engine 1 is used.
           = PHP5 supports oops concepts and Zend engine 2 is used.

           = Error supporting is increased in PHP5.
           = XML and SQLLite will is increased in PHP5.

What is meant by nl2br()?

            nl2br() inserts a HTML tag <br> before all new line characters \n in a string.
that is
echo nl2br("god bless \n you");
output:
god bless<br>
you

How can we increase the execution time of a php script?

            By the use of void set_time_limit(int seconds)
Set the number of seconds a script is allowed to run. If this is reached, the script returns a fatal error. The default limit is 30 seconds or, if it exists, the max_execution_time value defined in the php.ini. If seconds is set to zero, no time limit is imposed.


How can we get second of the current time using date function?

            $second = date("s"); 

What is the maximum size of a file that can be uploaded using PHP and how can we change this?

            You can change maximum size of a file set upload_max_filesize variable in php.ini file.default 2MB

What are the difference between abstract class and interface?

           Abstract class: abstract classes are the class where one or more methods are abstract but not necessarily all method has to be abstract. Abstract methods are the methods, which are declare in its class but not define. The definition of those methods must be in its extending class.

          Interface: Interfaces are one type of class where all the methods are abstract. That means all the methods only declared but not defined. All the methods must be define by its implemented class.

 


 






 



PHP Interview Questions - III

 

What is a PHP Session?

           
PHP session allows you to store the user session, like their information on server for faster access and for later use like username, IP addresses, their actions etc. The information which is saved is temporary and can be deleted after the user is no longer active. Example of starting a PHP session is as follows:

<?php 


session_start( );                                // start up your PHP session!

$_SESSION['user']="Premjith";              // store session data

$a=$_SESSION['user']                           // retrieve session data
unset($_SESSION['user']);                    // for unsetting session variable
session_destroy( );                                //for destroying the session

?>

What is meant by PEAR in php? What is the purpose of it?

         PEAR stands for "PHP Extension and Application Repository". 


As the name suggests, it gives advanced functionality to the PHP language and include many applications which can be used on fly. The purpose of it is as follows:-

          - Open source structured library for PHP users.
          - Code distribution and package maintenance system.
          - Standard style for code written in PHP.



Is PHP a loosely Typed Language? What is the difference between strongly typed and loosely typed language?

          Yes, PHP is a loosely typed language. In this type of language variable doesn’t need to be declared before their use. This language converts the data according to its given value. Whereas, in strongly typed language first every type has to be declared (defined) then only it can be used.
 


What do you mean by Persistent Cookie?
 
         
A cookie which is stored in a cookie file permanently on the browser’s computer. 


Explain the "unlink" and "unset" functions.

        
unlink( ) function is for file system handling. It just deletes the file in context.
unset( ) function is for variable management. It makes a variable undefined


What is the default session time in PHP?
 
         
Until closing the browser.  


How Can we kow thediiferent betweeen two days? 

              The start date and end date can be first found as shown below:
$date1= strtotime($start_date);
$date2= strtotime($end_date);
$date_diff = (($date1)- ($date2)) / (60*60*24)


lets look at an example:

$date1 = "2007-03-24";
$date2 = "2007-04-26";

$diff = abs(strtotime($date2) - strtotime($date1));
echo("diff = ".$diff);

$years = floor($diff / (365*60*60*24));  //that is
31536000(365*60*60*24) seconds in an year   
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

printf("%d years, %d months, %d days\n", $years, $months, $days);




19 May 2012

PHP Interview Questions - II

I am trying to assign a variable the value of 0123, but it keeps coming up with a different number, what’s the problem?

            PHP Interpreter treats numbers beginning with 0 as octal.


What are the different tables present in MySQL? Which type of table is generated when we are creating a table in the following syntax: create table employee(eno int(2),ename varchar(10))?

           Total 5 types of tables we can create
1. MyISAM
2. Heap
3. Merge
4. INNO DB
5. ISAM
MyISAM is the default storage engine as of MySQL 3.23. When you fire the above create query MySQL will create a MyISAM table.

What is the functionality of the functions STRSTR() and STRISTR()?

string strstr ( string haystack, string needle ) returns part of haystack string from the first occurrence of needle to the end of haystack. This function is case-sensitive. stristr() is idential to strstr() except that it is case insensitive. 

When are you supposed to use endif to end the conditional statement?

               When the original if was followed by : and then the code block without braces.

How can we send mail using JavaScript?

              No. There is no way to send emails directly using JavaScript. 



What is the purpose of the following files having extensions: frm, myd, and myi? What these files contain?

             In MySQL, the default table type is MyISAM.
Each MyISAM table is stored on disk in three files. The files have names that begin with the table name and have an extension to indicate the file type.

The '.frm' file stores the table definition.
The data file has a '.MYD' (MYData) extension.
The index file has a '.MYI' (MYIndex) extension,

Are objects passed by value or by reference?

           Everything is passed by value.

WHAT ARE THE DIFFERENT TYPES OF ERRORS IN PHP?

           Here are three basic types of runtime errors in PHP:

1. Notices: These are trivial, non-critical errors that PHP encounters while executing a script - for example, accessing a variable that has not yet been defined. By default, such errors are not displayed to the user at all - although you can change this default behavior.

2. Warnings: These are more serious errors - for example, attempting to include() a file which does not exist. By default, these errors are displayed to the user, but they do not result in script termination.

3. Fatal errors: These are critical errors - for example, instantiating an object of a non-existent class, or calling a non-existent function. These errors cause the immediate termination of the script, and PHP's default behavior is to display them to the user when they take place.

Internally, these variations are represented by twelve different error types


Can we use include ("abc.php") two times in a php page "makeit.php"?

            Yes. 

How can we destroy the session, how can we unset the variable of a session?
 
session_unregister() - Unregister a global variable from the current session
session_unset() - Free all session variables 










 


PHP Interview Questions 

? What is PHP
                PHP is a server-side scripting language and it stands for “PHP Hypertext Preprocessor”.
  
? Who is the father of PHP
                Rasmus Lerdorf .

? What is the difference between echo() and Print() 
                echo( ) is more faster than print( ), print( ) returns whereas echo( ) doesn’t return, echo has void return type, echo( ) prints multiple value without a blank space when separated by the commas whereas print( )
inserts a single blank space with each commas.

? What is the use (.) operator in PHP
                It is used to concatinate the two or more variables.

? What is the difference between $_GET , $_POST and $_REQUEST
               Both are used to retrieve the information from a form,like user input
in $_GET , input values are visible in the browser’s address bar but in case of $_POST it doesn’t
in $_GET variable has character length (Max. 100 character),$_POST have no length limit
we use $_GET when form method=”GET” and we use $_POST when method=”POST”. 
$_GET variable should not be used hen sending password or some sensitive information.
$_REQUEST contains the content of both $_GET ,$_POST and $_COOKIE. and it can be used to get
the result from form data sent with both GET and POST method.
  
? Do you heated about addslashes( ) in php
              Addslashes() function return a string with backslash before the predefined character.This function can be used to prepare a string for storage in a database and database query. The predefined characters are
  • single quote ( ‘ )
  • double quote ( “ )
  • backslash ( \ )
  • NULL 
? What will be the out put of following code 
   <?php 
               $x   =   10;
               $y   =    x;
               echo $$y;
     ?> 
                out put10
? What will be the out put of following code <?php $x=10; echo $$x;  ?>
                Nothing


? How do you define a constant
               Via define() directive, like define ("MYCONSTANT", 100);

? What are the differences between require and include, include_once
               All three are used to an include file into the current page. If the file is not present, require(), calls a fatal error, while in include() does not. The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement, with the only difference being that if the code from a file has already been included, it will not be included again. It des not call a fatal error if file not exists. require_once() does the same as include_once(), but it calls a fatal error if file not exists.