PHP Reviewer

7

Click here to load reader

Transcript of PHP Reviewer

Page 1: PHP Reviewer

PHP Reviewer

1. Variable names that are not valid

Invalid variable names are those that does not begin with a letter or underscore;

contains a space and contains a nonalphanumeric numeral character such as (-)

2. Code fragment output $num = 33; (boolean) $num; echo $num;

Prints the integer 33. Cast to Boolean does not alter the value.

3. echo gettype(“4”);

string

4. Code fragment output $test_val = 5.5466; settype($test_val, “integer”); echo

$test_val;

5. The float data type was converted to integer in settype() function.

5. If statement that print “Youth message” to the browser if an integer variable $age

is between 18-35? If age contains any other variable, print the “Generic message”.

$age = 22;

If (($age >18) && ($age < 35) ) { echo “Youth message”} else echo {“Generic

message”};

6. Casting operator introduced in PHP 6 is ?

(int64)

7. Trace the odd data type?

integer

8. Valid float values

4.5678, 4.0, 7e4

Page 2: PHP Reviewer

Page 2 of 7

9. <?php

$somerar=15;

function addit() {

$somerar;

$somerar++ ;

echo "somerar is". $somerar;

}

addit();

?>

Somerar is 16

10. What does a special set of tags <?= and ?> do in PHP?

The output is displayed directly to the browser.

11. How do you pass a variable by value?

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

12. What's the difference between include and require?

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.

13. I am trying to assign a variable the value of 0123, but it keeps coming up with a

different number, what's the problem?

PHP Interpreter treats numbers beginning with 0 as octal.

14. What is Octal Notation?

Octal notation - identified by its leading zero and used mainly to express UNIX-style

access permissions.

15. Would I use print "$a dollars" or "{$a} dollars" to print out the amount of dollars in

this example?

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.

Page 3: PHP Reviewer

Page 3 of 7

16. How do you define a constant?

Via define() directive, like define ("MYCONSTANT", 100);

17. Will comparison of string "10" and integer 11 work in PHP?

Yes, internally PHP will cast everything to the integer type, so numbers 10 and 11 will

be compared.

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

19. Explain the ternary conditional operator in PHP?

Expression preceding the ? is evaluated, if it's true, then the expression preceding

the : is executed, otherwise, the expression following : is executed.

E.g $id = isset($_GET['id']) ? $_GET['id'] : false;

20. How do I find out the number of parameters passed into function?

func_num_args() function returns the number of parameters passed in. This function

may be used in conjunction with func_get_arg() and func_get_args() to allow user-

defined functions to accept variable-length argument lists.

21. Are objects passed by value or by reference?

Everything is passed by value.

22. If the variable $a is equal to ’hello’, what's the value of $$a = ’world’?

A variable variable takes the value of a variable and treats that as the name of a

variable. At this point two variables have been defined and stored in the PHP symbol

tree: $a with contents "hello" and $hello with contents "world". Therefore, this

statement:

echo "$a ${$a}"; produces the same result as echo "$a $hello";

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

parent::constructor($value)

24. What's the special meaning of _ _sleep and _ _wakeup?

__sleep function returns an array with the names of all variables of that object that

should be serialized. Its use is to commit pending data or perform similar cleanup

Page 4: PHP Reviewer

Page 4 of 7

tasks. Also, the function is useful if you have very large objects which need not be

saved completely.

while __wakeup is to reestablish any database connections that may have been lost

during serialization and perform other re-initialization tasks.

25. What’s the difference between serialize and unserialize?

serialize() checks if your class has a function with the magic name __sleep.

Conversely, unserialize() checks for the presence of a function with the magic name

__wakeup. If present, this function can reconstruct any resources that object may

have.

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

it's always a better idea speed-wise to initialize a string with single quotes, unless you

specifically need variable substitution.

27. How come the code <?php print "Contents: $arr[1]"; ?> works, but <?php print

"Contents: $arr[1][2]"; ?> doesn't for two-dimensional array of mine?

Any time you have an array with more than one dimension, complex parsing syntax is

required. print "Contents: {$arr[1][2]}" would've worked.

28. What's 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.

29. With a heredoc syntax or “<<<”, do I get variable substitution inside the heredoc

contents?

Yes. <<< or heredoc syntax, delimits the strings. After this operator, an identifier is

provided, then a newline. The string itself follows, and then the same identifier again

to close the quotation. The closing identifier must begin in the first column of the line.

Also, the identifier must follow the same naming rules as any other label in PHP: it

must contain only alphanumeric characters and underscores, and must start with a

non-digit character or underscore.

Page 5: PHP Reviewer

Page 5 of 7

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

31. 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 it's the

slowest way to print out data out of echo, print and printf.

32. Writing an application in PHP that outputs a printable version of directions. If it

contains some long sentences, what functions must be used to make sure that no

line exceeds 50 characters?

On large strings that need to be formatted according to some length specifications,

use wordwrap() <?php wordwrap($text, 8, "\n", true); ?> or chunk_split() <?php

chunk_split_unicode($str, 4); ?>.

33. What's 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.

Page 6: PHP Reviewer

Page 6 of 7

34. What's 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.

35. What is E_WARNING PHP Error Types?

E_WARNING: Run-time warning that does not cause script termination.

36. What are the different types of Errors in PHP?

Notices: These are non-critical errors which PHP encounters while running a script.

For example, a variable accessibility before it is declared.

Warnings: The are more serious errors. For example, using include()without the

existence of the file.

Fatal Errors: These errors are critical errors. For example, creating an object of a

non-existent class. These errors terminate the script’s execution immediately. These

are intimated to the users.

37. When you need to obtain the ASCII value of a character which of the following

function you apply in PHP?

ord(); Return ASCII value of character. This function complements chr().

38. session_destroy() takes session_id as one of its arguments.

False. session_destroy destroys all data registered to a session. In order to kill the

session altogether, like to log the user out, the session id must also be unset

39. The register_globals directive is disabled by default in PHP versions 4.2.0 and

greater.

True. went from ON to OFF in PHP » 4.2.0.

40. What is the out put?

<?php $x=array("aaa","ttt","www","ttt","yyy","tttt"); $y=array_count_values($x); echo $y[ttt]; ?>

Answer is 2.

Page 7: PHP Reviewer

Page 7 of 7

41. What is the out put?

<?php

$x=array(1,3,2,3,7,8,9,7,3);

$y=array_count_values($x);

echo $y[8];

?>

Answer is 1

42. Are there regular expressions in PHP?

Yes - PHP supports two different types of regular expressions: POSIX-extended and

Perl-Compatible Regular Expressions (PCRE).

43. In PHP, which of the following function is used to insert content of one php file into

another php file before server executes it

include()