Você está na página 1de 11

PHP INTERVIEW QUESTIONS

1. What does a special set of tags <? = and?> do in PHP? The output is displayed directly to the browser. 2. What the difference is between include and require? Its 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. 3. I am trying to assign a variable the value of 0123, but it keeps coming up with a different number, whats the problem? PHP Interpreter treats numbers beginning with 0 as octal. 4. How do you pass a variable by value? Just like in C++, put an ampersand in front of it, like $a = &$b 5. Will comparison of string "10" and integer 11 works in PHP? Yes, internally PHP will cast everything to the integer type, so numbers 10 and 11 will be compared. 6. When are you supposed to use endif to end the conditional statement? When the original if was followed by: and then the code block without braces. 7. Explain the ternary conditional operator in PHP? Expression preceding the ? is evaluated, if its true, then the expression preceding the : is executed, otherwise, the expression following : is executed. 8. How do I find out the number of parameters passed into function? func_num_args() function returns the number of parameters passed in.

9. Whats the difference between accessing a class method via -> and via ::? :: is allowed to access methods that can perform static operations, i.e. those, which do not require object initialization. 10. Are objects passed by value or by reference? Everything is passed by value. 11. How do you call a constructor for a parent class? parent:: constructor ($value) 12. Whats the special meaning of __sleep and __wakeup? __sleep returns the array of all the variables than need to be saved, while __wakeup retrieves them. 13. Why doesnt the following code print the newline properly? <?php $str = Hello, there.nHow are you?nThanks for visiting TechInterviews; print $str; ?> Because inside the single quotes the n character is not interpreted as newline, just as a sequence of two characters - and n. 14. Would you initialize your strings with single quotes or double quotes? Since the data inside the single-quoted string is not parsed for variable substitution, its always a better idea speed-wise to initialize a string with single quotes, unless you specifically need variable substitution. 15. What is the difference between characters 23 and x23? The first one is octal 23, the second is hex 23.

16. I want to combine two variables together: $var1 = 'Welcome to '; $var2 = 'TechInterviews.com'; What will work faster? Code sample 1: $var 3 = $var1.$var2;

Or code sample 2: $var3 = "$var1$var2"; Both examples would provide the same result - $var3 equal to "Welcome to TechInterviews.com". However, Code Sample 1 will work significantly faster. Try it out with large sets of data (or via concatenating small sets a million times or so), and you will see that concatenation works significantly faster than variable substitution.

17. For printing out strings, there are echo, print and printf. Explain the differences. echo is the most primitive of them, and just outputs the contents following the construct to the screen. print is also a construct (so parentheses are optional when calling it), but it returns TRUE on successful output and FALSE if it was unable to print out the string. However, you can pass multiple parameters to echo, like: <?php echo 'Welcome ', 'to', ' ', 'TechInterviews!'; ?> and it will output the string "Welcome to TechInterviews!" print does not take multiple parameters. It is also generally argued that echo is faster, but usually the speed advantage is negligible, and might not be there for future versions of PHP. printf is a function, not a construct, and allows such advantages as formatted output, but its the slowest way to print out data out of echo, print and printf.

18. Whats the output of the ucwords function in this example? $formatted = ucwords("TECHINTERVIEWS IS COLLECTION OF QUESTIONS"); print $formatted; What will be printed is TECHINTERVIEWS IS COLLECTION OF QUESTIONS. ucwords() makes every first letter of every word capital, but it does not lower-case anything else. To avoid this, and get a properly formatted string, its worth using strtolower() first. 19. Whats the difference between htmlentities() and htmlspecialchars()? htmlspecialchars only takes care of <, >, single quote , double quote " and ampersand. htmlentities translates all occurrences of character sequences that have different meaning in HTML. 20. Whats the difference between md5(), crc32() and sha1() crypto on PHP? The major difference is the length of the hash generated. CRC32 is, evidently, 32 bits, while sha1() returns a 128 bit value, and md5() returns a 160 bit value. This is important when avoiding collisions. 21. So if md5() generates the most secure hash, why would you ever use the less secure crc32() and sha1()? Crypto usage in PHP is simple, but that doesnt mean its free. First off, depending on the data that youre encrypting, you might have reasons to store a 32-bit value in the database instead of the 160-bit value to save on space. Second, the more secure the crypto is, the longer is the computation time to deliver the hash value. A high volume site might be significantly slowed down, if frequent md5() generation is required. 22. How can we submit from without a submit button? 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.
4

23. What is the difference between mysql_fetch_object and mysql_fetch_array?

MySQL fetch object will collect first single matching record where mysql_fetch_array will collect all matching records from the table in an array. 24. How can we extract string 'abc.com ' from a string'http://info@a...' using regular _expression of php?

We can use the preg_match() function with "/.*@(.*)$/" asthe regular expression pattern. For example:preg_match("/.*@(.*)$/","http://info@abc.com",$data);echo $data[1];

25. How can we create a database using php and mysql? PHP: mysql_create_db()Mysql: create database;

26. How can I execute a php script using command line? Just run the PHP CLI (Command Line Interface) program and provide the PHPscript 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 notexecute properly in command line environment.

