quiz yourself on PHP

May 17, 2011

PHP

1.) The eregi() function is deprecated in PHP 5.3+
A. TRUE
B. FALSE

2.) Which of the following will be most likely to work as intended:
A. mail($headers, $body, $subject, $to);
B. if ($i = $item)
C. foreach ($i=0;$i<$numrows;++$i) D. for ($_POST as $k => $v)
E. none of the above

3.) Which of the following is a valid php statement
A. print ("<p> Hello World </p>");
B. echo <p> Hello World </p>;
C. print_html "<p> Hello World </p>";
D. print_r "<p> Hello World </p>";
E. html "<p> Hello World </p>";

4.) The return value type for chr() function is _________.
A. bool
B. int
C. string
D. void (it does not return any values)
E. char

5.) Which of the following is the short description for the rtrim() function?
A. void rtrim ( string $str)
B. string rtrim ( string $str [, string $charlist] )
C. int rtrim ( string $str [, string $charlist] )
D. string rtrim ( string $str )
E. int rtrim ( string $str)

6.) REGISTER_GLOBALS will not be supported in PHP 6.
A. TRUE
B. FALSE

7.) If mysql_connect fails to connect to the mysql server,
an error will be echoed to the user agent. You can prepend the function call with
___, in order to suppress this message.
A. @
B. &
C. #
D. %
E. none of the above

Sub-Topic String Functions
8.) The following line of code will return ______________________.
code: str_word_count ( string $string [, int $format [, string $charlist]] )
select ALL that apply:
A. the number of words in $string, if the optional parameter $format is used and set to 1
B. an array, if the optional parameter $format is used and set to 0
C. an array if the optional parameter $format is used and set to 1
D. the number of words in $string, if the optional parameter $format is used and set to 0
E. the number of words in $string, if the optional parameter $format is not used

9.)
which syntax is correct:
A. header(‘location’, ‘index.php’);
B. header(‘location: index.php’);
C. header(location, ‘index.php’);
D. location(header, ‘index.php’);
E. location(“header: index.php”);

Sub-Topic Mysql Functions
10.) mysql_connect() will return _____________________________________.
select ALL that apply:
A. an error code (string) if the it fails to connect to the mysql server
B. an error code (integer) if it fails to connect to the mysql server
C. FALSE, if it fails to connect to the mysql server
D. a resource identifier, if a persistent connection is established
E. a resource identifier, if a connection is established

Sub-Topic Mysql Functions
11.) mysql_real_escape_string() will return an “escaped” string only if
it has access to the character set used by the mysql server.
A. TRUE
B. FALSE

12.) mysql_query() will return ___________________________________________.
A. an error code (string) if the query could not be completed
B. TRUE, if a SELECT query ended successfully
C. FALSE, if the query ended with an error
D. an array if a SHOW query ended successfully
E. TRUE, if a DESCRIBE query ended succesfully

13.) The $_POST[] superglobal is initialized ONLY when the user agent is
making a request using the _________ method.
A. POST
B. TRACE
C. DELETE
D. HEAD
E. OPTIONS

14.) mysql_real_escape_string() will return an “escaped” string only if
it has access to the character set used by the mysql server.
A. TRUE
B. FALSE

15.) In order to get the length of a string, you can use _________.
select ALL that apply:
A. strlen()
B. str_len()
C. mb_strlen()
D. mb_str_len
E. mbstrlen

the official answer is A, B, C, and D ??? but, “I doubt it”; test it.

16.) What is the only type of data that cannot be handled by the serialize()
function.
A. BOOL
B. String
C. Array
D. Object
E. Resource

17. Which one of these variables has an illegal name?
A. $my-Var
B. $myVar
C. $my_Var
D. $myvar01
E.

18. What is the correct way to open the file “time.txt” as readable?
A. open(“time.txt”);
B. open(“time.txt”,”read”);
C. fopen(“time.txt”,”r”);
D. fopen(“time.txt”,”r+”);

19. What is the correct way to add 1 to the $count variable?
A. ++count
B. count++
C. $count =+1
D. $count++;

20. What is the correct way to connect to a MySQL database?
A. mysql_open(“localhost”);
B. mysql_connect(“localhost”);
C. connect_mysql(“localhost”);
D. dbopen(“localhost”);

21. What is the correct way to include the file “time.inc” ?
<% include file="time.inc" %>
<?php include_file(“time.inc”); ?>
<?php require(“time.inc”); ?>
<!–include file=”time.inc”–>


1.) A. TRUE

2.) E. none of the above
mail($to,$subject,$body,$headers)
if ($i == $item)
for($i=0; $i<$numrows; ++$i) foreach($_POST as $k => $v)

3.) A. print (“<p> Hello World </p>”); *

4.) C. string

5.) B. string rtrim ( string $str [, string $charlist] )

6.) A. TRUE

7.) A. @

8.) ALL that apply:
C. an array if the optional parameter $format is used and set to 1
D. the number of words in $string, if the optional parameter $format is used and set to 0
E. the number of words in $string, if the optional parameter $format is not used

9.) B. header(‘location: index.php’);

10.) the 2 correct options:
C. FALSE, if it fails to connect to the mysql server
d. a resource identifier, if a persistent connection is established ??
E. a resource identifier, if a connection is established

