PHP

Posts about the server side scripting language PHP

PHP Question: Pass By Reference

Question

Consider the following:

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

$a = 1;
doSomething($a);

What does the variable $a now equal?

PHP Question: Named Variables

Question

What does the following code snipped print?

$a = 5;
$b = 'a';
print $$b;

Number Pair Additions In PHP

I was helping my son out with his maths homework today and we came across a question that asked him to find some combinations of two numbers that added up to a given number (he is only 6 so this was good maths practice). This got me to thinking about how to calculate all possible ways in which two numbers could be added together to make another number.

The only rule had to be in there was that doing things like 1+11=12 and 11+1=12 are basically the same thing and so would be cheating. In which case I realised that it would only be necessary to use half of the numbers when creating the list, so for the number 12 every combination can be found using the numbers 0-6 because after this we start duplicating the sums, just swapping the order.

Here is the function I came up with.

PHP Question: Octal Values

Question

If I assign and print a value like this:

$a = 012;
print $a;

I get back a value of '10', why?

PHP Question: Include And Require Files

Question

What's the difference between include() and require()?

PHP Question: Increment By One

Question

What will the following snippet print, and why?

$count = 0;
print $count++;

PHP Question: Printing A Boolean Value

Question

What will the following code print, and why?

echo TRUE;

PHP Question: Scope Resolution

Question

What is printed after the following code has been run?

function calc() {
    $a = 1000;
    return $a;
}

$a = 1;
calc();
print $a;

 

PHP Questions

As part of a new series of posts I will be creating a set of PHP questions suitable for use in interviews or just for fun. They will range from the very easy to very difficult but are all designed to test one or two specific parts of the PHP language. The idea is not to test for knowledge of the order of PHP function parameters (which I often have to look up), but more to test if the candidate knows how the language works. It is also quite a good mechanism to post about some of the fundamentals of the PHP language.

Revisiting filter_var() and FILTER_VALIDATE_URL

Quite a while ago I looked at using the filter_var() function to validate URL's using the FILTER_VALIDATE_URL flag and someone pointed out recently that this function has not only changed since the initial release, but that a number of flags can be added to change the way that this function works. Here are the flags available.