Custom Search

Thursday, February 28, 2013

PHP QUESTION AND ANSWERS


PHP QUESTION AND ANSWERS

1. What is the functionality of the function html entities?

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

2. How to set cookies?

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

3. How to store the uploaded file to the final location?

move_uploaded_file( string filename, string destination)

4. What type of headers have to be added in the mail function to attach a file?


$boundary = '--' . md5( uniqid ( rand() ) );
$headers = "From: \"Me\"\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"";

?>

5. How can we find the number of rows in a result set using php?


$result = mysql_query($any_valid_sql, $database_link);
$num_rows = mysql_num_rows($result);
echo “$num_rows rows found”;
?>

6. How can we know the number of days between two given dates using php?


$tomorrow = mktime(0, 0, 0, date("m") , date("d")+1, date("Y"));
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
echo ($tomorrow-$lastmonth)/86400;
?>

6. How to open a file?


$file = fopen("file.txt","r");
?>
How many open modes available when a file open in PHP?
r  , r+  , w  , w+  , a  , a+  , x  , x+

7. Explain soundex() and metaphone().

soundex()
The soundex() function calculates the soundex key of a string. A soundex key is a four character long alphanumeric string that represent English pronunciation of a word. he soundex() function can be used for spelling applications.

$str = "hello";
echo soundex($str);

?>

metaphone()
The metaphone() function calculates the metaphone key of a string. A metaphone key represents how a string sounds if said by an English speaking person. The metaphone() function can be used for spelling applications.

echo metaphone("world");
?>

8. Explain the types of functions for Splitting String?

Function                          Descriptions
1.
split()
Splits a string into an array by using a regular expression as the delimiter.
2.
spliti()
Splits a string into an array by a regular expression and is case insensitive.
3.
str_split()
Converts a string into an array where the size of the elements can be specified
4.
preg_split()
Splits up a string by a Perl compatible regular expression and returns an array of substrings
5.
explode()
Splits up a string by another string (not a regular expression) and returns an array
6.
implode()
Joins array elements together by a string and returns a string

9. Explain Whitespace Characters.

Whitespace Character
ASCII Value(Decimal/Hex)
Descriptions
" "
32 (0x20))
An ordinary space
"\t"
9(0x0)
A tab.
"\n"
10(0x0A)
A newline (line feed).
"\r"
13(0x0D))
A carriage return.
"\0"
0(0x00))
The NULL-byte.
"\x0B"
11(0x0B))
A vertical tab.


10. What do you mean range()?

Starting from a low value and going to a high value, the range() function creates an array of consecutive integer or character values. It takes up to three arguments: a starting value, an ending value, and an increment value. If only two arguments are given, the increment value defaults to 1.
Example :

echo range(1,10); // Returns 1,2,3,4,5,6,7,8,9,10
?>

11. Explain Creating and Naming an Array.


Function
Descriptions
1.
array()
Creates an array
2.
array_combine()
Creates an array by using one array for keys and another for its values
3.
array_fill()
Fills an array with values
4.
array_pad()
Pads an array to the specified length with a value
5.
compact()
Creates array containing variables and their values
6.
range()
Creates an array containing a range of elements

12. How to read and display a HTML source from the website url?


$filename="http://www.kaptivate.in/";
$fh=fopen("$filename", "r");
while( !feof($fh) ){
$contents=htmlspecialchars(fgets($fh, 1024));
print "
$contents
";
}
fclose($fh);

?>

13. How to display your correct URL of the current web page?


echo $_SERVER['PHP_SELF'];
?>

14. Explain $_FILES Superglobal Array.

Array
Descriptions
$_FILES['userfile']['name']
The original name of the file on the client machine.
$_FILES['userfile']['type']
The MIME type of the file, if the browser provided this information. An example would be "image/gif".
$_FILES['userfile']['size']
The size, in bytes, of the uploaded file.
$_FILES['userfile']['tmp_name']
The temporary filename of the file in which the uploaded file was stored on the server.
$_FILES['userfile']['error']

The error code associated with this file upload.
15. Explain mysql_error().


Database Function
Descriptions
1.
mysql_connect()
Opens a connection to a MySQL server.
2.
mysql_pconnect()
Opens a persistent connection.
3.
mysql_selectdb()
Selects the default database.
4.
mysql_change_user()
Changes the identity of the user logged on.
5.
mysql_list_dbs
Lists databases for this MySQL server.
6.
mysql_list_tables
Lists tables in the database.


16. How to get no. of rows using MYSQL function?

Database Function
Descriptions
1.
mysql_fetch_assoc()
Returns one result row, as an associative array.
2.
mysql_fetch_row()
Returns one result row, as an array.
3.
mysql_affected_rows()
Returns number of rows affected by query.
4.
mysql_num_rows()
Returns number of rows selected.
5.
mysql_list_dbs
Lists databases for this MySQL server.
6.
mysql_fetch_object()
Returns a result row, as an object.
17. Explain mysql_errno().

Returns the numerical value of the error message from previous MySQL operation.
What types of MYSQL function available for affecting columns
Array
Descriptions
mysql_fetch_field()
Gets column information from a result and returns as an object.
mysql_field_name()
Gets the name of the specified field in a result.
mysql_list_fields()
Sets result pointer to a specified field offset.
mysql_num_fields()
Gets number of fields in a result.
mysql_field_seek()
Sets result pointer to a specified field offset.
mysql_field_type()
Gets the type of the specified field in a result.
mysql_field_len()
Returns the length of the specified field.
mysql_field_table()
Gets name of the table the specified field is in.
mysql_tablename()
Gets table name of field.

18. What is Constructors and Destructors?

CONSTRUCTOR : PHP allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.

DESTRUCTORS : PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++. The destructor method will be called as soon as all references to a particular object are removed or when the object is explicitly destroyed or in any order in shutdown sequence.

19. Why do we create an instance of a class?

To create an instance of a class, the new keyword must be used. An object will always be created unless the object has a constructor defined that throws an exception on error. Classes should be defined before instantiation (and in some cases this is a requirement).

If a string containing the name of a class is used with new, a new instance of that class will be created. If the class is in a namespace, its fully qualified name must be used when doing this.

20. What is properties of class?

Class member variables are called "properties". We may also see them referred to using other terms such as "attributes" or "fields", but for the purposes of this reference we will use "properties". They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.
Explain Constant in Class.
It is possible to define constant values on a per-class basis remaining the same and unchangeable. Constants differ from normal variables in that we don't use the $ symbol to declare or use them.

The value must be a constant expression, not (for example) a variable, a property, a result of a mathematical operation, or a function call.
Explain the visibility of the property or method.
The visibility of a property or method must be defined by prefixing the declaration with the keywords public, protected or private.

§  Class members declared public can be accessed everywhere.
§  Members declared protected can be accessed only within the class itself and by inherited and parent classes.
§  Members declared as private may only be accessed by the class that defines the member.

No comments:

Post a Comment