PHP

Beginning Zend Framework by Armando Padilla

Beginning Zend Framework

I was lucky enough to pick up a couple of free books from the recent PHP Unconference Europe, one of which was Beginning Zend Framework by Armando Padilla. Having not looked into Zend Framework for a while I thought I would read the book to refresh my knowledge catch up and post a review.

The premise of the book was to create a sample application to keep track of music artist information, with each chapter building on the code from the previous. The first few chapters are about installing Apache, PHP and MySQL and some UML diagrams of the application that will be built. After reading this I was actually enthusiastic about the application and couldn't wait to get started.

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?

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?

Automatically Copying The Node Title To The Menu Title In Drupal 6

One new feature of Drupal 7 is that any title you give a node will be copied to the menu title field when you create a menu item. I wanted to replicate this functionality in Drupal 6 and so I created a function that did just that. I have used this function a few times in different projects so I'm posting it here.

This code uses the drupal_add_js() function to push a small amount of JavaScript code into the page when a node is added or edited. The hook hook_init() is used to add this content as it is run very early on in the Drupal boot cycle. I have tried putting this code into other hooks (like hook_preprocess_page()) but it isn't included, I would look into this further but this solution seems to work well.

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;