PHP Questions

PHP questions suitable for use in interviews or just for fun.

PHP Question: Variable Reference

Question

What does the following code print out?

function arrayPrint($array)
{
   echo implode(' ', $array);
}

$arrayA = [1, 2, 3];
$arrayB = $arrayA;
$arrayB[1] = 0;
arrayPrint($arrayA);

PHP Question: Post Variables

Question Take the following HTML form.

PHP Question: While And Do While Looping

Question

What does the $count variable equal after each of these loops?

PHP Question: Defining Constants In PHP5

Question

What does the following code do?

define("MY_CONSTANT", array(1,2,3,4,5));

PHP Question: Class Methods

Question

What is the difference between these two lines of code and can you produce the background code used for them?

PHP Question: PHP Script Shape

Question

Write a PHP script that will print out the following text in the correct diamond shape.

    *
   ***
  *****
 *******
*********
*********
 *******
  *****
   ***
    *

PHP Question: Print Object

Question

The following code was executed.

<?php
class MyClass {
  private $foo = "bar";
}

$myObject = new MyClass();
echo $myObject;

Which produced the following error.

Catchable fatal error: Object of class MyClass could not be converted to string in test.php on line 7

Call Stack:
    0.0011     323736   1. {main}() /test.php:0

How can the code be simply changed to prevent this error and produce some form of result?

PHP Question: Object Property Increment

Question

What does the following snippet print and why?

PHP Question: Form Variables

Question

Given the following form:

<form action="index.php" method="post">
<input name="text" type="input" value="" />
<input type="submit" value="Submit" /> 
</form>

How would you get hold of the value of the input box after the form is submitted?

PHP Question: Pass By Reference

Question

Consider the following:

function doSomething(&$val) {
    $val++;
}

$a = 1;
doSomething($a);

What does the variable $a now equal?