PHP

  Home  Computer Programming  PHP


“Learn PHP programming with Interview Questions and Answers and examples.”



139 PHP Questions And Answers

101⟩ What are the differences between mysql_fetch_array(), mysql_fetch_object(), mysql_fetch_row()?

Answer 1:

mysql_fetch_array() -> Fetch a result row as a combination of associative array and regular array.

mysql_fetch_object() -> Fetch a result row as an object.

mysql_fetch_row() -> Fetch a result set as a regular array().

Answer 2:

The difference between mysql_fetch_row() and mysql_fetch_array() is that the first returns the results in a numeric array ($row[0], $row[1], etc.), while the latter returns a the results an array containing both numeric and associative keys ($row['name'], $row['email'], etc.). mysql_fetch_object() returns an object ($row->name, $row->email, etc.).

 236 views

102⟩ What is meant by nl2br()?

Anwser1:

nl2br() inserts a HTML tag <br> before all new line characters n in a string.

echo nl2br("god bless n you");

output:

god bless<br>

you

 193 views

103⟩ 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.

 198 views

104⟩ What are the functions for IMAP?

imap_body - Read the message body

imap_check - Check current mailbox

imap_delete - Mark a message for deletion from current mailbox

imap_mail - Send an email message

 198 views

107⟩ What is the functionality of the function htmlentities?

htmlentities() - Convert all applicable characters to HTML entities

This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.

 191 views

108⟩ 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.

When called, set_time_limit() restarts the timeout counter from zero. In other words, if the timeout is the default 30 seconds, and 25 seconds into script execution a call such as set_time_limit(20) is made, the script will run for a total of 45 seconds before timing out.

 189 views

109⟩ How to set cookies in PHP?

setcookie('variable','value','time')

;

variable - name of the cookie variable

value - value of the cookie variable

time - expiry time

Example: setcookie('Test',$i,time()+3600);

Test - cookie variable name

$i - value of the variable 'Test'

time()+3600 - denotes that the cookie will expire after an one hour

 168 views

110⟩ WHAT TYPES OF IMAGES THAT PHP SUPPORTS?

Using imagetypes() function to find out what types of images are supported in your PHP engine.

imagetypes() - Returns the image types supported.

This function returns a bit-field corresponding to the image formats supported by the version of GD linked into PHP. The following bits are returned, IMG_GIF | IMG_JPG | IMG_PNG | IMG_WBMP | IMG_XPM.

 204 views

111⟩ How to reset/destroy a cookie in PHP?

Reset a cookie by specifying expire time in the past:

Example: setcookie('Test',$i,time()-3600); // already expired time

Reset a cookie by specifying its name only

Example: setcookie('Test');

 213 views

112⟩ How can I know that a variable is a number or not using a JavaScript?

Answer 1:

bool is_numeric( mixed var)

Returns TRUE if var is a number or a numeric string, FALSE otherwise.

Answer 2:

Definition and Usage

The isNaN() function is used to check if a value is not a number.

Syntax

isNaN(number)

Parameter Description

number Required. The value to be tested

 214 views

116⟩ What is the use of friend function in PHP?

Friend functions

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.

class mylinkage

{

private:

mylinkage * prev;

mylinkage * next;

protected:

friend void set_prev(mylinkage* L, mylinkage* N);

void set_next(mylinkage* L);

public:

mylinkage * succ();

mylinkage * pred();

mylinkage();

};

void mylinkage::set_next(mylinkage* L) { next = L; }

void set_prev(mylinkage * L, mylinkage * N ) { N->prev = L; }

Friends in other classes

It is possible to specify a member function of another class as a friend as follows:

class C

{

friend int B::f1();

};

class B

{

int f1();

};

It is also possible to specify all the functions in another class as friends, by specifying the entire class as a friend.

class A

{

friend class B;

};

 207 views

119⟩ 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.

 224 views

120⟩ What are the advantages of stored procedures, triggers, indexes in PHP?

A stored procedure is a set of SQL commands that can be compiled and stored in the server. Once this has been done, clients don't need to keep re-issuing the entire query but can refer to the stored procedure. This provides better overall performance because the query has to be parsed only once, and less information needs to be sent between the server and the client. You can also raise the conceptual level by having libraries of functions in the server. However, stored procedures of course do increase the load on the database server system, as more of the work is done on the server side and less on the client (application) side. Triggers will also be implemented. A trigger is effectively a type of stored procedure, one that is invoked when a particular event occurs. For example, you can install a stored procedure that is triggered each time a record is deleted from a transaction table and that stored procedure automatically deletes the corresponding customer from a customer table when all his transactions are deleted. Indexes are used to find rows with specific column values quickly. Without an index, MySQL must begin with the first row and then read through the entire table to find the relevant rows. The larger the table, the more this costs. If the table has an index for the columns in question, MySQL can quickly determine the position to seek to in the middle of the data file without having to look at all the data.

 225 views