PHP Descriptive Question and Answer
![]() |
| PHP- youngheart tk |
1) What are the general language features of php?
Ans: The general features of php are:
- PHP allows the user to build powerful applications even with a minimum of knowledge.
- PHP is a loosely typed language, meaning there is no need to explicitly create, typecast or destroy a variable.
- PHP developers are rarely bound to any single implementation solution. PHP user is typically fraught with choices offered by the language.
- PHP is available free of charge. PHP has no redistribution restriction.
- PHP developers have almost 200 native libraries at their disposal, collectively containing well over 1000 functions, in addition to thousands of third-party extensions.
2) Between echo () and print () functions which one is the faster and why?
Ans: Both The echo() and print() functions are used to output data to the browser. But there are technically differences between echo() and print() functions. The echo() function is a tad faster because it returns nothing, whereas print() function will return 1 if the statement is successfully output. So the echo() function is faster than print() function to output data to the browser.
3) What are type casting and juggling?
Ans : Type casting: Converting values from one data type to another is known as type casting. A variable can be evaluated once as a different type by casting it to another. This is accomplished by placing the intended type in front of the variable to be cast.
Example: $score = (double) 13; //$score = 13.0
Type Juggling: Because of PHP’s lax attitude toward type definitions, variables are sometimes automatically cast to best fit the circumstances in which they are referenced. This automatic conversion of values from one data type to another is known as type juggling
4)What is constant? How can you declare a constant?
Ans : A constant is a value that cannot be modified throughout the execution of a program. Constants are particularly useful when working with values that definitely will not require modification, such as Pi (3.141592).
Constants are defined using the define() function. The define() function defines a constant by assigning a value to a name. Its’ prototype follow:
Boolean define (string name, mixed value [, bool case_insensitive])
5) Which functions are used to add file in a script?
Ans: PHP offers four functions for including files into applications. These are:
include(), require(), include_once(), require_once().
6. What are passing arguments by reference and by value?
Ans : By reference: Any changes made to an argument within a function to be reflected outside of the function’s scope is called passing arguments by reference. Passing an argument by reference is accomplished by appending an ampersand to the front of the argument.
By value: Any changes made to the values within the scope of the function are called passing argument by value. Passing arguments by value is useful when we want to pass data into the same function.
7. What is recursive function?
Ans : Recursive functions that call themselves, offer considerable practical value to the programmer and are used to divide an otherwise complex problem into a simple case, reiterating that case until the problem is resolved.
8. What are the types of key of array?
Ans : The key serves as the lookup facility for retrieving its’ counterpart, the value. The types of key of array are numerical and associative. Numerical keys bear no real relation to the value rather the value’s position in the array. An associative key logically bears a direct relation to its corresponding value.
9. What are class and object?
Ans : Class: The embodiment of an entity’s defining attributes and behaviors is known as a class. Classes are intended to represent the real-life items that we would like to manipulate within an application.
Object: A class provides a basis from which we can create specific instances of the entity, the class models which is better known as objects. For example, an employee management application may include an Employee class. We can then call upon this class to create and maintain specific instances.
10. What is property overloading?
Ans : Property overloading continues to protect properties by forcing access and manipulation through public methods, yet allowing the data to be accessed as if it were a public property. These methods, known as getters and setters, are automatically triggered whenever the property is accessed or manipulated, respectively.
11. What are constructor and destructor?
Ans : Constructor: A constructor is defined as a block of code that automatically executes at the time of object instantiation. PHP recognizes constructors by the name __construct() [a double underscore precedes the constructor keyword].
Destructor: We can use destructors to modify the object destruction process. Destructors are created like any other method but must be titled __destruct() [a double underscore precedes the constructor keyword].
12. What is object cloning?
Ans: Object cloning offers a gateway to copy a referenced object without pointing back to the addressing location of the original object.
We clone an object by prefacing it with the clone keyword, like so:
destinationObject = clone targetObject;
13. What type of inheritance that PHP supports
Ans : Inheritance is a well-established programming principle, and PHP makes use of this principle in its object model. This principle will affect the way many classes and objects relate to one another. The type of inheritance that PHP support are :
Single level
hierarchical level
Multilevel
14. What are the abstract class and interface?
Ans : Abstract class: An abstract class is a class that cannot be instantiated. Abstract classes are intended to be inherited by a class that can be instantiated, better known as a concrete class. Abstract classes can be fully implemented, partially implemented or not implemented at all.
Interfaces: An interface is a collection of unimplemented method definitions and constants that serves as a class blueprint. Interfaces define exactly what can be done with the class, without getting bogged down in implementation-specific details.
15. Define exceptions?
Answer: Programming, like the real world, often involves unforeseen happenings that disrupt the flow of events. In programmer’s lingo, these unexpected happenings are known as exceptions. In this case, exception handling is used to change the normal flow of the code execution if a specified error condition (exception) occurs.
16. What is regular expression?
Answer: Regular expressions provide the foundation for describing or matching data according to defined syntax rules. A regular expression is nothing more than a pattern of characters itself, matched against a certain parcel of text.
17. How many ways can you read a file?
Ans : PHP offers numerous methods for reading data from a file, ranging from reading in just one character at a time to reading in the entire file with a single operation.
Example: file(), file_get_contents(), fgetcsv(), fgets(), fgetss(), fgetc(), fread(), readfile(),fscan().
18. How many ways you can write into a file?
Ans: We can write a file using the following method and modes:
Method: fwrite() .
Modes: r+, W, w+,A,a+,X,x+.
19. What is the functionality of the function strstr and stristr?
Answer: The strstr() function returns the remainder of a string beginning with the first occurrence of a predefined string whereas the stristr() function searches for the first occurrence of a string inside another string.
20. What is the difference between ereg_replace() and eregi_replace()?
Answer:The ereg_replace() function operates to finding and replacing a pattern with a replacement string whereas the eregi_replace() function operates exactly like ereg_replace(), except that the search for pattern in string is not case sensitive.
21.What are the different functions in sorting an array? Discuss them?
Answer:
array_reverse(): reverses an array’s element order.
array_flip(): reverses the roles of the keys and their corresponding values.
sort(): sorts an array, ordering elements from lowest to highest value.
rsort(): sorts array items in reverse (descending) order.
asort() : sorting an array in ascending order, except that the key/value correspondence is maintained.
arsort(): it sorts the array in reverse order maintained key/value co-relation.
22. What is super global variable? Write name of some superglobal variable?
Answer: PHP offers a number of useful predefined variables that are accessible from anywhere within the executing script which are known as super global.
Some super global are: $_SERVER, $_GET, $_POST, $_COOKIE, $_FILES, $_ENV, $_SESSION,$GLOBAL,$_REQUEST,
23.What is the difference between print and printf?
The print() statement outputs data passed to it whereas the printf() statement is ideal when we want to output a blend of static text and dynamic information stored within one or several variables.
24. Discuss five predefined subscript of $_FILES super global variable?
Answer:
- a. $_FILES['upload-name']['name']: The name of the file as uploaded from
- the client to the server.
- b. $_FILES['upload-name']['type']: The MIME type of the uploaded file.
- Whether this variable is assigned depends on the browser capabilities.
- c. $_FILES['upload-name']['size']: The byte size of the uploaded file.
- d. $_FILES['upload-name']['tmp_name']: Once uploaded, the file will be
- assigned a temporary name before it is moved to its final location.
- e. $_FILES['upload-name']['error']: An upload status code.
25. Why substr() is used?
Answer: The substr() function returns the part of a string located between a predefined starting offset and length positions.
Its prototype : string substr(string str, int start [, int length])
26. Why explode() is used?
Answer: The explode() function splits a string into a series of substrings, with each string boundary determined by a specific separator.
Its prototype: explode(string separator, string string [, int limit]).
27.Difference between array_merge() and array_slice()?
Answer: The array_merge() function merges arrays together, returning a single, unified array whereas the array_slice() function returns a section of an array based on a starting and ending offset value.
28. What are the advantage of object oriented programming?
Answer: Object-Oriented Programming has the following advantages over conventional approaches:
The complexity of the software can be merged easily.
The data hiding concept helps the programmer to build secure programs.
Through class concept we can define the user defined data types.
The inheritance concept can be used to eliminate the redundant code.
The message passing concept helps the programmer for communicating between different objects.
New data and functions can be easily coded whenever necessary.
OOP ties data elements more closely to the functions that operates on.
29. What is the benefits of PEAR?
Answer : PEAR is a framework and distribution system for reusable PHP components. It presently offers more than 550 packages categorized under 37 different topics. Furthermore, because many PEAR packages logically implement common tasks guaranteed to repeatedly occur no matter the type of application, taking advantage of this community-driven service will save countless hours of programming time.
30. What are the PHP’s authentication variables?
Ans: PHP’s authentication variables are used to authenticate the user’s username and password values. PHP uses two predefined variables to authenticate a user:
$_SERVER[‘PHP_AUTH_USER’]
$_SERVER[‘PHP_AUTH_PW’]
These variables store the username and password values, respectively.
31. What are the regular expressions syntax?
Ans: Regular expression types
There are 2 types of regular expressions:
POSIX Extended
Perl Compatible
The ereg, eregi, ... are the POSIX versions and preg_match, preg_replace, ... are the Perl version. It is important that using Perl compatible regular expressions the expression should be enclosed in the delimiters, a forward slash (/), for example. However this version is more powerful and faster as well than the POSIX one.
32. What are the difference between GET and POST method?
GET and POST methods are the two common methods for passing data from one script to another. POST method is capable of handling considerably more data than GET method. In fact, the important characteristics of POST method is that it is used to insert and modify large blocks of text. Besides, POST method can send data to the URL.
33. What are the four configuration Directive scopes?
PHP_INI_PERDIR: Directive can be modified within the php.ini, httpd.conf, or .htaccess files.
PHP_INI_SYSTEM: Directive can be modified within the php.ini and httpd.conf files.
PHP_INI_USER: Directive can be modified within user scripts.
PHP_INI_ALL: Directive can be modified anywhere.