11.) A. TRUE

12.) C. FALSE, if the query ended with an error

13.) A. POST

14.) A. TRUE

15.)
the PHP functions are:
A. strlen() *
B. str_len()
C. mb_strlen() *
D. mb_str_len
the official answer is A, B, C, and D ??? but, “I doubt it”; test it.

16.) E. Resource

17. A. $my-Var

18. C. fopen(“time.txt”,”r”);
‘r+’ = Open for reading and writing;

19. D. $count++;

20. B. mysql_connect(“localhost”);

21. <?php require(“time.inc”); ?>


Are you looking to update your PHP programming knowledge or need to prepare for a job interview? Check out this collection of PHP Interview Questions and Answers.

1. What’s the difference between include and require?

Answer: It’s 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.

2. When you assign a variable the value of 0123, it keeps coming up with a different number, why?

Answer: PHP Interpreter treats numbers beginning with 0 as octal.

3. Would you use print “$a dollars” or “{$a} dollars” to print out the amount of dollars in this example?

Answer: In this example it wouldn’t matter, since the variable is all by itself, but if you were to print something like “{$a},000,000 mln dollars”, then you definitely need to use the braces.

4. How do you define a constant?

Answer: Constants in PHP are defined using define() directive, like define(“MYCONSTANT”, 100);

5. How do you pass a variable by value reference in PHP?

Answer: Just like in C++, put an ampersand in front of it, like $a = &$b;

Note: When passing a value to a function, PHP must copy the value. Particularly for large strings and objects, this can be an expensive operation, and unless the variable is declared global, outside the function, it remains untouched. Passing by reference removes the need to copy the value, however if you change it now, it will be changed within the function and it does not need to be “returned”. The deed is done.
Also, a parameter that is declared as being passed by reference can only be a variable.
function pass_by_value($param) {
function pass_by_reference(&$param) {

6. Will comparison of string “10” and integer 11 work in PHP?

Answer: Yes, internally PHP will cast everything to the integer type, so numbers 10 and 11 will be compared.

7. When are you supposed to use endif to end the conditional statement?

Answer: When the original if was followed by : and then the code block without braces.

8. Explain the ternary conditional operator in PHP?

Answer: Expression preceding the ? is evaluated, if it’s true, then the expression preceding the : is executed, otherwise, the expression following : is executed.

9. How do I find out the number of parameters passed into function in PHP?

Answer: func_num_args() function returns the number of parameters/arguments passed to a function in PHP.

10. If the variable $a is equal to 55 and variable $b is equal to character a, what’s the value of $$b?

Answer: 55, it’s a reference to existing variable.

11. What’s the difference between accessing a class method via -> and via ::?

Answer: In PHP, :: is allowed to access methods that can perform static operations, i.e. those, which do not require object initialization.

12. Are objects passed by value or by reference?

Answer: Everything is passed by value. In PHP4 it's true, BUT in PHP5 absolutely not – all objects are passed by reference.

13. How do you call a constructor for a parent class?

Answer: parent::constructor($value)

14. What’s the special meaning of __sleep and __wakeup?

Answer: __sleep returns the array of all the variables than need to be saved, while __wakeup retrieves them.

15. Would you initialize your strings with single quotes or double quotes in PHP?

Answer: Since the data inside the single-quoted string is not parsed for variable substitution, it’s always a better idea speed-wise to initialize a string with single quotes, unless you specifically need variable substitution.

16. I am writing an application in PHP that outputs a printable version of driving directions. It contains some long sentences, and I am a neat freak, and would like to make sure that no line exceeds 50 characters. How do I accomplish that with PHP?

Answer: On large strings that need to be formatted according to some length specifications, use wordwrap() or chunk_split().

17. What’s the output of the ucwords() function in the example below?

$formatted = ucwords(“THIS COLLECTION OF PHP INTERVIEW QUESTIONS”);
print $formatted;

Answer: Output will be THIS IS COLLECTION OF PHP INTERVIEW 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, it’s worth using strtolower() first.

18. What’s the difference between htmlentities() and htmlspecialchars()?

Answer: htmlspecialchars only takes care of <, >, single quote ‘, double quote ” and ampersand. htmlentities translates all occurrences of character sequences that have different meaning in HTML.

19. What’s the difference between md5(), crc32() and sha1() crypto on PHP?

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

20. If md5() generates the most secure hash, why would you ever use the less secure crc32() and sha1()?

Answer: Crypto usage in PHP is simple, but that doesn’t mean it’s free. First off, depending on the data that you’re 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.

One Response to quiz yourself on PHP

  1. seal on May 16, 2011 at 9:27 pm

    Regarding 5. How do you pass a variable by value in PHP?

    mmm the question does not correspond with the answer.
    assign by reference $a= &$b;
    assign by value $a = $b;

    Probably a typo error


    probably, and it is getting copied around the internet:
    http://odesk-solutions-test-answers.blogspot.com/2010/11/php-interview-questions-and-answers.html
    http://php.admarkr.com/hypertext-preprocessor/php-interview-questions/

    function pass_by_value($param) {
    function pass_by_reference(&$param) {

Leave a Reply

We try to post all comments within 1 business day