Thursday, May 5, 2011

PHP Interview Questions 6-10

7. How can we extract string ‘techinterviews.com ‘ from a string ‘http://www.techinterviews.com’ using regular expression of PHP?


    Type 1

    '<'?php
    preg_match
    ('@^(?:http://)?([^/]+)@i',
    "http://www.php.net/index.html",$matches);
    $host=$matches[1];


    preg_match('/[^.]+\.[^.]+$/', $host, $matches);
    echo
    "domain name is: {$matches[0]}\n";
    ?>

type 2

    '<'?php

    preg_match ( "/^(http:\/\/www\.)?([^\/]+)/i" , "http://www.jacksiva.blogpot.com/siva/jack/index.html" , $matches );

    echo $matches [ 2 ].'
    ';
    //echo $host -> jacksiva.blogspot.com

    ?>

    type 3

    preg_match ( "/^(http:\/\/)?([^\/]+)/i" , "http://www.jacksiva.blogpot.com/siva/index.html" , $matches );

    $host = $matches [ 2 ]; //echo $host -> www.jacksiva.blogspot.com

    preg_match ( "/[^.]+\.[^.]+\.[^.]+$/i" , $host, $matches );

    echo $host2 = $matches[0];

    ?>

    output: php.net

    note:

    asterisk ("*") has the meaning "some or none" or "zero or more."

    The plus-sign ("+") means "one or more times"

    The question-mark ("?") means "zero or one times."


    email validation:

    '<'?php
    if (isset($_POST['submit'])){
    $emailfield=$_POST['emailfield'];
    echo $emailfield;
    $okay=
    preg_match('/^[A-z0-9_\-\.]+[@][A-z0-9_\-]+([.][A-z0-9_\-])+[A-z]{2,4}$/',$emailfield);
    if($okay){
    echo "E-mail is validated";
    }else{
    echo"E-mail is incorrect";
    }
    }
    ?>
    '<'form method="POST" action="email.php">
    E-mail address: '<'input type="text" name="emailfield">
    '<'input type="submit" name="submit" value="Validate">

8.How can we create a database using PHP and mysql?
$con = mysql_connect("localhost","peter","abc123");
mysql_query("CREATE DATABASE my_db",$con);

9. What are the differences between require and include, include_once?

    include();--> include the file, but in case of file missing
    throws a warning and continues execution of code of next line.
    require();; require also includes the file but in case of file missing throws an fatal error
    and stop the execution of
    the code of next line.
    include_once();if same file was included first it will not include the file another time.
    but include( ) using more than one time.

    10. Can we use include (”techinterviews.php”) two times in a PHP page “makeit.PHP”?

    Yes. To use include () two time is possible.



PHP Interview Questions 1-6


PHP INTERVIEW QUESTIONS


  1. What are the differences between Get and post methods in form submitting, give the case where we can use get and we can use post methods?

    Ans:

    using get method is not secure, ur informations will be appeared in the url address. using post method is much secure it will not appear in the url address. eg.secure for using post method in login form.


    Get-> we can transfer limited data and its not secure. (256char)
    post-> we can transfer unlimited data. ans its a secure.

  2. Who is the father of PHP and explain the changes in PHP versions?

    Ans:


    Father - Rasmus lerdorf

    Released php versions - > 1997 – php3, 1998 – php4 , 2004 – php5

  3. How can we submit a form without a submit button?

    You can attach the document.formname.submit() method to onclick,onchange events of different inputs and perform the form submission.

  4. In how many ways we can retrieve the date in the result set of mysql using PHP?

  • mysql_fetch_array - Fetch a result row as an associative array, a numeric array, or both

  • mysql_fetch_assoc - Fetch a result row as an associative array

  • mysql_fetch_object - Fetch a result row as an object

  • mysql_fetch_row —- Get a result row as an enumerated array


  1. What is the difference between mysql_fetch_object and mysql_fetch_array?

    Mysql_fetch_object returns the result from the database as objects while mysql_fetch_array returns result as an array. This will allow access to the data by the field names.

    mysql_fetch_object: Results are objects returned from database. Fields are accessible like
    $result->name, $result->cust_name, where $result is the result object and name, cust_name are the fields.
    mysql_fetch_array : Results are arrays returned from database. Fields are accessible like
    $result[name], $result[cust_name].

  2. What is the difference between $message and $$message?

    $message – php variable

    $$message – Reference variable.