Showing posts with label Php Interview Questions. Show all posts
Showing posts with label Php Interview Questions. Show all posts

Testing working using phpinfo()

Let’s install Apache, PHP, and MySQL under a Unix environment. First, you need to
decide which extra modules you will load under the trio. Because some of the examples
covered in this book use a secure server for web transactions, you should install an SSLenabled
server.
For purposes of this book, the PHP configuration is more or less the default setup but
also covers ways to enable the gd2 library under PHP.
The gd2 library is just one of the many libraries available for PHP.We included this
installation step so that you can get an idea of what is required to enable extra libraries
within PHP. Compiling most Unix programs follows a similar process.
You usually need to recompile PHP after installing a new library, so if you know
what you need in advance, you can install all required libraries on your machine and
then begin to compile the PHP module.
Here, we describe installation on an SuSE Linux server, but the description is generic
enough to apply to other Unix servers.
Start by gathering the required files for the installation.You need these items:
n Apache (http://httpd.apache.org/)—The web server
n OpenSSL (http://www.openssl.org/)—Open source toolkit that implements the
Secure Sockets Layer
n MySQL (http://www.mysql.com/)—The relational database
n PHP (http://www.php.net/)—The server-side scripting language
n ftp://ftp.uu.net/graphics/jpeg/—The JPEG library, needed for PDFlib and gd
n http://www.libpng.org/pub/png/libpng.html—The PNG library, needed for gd
n http://www.zlib.net/—The zlib library, needed for the PNG library, above
n http://www.libtiff.org/—The TIFF library, needed for PDFlib
n ftp://ftp.cac.washington.edu/imap/—The IMAP c client, needed for IMAP


Is PHP  Working?
Now you can test for PHP support. Create a file named test.php with the following
code in it.The file needs to be located in document root path, which should be set up,
by default, to /usr/local/apache/htdocs. Note that this path depends on the directory
prefix that you chose initially. However, you could change this in the httpd.conf file:
<?php phpinfo(); ?>

Query Strings

Query string data is very easy for the user to alter, because it ’ s visible and editable within the browser ’ s
address bar. Therefore, query strings should be used only in situations where sending incorrect data
won ’ t compromise security.

You also need to make sure you don ’ t rely on query strings to authenticate users, because people often
send URLs to friends in emails or instant messaging applications. If your URL contains all the data
needed to authenticate a user, and that user sends the URL to a friend, then the friend can pretend to be
them! You ’ ll find that sessions — discussed later in the chapter — are a much better way of authenticating
users.
If you ’ ve worked your way through Chapter 9 , you ’ re already somewhat familiar with the concept of
query strings. You ’ ll remember that you can embed sent form data in a URL by setting the form ’ s
method attribute to get . When the form data is sent to the server.

http://localhost/iscript.php?firstname=Tred & lastname=Fish& ...

the browser adds a query ( ? ) character to the end of the URL, then follows it with each
of the form fields as “name=value” pairs, with each pair separated by an ampersand ( & ).

Passing References to Your Own Functions php

By passing a reference to a variable as an argument to a function, rather than the variable itself, you pass
the argument by reference , rather than by value. This means that the function can now alter the original
value, rather than working on a copy.
To get a function to accept an argument as a reference rather than a value, put an ampersand ( & ) before
the parameter name within the function definition:
function myFunc( & $aReference ){
// (do stuff with $aReference)
}
Now, whenever a variable is passed to myFunc() , PHP actually passes a reference to that variable, so
that myFunc() can work directly with the original contents of the variable, rather than a copy.
Now that you know this, you can fix the earlier counter example by using a reference:
function resetCounter( & $c ) {
$c = 0;
}
$counter = 0;
$counter++;
$counter++;
$counter++;
echo “$counter < br/ > ”; // Displays “3”
resetCounter( $counter );
echo “$counter < br/ > ”; // Displays “0”
The only change in the script is in the first line:
function resetCounter( & $c ) {
Adding the ampersand before the $c causes the $c parameter to be a reference to the passed argument
( $counter in this example). Now, when the function sets $c to zero, it ’ s actually setting the value of
$counter to zero, as can be seen by the second echo statement.
Many built - in PHP functions accept references in this way. For example, PHP ’ s sort() function, which you
met in the previous chapter, changes the array you pass to it, sorting its elements in order. The array is passed
in by reference rather than by value, so that the function can change the array itself.

find the number of parameters passed into function in PHP?

func_num_args() function returns the number of parameters/arguments passed to a function in PHP.

difference between include and require?


Answer: It"s how they handle failures. If the file is not found by require(), it will cause a fatal error and halt the
execution of the script. If the file is not found by include(), a warning will be issued, but execution will
continue.

What is SQL Injection?

SQL Injection is the hacking technique which attempts to pass SQL commands (statements) through a web application for execution by the backend database.

it can be prevented by mysql_real_escape_string() function of PHP.

Such features as login pages, support and product request forms, feedback forms, search pages, shopping carts and the general delivery of dynamic content, shape modern websites and provide businesses with the means necessary to communicate with prospects and customers.

what is ob_start?

ob_start turns on output buffering,

Scope Resolution Operator?

::  is the scope operator it is used to call methods of a class that has not been instantiated.

What is MVC?

MVC- Model, View, Controller - is simply
Model -  contains data access code and all of you business logic code.
View - Contains markup/design code, generally html,xml, json.
Controller -  contains very little code, just whatever is needed to call the Model code and render the View code.

how access a method in the parent class that's been overridden in the child?

Prefix parent:: to the method name:
class shape {
    function draw( ) {
        // write to screen
    }
}

class circle extends shape {
   function draw($origin, $radius) {
      // validate data
      if ($radius > 0) {
          parent::draw( );
          return true;
      }

      return false;
   }
}

how execute different code depending on the number and type of arguments passed to a method?

PHP doesn't support method polymorphism as a built-in feature. However, you can emulate it using various type-checking functions. The following combine( ) function uses is_numeric(), is_string(), is_array(), and is_bool():
// combine() adds numbers, concatenates strings, merges arrays,
// and ANDs bitwise and boolean arguments
function combine($a, $b) {
    if (is_numeric($a) && is_numeric($b)) {
        return $a + $b;
    }

    if (is_string($a)  && is_string($b))  {
        return "$a$b";
    }

    if (is_array($a)   && is_array($b))   {
        return array_merge($a, $b);
    }

    if (is_bool($a)    && is_bool($b))    {
        return $a & $b;
    }

    return false;
}

want to link two objects, so when you update one, you also update the other?

Use =& to assign one object to another by reference:
$adam = new user;
$dave =& $adam;

how eliminate an object?

Objects are automatically destroyed when a script terminates. To force the destruction of an object, use unset( ):
$car = new car; // buy new car
...
unset($car);    // car wreck

how create a new instance of an object?

Define the class, then use new to create an instance of the class:
class user {
    function load_info($username) {
       // load profile from database
    }
}

$user = new user;
$user->load_info($_REQUEST['username']);

exchange the values in two variables without using additional variables for storage?

To swap $a and $b:
list($a,$b) = array($b,$a);

access the values passed to a function?how

Use the names from the function prototype:
function commercial_sponsorship($letter, $number) { 
    print "This episode of Sesame Street is brought to you by ";
    print "the letter $letter and number $number.\n";
}

commercial_sponsorship('G', 3);
commercial_sponsorship($another_letter, $another_number);

how associate multiple elements with a single key?

Store the multiple elements in an array:
$fruits = array('red' => array('strawberry','apple'),
                'yellow' => array('banana'));
Or, use an object:
while ($obj = mysql_fetch_object($r)) {
    $fruits[ ] = $obj;
}

How to check whether two floating-point numbers are equal?

Use a small delta value, and check if the numbers are equal within that delta:
$delta = 0.00001;

$a = 1.00000001;
$b = 1.00000000;

if (abs($a - $b) < $delta) { /* $a and $b are equal */ }

You want to extract part of a string, starting at a particular place in the string

Use substr( ) to select your substrings:
$substring = substr($string,$start,$length);
$username = substr($_REQUEST['username'],0,8);

Php get_meta_tags-Extracts all meta tag content attributes

get_meta_tagsExtracts all meta tag content attributes from a file and returns an array

<?php// Assuming the above tags are at www.example.com$tags get_meta_tags('http://www.example.com/');
// Notice how the keys are all lowercase now, and
// how . was replaced by _ in the key.
echo $tags['author'];       // nameecho $tags['keywords'];     // php documentationecho $tags['description'];  // a php manualecho $tags['geo_position']; // 49.33;-86.59?>