27. What are the different types of errors in php?

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 behavior. 2. Warnings: These are more serious errors - for example, attempting to include() afile 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 anonexistent class, or calling a non-existent function. These errors cause the immediate termination of the script, and PHP's default behavior is to display them to the user when they take place. 28. Who is the father of PHP and explain the changes in PHP versions?

Rasmus Lerdorf is known as the father of PHP.PHP/FI 2.0 is an early and no longer supported version of PHP. PHP 3 is the successor to PHP/FI 2.0 and is a lot nicer. PHP 4 is the current generation of PHP, which uses the Zend engine under thehood. PHP 5 uses Zend engine 2 which, among other things, offers many additional OOP features 29. In how many ways we can retrieve the data in the result set of MySQL using PHP? You can do it by 4 Ways 1. mysql_fetch_row. 2. mysql_fetch_array 3. mysql_fetch_object 4. mysql_fetch_assoc 30. What are the features and advantages of object-oriented programming? 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
6

costs. OO P 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

31. What is the use of friend function? 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.

32. What are the differences between public, private, protected, static, transient, final and volatile? Public : Public declared items can be accessed everywhere. Protected: Protected limits access to inherited and parent classes (and to the class that defines the item). Private: Private limits visibility only to the class that defines the item. Static: A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope. Final: Final keyword prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.
7

transient : A transient variable is a variable that may not be serialized. volatile : a variable that might be concurrently modified by multiple threads should be declared volatile. Variables declared to be volatile will not be optimized by the compiler because their value can change at any time.

33. Draw the architecture of Zend engine? The Zend Engine is the internal compiler and runtime engine used by PHP4. Developed by Zeev Suraski and Andi Gutmans, the Zend Engine is an abbreviation of their names. In the early days of PHP4, it worked as follows:

The PHP script was loaded by the Zend Engine and compiled into Zend. opcode. Opcodes, short for operation codes, are low level binary instructions. Then the opcode was executed and the HTML generated sent to the client. The opcode was flushed from memory after execution.Today, there are a multitude of products and techniques to help you speed up this process.

34. How can we register the variables into a session? $_SESSION[name] = SessionName;

35. How can we get the properties (size, type, width, height) of an image using PHP image functions? To know the Image type use exif_imagetype () function To know the Image size use getimagesize () function To know the image width use imagesx () function To know the image height use imagesy() function 36. How can we get the browser properties using PHP? By using $_SERVER['HTTP_USER_AGENT'] variable. 37. What is the maximum size of a file that can be uploaded using PHP and how can we change this? By default the maximum size is 2MB. And we can change the following setup at php.iniupload_max_filesize = 2M 38. How can we increase the execution time of a PHP script? By changing the following setup at php.inimax_execution_time = 30 ; Maximum execution time of each script, in seconds

39. How many ways can we get the value of current session id? session_id() returns the session id for the current session.

40. How can we destroy the session, how can we unset the variable of a session? session_unregister session_unset Unregister a global variable from the current session Free all session variables

41. How many ways we can pass the variable through the navigation between the pages? GET/QueryString and POST 42. How can we know the count/number of elements of an array? 2 ways a) sizeof($urarray) This function is an alias of count() b) count($urarray)

43. 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. 44. What type of inheritance that PHP supports? In PHP an extended class is always dependent on a single base class, that is, multiple inheritance is not supported. Classes are extended using the keyword extends. 45. What is the difference between Primary Key and Unique key? Primary Key: A column in a table whose values uniquely identify the rows in the table. A primary key value cannot be NULL. Unique Key: Unique Keys are used to uniquely identify each row in the table. There can be one and only one row for each unique key value. So NULL can be a unique key.There can be only one primary key for a table but there can be more than one unique for a table.

46. What is CAPTCHA?

CAPTCHA stands for Completely Automated Public Turing Test to tell Computers and Humans Apart. To prevent spammers from using bots to automatically fill out forms, CAPTCHA programmers will generate an image containing distorted images of a string of numbers and letters. Computers cannot determine what the numbers and letters are from the image but humans have great pattern recognition abilities and will be able to fairly accurately determine the string of numbers and letters. By entering the numbers and letters from the image in the validation field, the application can be fairly assured that there is a human client using it.

10

47. What is difference between require_once(), require(), include(). Because above three function usely use to call a file in another file? Difference between require() and require_once(): require() includes and evaluates a specific file, while require_once() does that only if it has not been included before (on the same page). So, require_once() is recommended to use when you want to include a file where you have a lot of functions for example. This way you make sure you don't include the file more times and you will not get the "function re-declared" error.

Difference between require () and include () is that require () produces a FATAL ERROR if the file you want to include is not found, while include () only produces a WARNING. There is also include_once() which is the same as include(), but the difference between them is the same as the difference between require() and require_once(). 48. How i can get ip address? We can use SERVER var $_SERVER['SERVER_ADDR'] and getenv("REMOTE_ADDR") functions to get the IP address.

49. How will you create a database using PHP and MySQL? By using : mysql_create_db("Database Name")

50. Explain "GET" and "POST" methods. Both the methods are used to send data to the server. GET method - the browser appends the data onto the URL. Post method - the data is sent as standard input.

11

Você também pode gostar