How many times we need to write selectbox code 0n our page.
Lets say in my website there are 10 pages where i need select box for country.
so writing same code on all of 10 page is very iritating job.
the best way is to write either a function or class for that. with object-oriented concept of php 5 we can tremendously reduce that tedious code on every page.
just make a class file for that contains functions for every selectbox.
like if u want a country selectbox then
make a class file , call that class on ur page and aha!!!
you got the box. no need to write time and again same code on every page.
just type or copy this code for class file
class Select_Option_Class {
public function buildCountrySelect($country_id = false, $selectName = 'country') {
$this->countrySelect = '';
$sql = 'SELECT * FROM country ORDER BY printable_name';
$result = mysql_query($sql);
if (!$result || !mysql_num_rows($result)) return false;
while ($row = mysql_fetch_array($result)) {
$this->countrySelect .= ''."\n"; }
$this->countrySelect = ''."\n";
return $this->countrySelect;
}
}
include following line in ur config file so that you don't need to call it on every page
require_once('admin/class/Select_Option_Class.php');
$selectOption = new Select_Option_Class();
now finally use this line on ur page to call the function
echo $selectOption->buildFrontCountrySelect($country);
thats it.
here u can find that instead of writing selectbox code on every page. u are done with just a single line code.
enjoy coding!!!!!!!!!!!!!!!!!
Tuesday, July 29, 2008
Monday, July 28, 2008
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.On the server side, the main difference between GET and POST is where the submitted is stored. The $_GET array stores data submitted by the GET method. The $_POST array stores data submitted by the POST method.
On the browser side, the difference is that data submitted by the GET method will be displayed in the browser’s address field. Data submitted by the POST method will not be displayed anywhere on the browser.GET method is mostly used for submitting a small amount and less sensitive data. POST method is mostly used for submitting a large amount or sensitive data
2. Who is the father of php and explain the changes in php versions?
Ans.Rasmus Lerdorf for version changes go to http://php.net/ Marco Tabini is the founder and publisher of phparchitect.
3. How can we submit from without a submit button?
Ans.We can use a simple JavaScript code linked to an event trigger of any form field. In the JavaScript code, we can call the document.form.submit() function to submit the form. For example:
4. How many ways we can retrieve the date in result set of mysql Using php?
Ans.As individual objects so single record or as a set or arrays.
5. What is the difference between mysql_fetch_object and mysql_fetch_array?
Ans.MySQL fetch object will collect first single matching record where mysql_fetch_array will collect all matching records from the table in an array.
6. What is the difference between $message and $$message?
Ans.They are both variables. But $message is a variable with a fixed name. $$message is a variable who’s name is stored in $message. For example, if $message contains "var", $$message is the same as $var.
7. How can we extract string ‘abc.com ‘ from a string ‘http://info@a…’ using regular _expression of php?
Ans.We can use the preg_match() function with "/.*@(.*)$/" as the regular expression pattern. For example: preg_match("/.*@(.*)$/","http://info@abc.com",$data); echo $data[1];
8. How can we create a database using php and mysql?
Ans.PHP: mysql_create_db()Mysql: create database;
9. What are the differences between require and include, include_once?
Ans.File will not be included more than once. If we want to include a file once only and further calling of the file will be ignored then we have to use the PHP function include_once(). This will prevent problems with function redefinitions, variable value reassignments, etc.
10. Can we use include ("abc.php") two times in a php page "makeit.php"?
Ans.Yes we can include..
11. What are the different tables present in mysql, which type of table is generated when we are creating a table in the followingsyntax: create table employee(eno int(2),ename varchar(10)) ?
Ans.Total 5 types of tables we can create
1. MyISAM
2. Heap
3. Merge
4. InnoDB
5. ISAM
6. BDBMyISAM is the default storage engine as of MySQL 3.23.
12. How can I execute a php script using command line?
Ans.Just run the PHP CLI (Command Line Interface) program and provide the PHP script file name as the command line argument. For example, "php myScript.php", assuming "php" is the command to invoke the CLI program.Be aware that if your PHP script was written for the Web CGI interface, it may not execute properly in command line environment.
13. Suppose your ZEND engine supports the mode Then how can u configure your php ZEND engine to support mode ?
Ans.If you change the line: short_open_tag = off in php.ini file. Then your php ZEND engine support only mode.
14. What is meant by nl2br()?
Ans.nl2br — Inserts HTML line breaks before all newlines in a string string nl2br (string); Returns string with ‘’ inserted before all newlines. For example: echo nl2br("god bless\n you") will output "god bless \n you" to your browser.
15. What are the reasons for selecting lamp (Linux, apache, mysql, php) instead of combination of other software programs, servers and operating systems?
Ans.All of those are open source resource. Security of linux is very very more than windows. Apache is a better server that IIS both in functionality and security. Mysql is world most popular open source database. Php is more faster that asp or any other scripting language.
16. How can we encrypt and decrypt a data present in a mysql table using mysql?
Ans.AES_ENCRYPT () and AES_DECRYPT ()
17. How can we encrypt the username and password using php?
Ans.You can encrypt a password with the following Mysql>SET PASSWORD=PASSWORD("Password");We can encode data using base64_encode($string) and can decode using base64_decode($string);
18. What are the features and advantages of OBJECT ORIENTED PROGRAMMING?
Ans.One of the main advantages of OO programming is its ease of modification; objects can easily be modified and added to a system there by reducing maintenance costs. OO programming is also considered to be better at modeling the real world than is procedural programming. It allows for more complicated and flexible interactions. OO systems are also easier for non-technical personnel to understand and easier for them to participate in the maintenance and enhancement of a system because it appeals to natural human cognition patterns.For some systems, an OO approach can speed development time since many objects are standard across systems and can be reused. Components that manage dates, shipping, shopping carts, etc. can be purchased and easily modified for a specific system.
19. What are the differences between PROCEDURE ORIENTED LANGUAGES and OBJECT ORIENTED LANGUAGES?
Ans.Traditional programming has the following characteristics:
Functions are written sequentially, so that a change in programming can affect any code that follows it.If a function is used multiple times in a system (i.e., a piece of code that manages the date), it is often simply cut and pasted into each program (i.e., a change log, order function, fulfillment system, etc). If a date change is needed (i.e., Y2K when the code needed to be changed to handle four numerical digits instead of two), all these pieces of code must be found, modified, and tested.Code (sequences of computer instructions) and data (information on which the instructions operates on) are kept separate. Multiple sets of code can access and modify one set of data. One set of code may rely on data in multiple places. Multiple sets of code and data are required to work together. Changes made to any of the code sets and data sets can cause problems through out the system.
Object-Oriented programming takes a radically different approach:
Code and data are merged into one indivisible item – an object (the term "component" has also been used to describe an object.) An object is an abstraction of a set of real-world things (for example, an object may be created around "date") The object would contain all information and functionality for that thing (A dateobject it may contain labels like January, February, Tuesday, Wednesday. It may contain functionality that manages leap years, determines if it is a business day or a holiday, etc., See Fig. 1). Ideally, information about a particular thing should reside in only one place in a system. The information within an object is encapsulated (or hidden) from the rest of the system.A system is composed of multiple objects (i.e., date function, reports, order processing, etc., See Fig 2). When one object needs information from another object, a request is sent asking for specific information. (for example, a report object may need to know what today’s date is and will send a request to the date object) These requests are called messages and each object has an interface that manages messages.OO programming languages include features such as "class", "instance", "inheritance", and "polymorphism" that increase the power and flexibility of an object.
20. What is the use of friend function?
Ans.Friend functionsSometimes 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 whichnames 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;};
Friend functions allow binary operators to be defined which combine private data in a pair of objects. This is particularly powerful when using the operator overloading features of C++. We will return to it when we look at overloading.
21. What are the different types of errors in php?
ans.Three are three types of errors:
1. Notices: These are trivial, non-critical errors that PHP encounters while executing a script - for example, accessing a variable that has not yet been defined. By default, such errors are not displayed to the user at all - although, as you will see, you can change this default behaviour.
2. Warnings: These are more serious errors - for example, attempting to include() a file which does not exist. By default, these errors are displayed to the user, but they do not result in script termination.
3. Fatal errors: These are critical errors - for example, instantiating an object of a non-existent class, or calling a non-existent function. These errors cause the immediate termination of the script, and PHP's default behaviour is to display them to the user when they take place.
22. What is the functionality of the function strstr and stristr?
Ans.strstr() returns part of a given string from the first occurrence of a given substring to the end of the string. For example: strstr("user@example.com","@") will return "@example.com".stristr() is idential to strstr() except that it is case insensitive.
23. What is the functionality of the function htmlentities?
Ans: htmlentities — Convert all applicable characters to HTML entitiesThis function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.
24. How can we get second of the current time using date function?
$second = date("s");
25. What is meant by urlencode and urldocode?
Ans.urlencode() returns the URL encoded version of the given string. URL coding converts special characters into % signs followed by two hex digits. For example: urlencode("10.00%") will return "10%2E00%25?. URL encoded strings are safe to be used as part of URLs.urldecode() returns the URL decoded version of the given string.
26. What is the difference between the functions unlink and unset?
Ans.unlink() deletes the given file from the file system.unset() makes a variable undefined.
27. How can we register the variables into a session?
Ans.We can use the session_register ($ur_session_var) function.
28. How can we get the properties (size, type, width, height) of an image using php image functions?
Ans.To know the Image type use exif_imagetype () functionTo know the Image size use getimagesize () functionTo know the image width use imagesx () functionTo know the image height use imagesy() function
29. What is the maximum size of a file that can be uploaded using php and how can we change this?
Ans.You can change maximum size of a file set upload_max_filesize variable in php.ini file
30. How can we increase the execution time of a php script?
AnsSet max_execution_time variable in php.ini file to your desired time in second.
31. How can we take a backup of a mysql table and how can we restore it.?
Answer: Create a full backup of your database: shell> mysqldump –tab=/path/to/some/dir –opt db_name Or: shell> mysqlhotcopy db_name /path/to/some/dirThe full backup file is just a set of SQL statements, so restoring it is very easy:shell> mysql "."Executed";mysql_close($link2);
32. How many ways can we get the value of current session id?
Ans session_id() returns the session id for the current session.
33. How can we destroy the session, how can we unset the variable of a session?
Ans session_unregister — Unregister a global variable from the current sessionsession_unset — Free all session variables
34. How can we destroy the cookie?
Ans Set the cookie in past
35. How many ways we can pass the variable through the navigation between the pages?
Ans GET or QueryString and POST46. What is the difference between ereg_replace() and eregi_replace()?Ans:-eregi_replace() function is identical to ereg_replace() except that this ignores case distinction when matching alphabetic characters.eregi_replace() function is identical to ereg_replace() except that this ignores case distinction when matching alphabetic characters.
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.On the server side, the main difference between GET and POST is where the submitted is stored. The $_GET array stores data submitted by the GET method. The $_POST array stores data submitted by the POST method.
On the browser side, the difference is that data submitted by the GET method will be displayed in the browser’s address field. Data submitted by the POST method will not be displayed anywhere on the browser.GET method is mostly used for submitting a small amount and less sensitive data. POST method is mostly used for submitting a large amount or sensitive data
2. Who is the father of php and explain the changes in php versions?
Ans.Rasmus Lerdorf for version changes go to http://php.net/ Marco Tabini is the founder and publisher of phparchitect.
3. How can we submit from without a submit button?
Ans.We can use a simple JavaScript code linked to an event trigger of any form field. In the JavaScript code, we can call the document.form.submit() function to submit the form. For example:
4. How many ways we can retrieve the date in result set of mysql Using php?
Ans.As individual objects so single record or as a set or arrays.
5. What is the difference between mysql_fetch_object and mysql_fetch_array?
Ans.MySQL fetch object will collect first single matching record where mysql_fetch_array will collect all matching records from the table in an array.
6. What is the difference between $message and $$message?
Ans.They are both variables. But $message is a variable with a fixed name. $$message is a variable who’s name is stored in $message. For example, if $message contains "var", $$message is the same as $var.
7. How can we extract string ‘abc.com ‘ from a string ‘http://info@a…’ using regular _expression of php?
Ans.We can use the preg_match() function with "/.*@(.*)$/" as the regular expression pattern. For example: preg_match("/.*@(.*)$/","http://info@abc.com",$data); echo $data[1];
8. How can we create a database using php and mysql?
Ans.PHP: mysql_create_db()Mysql: create database;
9. What are the differences between require and include, include_once?
Ans.File will not be included more than once. If we want to include a file once only and further calling of the file will be ignored then we have to use the PHP function include_once(). This will prevent problems with function redefinitions, variable value reassignments, etc.
10. Can we use include ("abc.php") two times in a php page "makeit.php"?
Ans.Yes we can include..
11. What are the different tables present in mysql, which type of table is generated when we are creating a table in the followingsyntax: create table employee(eno int(2),ename varchar(10)) ?
Ans.Total 5 types of tables we can create
1. MyISAM
2. Heap
3. Merge
4. InnoDB
5. ISAM
6. BDBMyISAM is the default storage engine as of MySQL 3.23.
12. How can I execute a php script using command line?
Ans.Just run the PHP CLI (Command Line Interface) program and provide the PHP script file name as the command line argument. For example, "php myScript.php", assuming "php" is the command to invoke the CLI program.Be aware that if your PHP script was written for the Web CGI interface, it may not execute properly in command line environment.
13. Suppose your ZEND engine supports the mode Then how can u configure your php ZEND engine to support mode ?
Ans.If you change the line: short_open_tag = off in php.ini file. Then your php ZEND engine support only mode.
14. What is meant by nl2br()?
Ans.nl2br — Inserts HTML line breaks before all newlines in a string string nl2br (string); Returns string with ‘’ inserted before all newlines. For example: echo nl2br("god bless\n you") will output "god bless \n you" to your browser.
15. What are the reasons for selecting lamp (Linux, apache, mysql, php) instead of combination of other software programs, servers and operating systems?
Ans.All of those are open source resource. Security of linux is very very more than windows. Apache is a better server that IIS both in functionality and security. Mysql is world most popular open source database. Php is more faster that asp or any other scripting language.
16. How can we encrypt and decrypt a data present in a mysql table using mysql?
Ans.AES_ENCRYPT () and AES_DECRYPT ()
17. How can we encrypt the username and password using php?
Ans.You can encrypt a password with the following Mysql>SET PASSWORD=PASSWORD("Password");We can encode data using base64_encode($string) and can decode using base64_decode($string);
18. What are the features and advantages of OBJECT ORIENTED PROGRAMMING?
Ans.One of the main advantages of OO programming is its ease of modification; objects can easily be modified and added to a system there by reducing maintenance costs. OO programming is also considered to be better at modeling the real world than is procedural programming. It allows for more complicated and flexible interactions. OO systems are also easier for non-technical personnel to understand and easier for them to participate in the maintenance and enhancement of a system because it appeals to natural human cognition patterns.For some systems, an OO approach can speed development time since many objects are standard across systems and can be reused. Components that manage dates, shipping, shopping carts, etc. can be purchased and easily modified for a specific system.
19. What are the differences between PROCEDURE ORIENTED LANGUAGES and OBJECT ORIENTED LANGUAGES?
Ans.Traditional programming has the following characteristics:
Functions are written sequentially, so that a change in programming can affect any code that follows it.If a function is used multiple times in a system (i.e., a piece of code that manages the date), it is often simply cut and pasted into each program (i.e., a change log, order function, fulfillment system, etc). If a date change is needed (i.e., Y2K when the code needed to be changed to handle four numerical digits instead of two), all these pieces of code must be found, modified, and tested.Code (sequences of computer instructions) and data (information on which the instructions operates on) are kept separate. Multiple sets of code can access and modify one set of data. One set of code may rely on data in multiple places. Multiple sets of code and data are required to work together. Changes made to any of the code sets and data sets can cause problems through out the system.
Object-Oriented programming takes a radically different approach:
Code and data are merged into one indivisible item – an object (the term "component" has also been used to describe an object.) An object is an abstraction of a set of real-world things (for example, an object may be created around "date") The object would contain all information and functionality for that thing (A dateobject it may contain labels like January, February, Tuesday, Wednesday. It may contain functionality that manages leap years, determines if it is a business day or a holiday, etc., See Fig. 1). Ideally, information about a particular thing should reside in only one place in a system. The information within an object is encapsulated (or hidden) from the rest of the system.A system is composed of multiple objects (i.e., date function, reports, order processing, etc., See Fig 2). When one object needs information from another object, a request is sent asking for specific information. (for example, a report object may need to know what today’s date is and will send a request to the date object) These requests are called messages and each object has an interface that manages messages.OO programming languages include features such as "class", "instance", "inheritance", and "polymorphism" that increase the power and flexibility of an object.
20. What is the use of friend function?
Ans.Friend functionsSometimes 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 whichnames 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;};
Friend functions allow binary operators to be defined which combine private data in a pair of objects. This is particularly powerful when using the operator overloading features of C++. We will return to it when we look at overloading.
21. What are the different types of errors in php?
ans.Three are three types of errors:
1. Notices: These are trivial, non-critical errors that PHP encounters while executing a script - for example, accessing a variable that has not yet been defined. By default, such errors are not displayed to the user at all - although, as you will see, you can change this default behaviour.
2. Warnings: These are more serious errors - for example, attempting to include() a file which does not exist. By default, these errors are displayed to the user, but they do not result in script termination.
3. Fatal errors: These are critical errors - for example, instantiating an object of a non-existent class, or calling a non-existent function. These errors cause the immediate termination of the script, and PHP's default behaviour is to display them to the user when they take place.
22. What is the functionality of the function strstr and stristr?
Ans.strstr() returns part of a given string from the first occurrence of a given substring to the end of the string. For example: strstr("user@example.com","@") will return "@example.com".stristr() is idential to strstr() except that it is case insensitive.
23. What is the functionality of the function htmlentities?
Ans: htmlentities — Convert all applicable characters to HTML entitiesThis function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.
24. How can we get second of the current time using date function?
$second = date("s");
25. What is meant by urlencode and urldocode?
Ans.urlencode() returns the URL encoded version of the given string. URL coding converts special characters into % signs followed by two hex digits. For example: urlencode("10.00%") will return "10%2E00%25?. URL encoded strings are safe to be used as part of URLs.urldecode() returns the URL decoded version of the given string.
26. What is the difference between the functions unlink and unset?
Ans.unlink() deletes the given file from the file system.unset() makes a variable undefined.
27. How can we register the variables into a session?
Ans.We can use the session_register ($ur_session_var) function.
28. How can we get the properties (size, type, width, height) of an image using php image functions?
Ans.To know the Image type use exif_imagetype () functionTo know the Image size use getimagesize () functionTo know the image width use imagesx () functionTo know the image height use imagesy() function
29. What is the maximum size of a file that can be uploaded using php and how can we change this?
Ans.You can change maximum size of a file set upload_max_filesize variable in php.ini file
30. How can we increase the execution time of a php script?
AnsSet max_execution_time variable in php.ini file to your desired time in second.
31. How can we take a backup of a mysql table and how can we restore it.?
Answer: Create a full backup of your database: shell> mysqldump –tab=/path/to/some/dir –opt db_name Or: shell> mysqlhotcopy db_name /path/to/some/dirThe full backup file is just a set of SQL statements, so restoring it is very easy:shell> mysql "."Executed";mysql_close($link2);
32. How many ways can we get the value of current session id?
Ans session_id() returns the session id for the current session.
33. How can we destroy the session, how can we unset the variable of a session?
Ans session_unregister — Unregister a global variable from the current sessionsession_unset — Free all session variables
34. How can we destroy the cookie?
Ans Set the cookie in past
35. How many ways we can pass the variable through the navigation between the pages?
Ans GET or QueryString and POST46. What is the difference between ereg_replace() and eregi_replace()?Ans:-eregi_replace() function is identical to ereg_replace() except that this ignores case distinction when matching alphabetic characters.eregi_replace() function is identical to ereg_replace() except that this ignores case distinction when matching alphabetic characters.
Subscribe to:
Posts (Atom)