PHP Interview Questions For Beginners To Intermediate Level with Answers

PHP Interview Questions For Beginners To Intermediate Level

These are some questions that i would expect candidates to know who claim that they are working in PHP for 6 Months to 1 to 2 year.

1. How do you find the PHP Version in command prompt that is running in a computer ?

Ans: php -v

Generally these kinds of question are asked to see if developers are aware of php’s command line usage.

2. What is the command used to list all the extensions loaded in PHP ?

Ans: php -m

This is one very handy utility to find all the extensions / modules that are loaded dynamically into php at runtime.

Output will be something like this ` XCache requires Zend Engine API version 220100525. The Zend Engine API version 220090626 which is installed, is outdated.

[PHP Modules] bcmath bz2 calendar Core ctype curl ….. ….. `

3. what is the output of the following program ?

<? php
$name = "John";
echo "Hello $name";
?>

Ans: General tendency of the developer is to say Hello John but there is a slight trick if you closely watch there is a space between ?> and the word php. This is not a valid <?php standard PHP Open Tag. As per PHP Coding Style Guide you can use one of the <?php or <?= ?>

4. What is the difference between print_r() and var_dump() ?

Ans: Both print_r() and var_dump() are used inspect or display all the values in variable, array. The only main difference is var_dump() displays values along with its datatype. Since PHP is a loosely typed language var_dump() becomes quite handy to find the datatype of the value of the variable / array in PHP.

5. What is the output of the following code ?

<?php
$a = 1 + "14 chickens";
echo "A : " . $a;
$b = "14 Chickens" + 1;
echo "B : " . $b;
?>
Ans: A : 5; B : 5; From PHP.net

When a string is evaluated in a numeric context, the resulting value and type are determined as follows.

If the string does not contain any of the characters ‘.’, ‘e’, or ‘E’ and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer. In all other cases it will be evaluated as a float.

The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an ‘e’ or ‘E’ followed by one or more digits.

More info

6. What is the output of the following program ?

<?php
$a = ['apple', 'orange', 'guva'];
foreach( $a as $k =&gt; $v ){
echo "Key :" . $k . "Val :" . $v;
}
?>

Ans: Key :0 Val :apple Key :1 Val :orange Key :2 Val :guva

For Array with no keys, keys would be 0,1,2,… (indices)

7. What is the output of the following code ?


<?php

$a = 'a';
echo "$a";
echo '$a';

?>
`
Ans: a$a.

The reason is variable ( $a ) inside “” are replaced with the value in PHP, and variable inside single quotes are not replaced with value. Hence echo '$a' would print as ‘$a’ whereas echo “$a” would output ‘a’ which is the value.

8. How will you declare a Constant in PHP ? Give an example ? What is magic constant in PHP ? Why its called magic constant ? Give some examples of magic constant.

Ans: define('CONSTANT_NAME',its value);

Its general practise that developers have constant names in all CAPS to distinguish from variables.

Eg: define("CONSTANT", "Hello world.");

PHP language by itself has lots of constants that stores some special values / magic values depending on the context called MAGIC CONSTANTS

eg: ****LINE**** displays Line number where it is placed here is complete list of Magic Constants

9. Write a code in PHP to find all the whitespace and replace with ‘S’.

Ans: <?php $s = "Hello This is the String";
$newstr = str_replace(' ', 'S', $s);
print_r($newstr);
?>
Manual: Str_replace

10. What does the function list() do in PHP ?

Used to assign an array to individual variables <?php
$fruits =array("orange","apple","banana");
list($v1, $v2, $v3) = $fruits;
?>

The mysterious invisible CRLF

How do we End a line and move to next line to be precise (Beginning of the Next Line) ?

when you are using your computer to type something or The Computer displays one line after another line. The Computer should have some mechanism to mark the line ending.

So Should every editor, word processor should do this ?

Yea, can be done but each and every editor will have a mechanism and worstly You can’t open the same file in different editor. It would look awkward.

Hence, It should be done at the Operating System level and so that any editor can understand the line ending the same way and you would have great time working on the files.

How do you do that?

Use a Character or a set of Character Code To mark the end of line.

Lets go little closer and see. In a typewriter there are two operations that are done.

  1. Carriage Return – in short CR – You push or return the Carriage to the beginning of the position.

  2. Paper Feed – Line Feed – You push the lever to bring the paper to next line. So that the Typewriter can type on blank space.

<

p>So MS-DOS, Windows Developers took this inspiration and implemented exactly the same way. They assigned Two Control Codes. One for Carriage Return and the other for Line Feed.

Hence MS-DOS, MS-Windows has CRLF character code for Newline / EndOfLine / LineBreak.

CR+LF is used in Windows ‘\r’ 0x0D (in decimal 13) for CR and LF ‘\n’ 0x0A (in decimal 10).

Creators of Unix are smarter enough to keep one single character code to do both the job of Carriage Return, Line Feed. LF is used in Unix.

Can I see CRLF Characters ?

Yea, Lots of text editors have options to show invisible characters. You can enable those options and you can see in Notepad++ Editor.

Enable Showing CRLF Character in Notepad++

and this is how it looks in your text file

textwithnewline