PHP Interview Questions and Answers
Experienced / Expert level questions & answers
Ques 1. 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.
Ques 2. How do I find out the number of parameters passed into function?
func_num_args() function returns the number of parameters passed in.
Ques 3. If the variable $a is equal to 5 and variable $b is equal to character a, what's the value of $$b?
100, it's a reference to existing variable.
Ques 4. 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.
Ques 5. 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.
Ques 6. With a heredoc syntax, do I get variable substitution inside the heredoc contents?
Yes.
Ques 7. 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?
On large strings that need to be formatted according to some length specifications, use wordwrap() or chunk_split().
Ques 8. 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.
Ques 9. 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 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.
Most helpful rated by users:
- What does a special set of tags <?= and ?> do in PHP?
- What's the difference between include and require? -
- I am trying to assign a variable the value of 0123, but it keeps coming up with a different number, what's the problem?
- How do you define a constant?
- Would I use print "$a dollars" or "{$a} dollars" to print out the amount of dollars in this example?