Monday, 30 September 2013

PHP mathmatical assignment operators

This is another post on some basic PHP stuff which isn't so obvious to new PHP developers, even some people who've been coding for a while didn't know this just because it hasn't been brought to their attention.

What are they?

They are just short cut ways of taking a variable and performing either an addition, subtraction, multiplication or division on them with less code. I've put examples below including what they do and what the 'long hand' way of doing it would've been.

Mathematical operationLong handShort hand
Addition$var1 = $var1 + 2$var1 += 2
Subtraction$var1 = $var1 - 2$var1 -= 2
Multiplication$var1 = $var1 * 2$var1 *= 2
Division$var1 = $var1 / 2$var1 /= 2

One more

There is one more which is the modulo assignment operator. I've put this separately as it's not as obvious as the the ones above (unless you know your maths of course). This divides the first number (called first operand) by the second number (called second operand) and the result would be the remainder of the division (called modulus).

Mathematical operationLong handShort hand
ModuloN/A$var1 %= 2

Further reading

Monday, 23 September 2013

PHP empty(), what is empty?

Using the PHP empty(); function can be really helpful in development but also is one of the functions which cause a lot of errors, especially when starting out with PHP, as what PHP considers to be empty isn't as obvious as it should be.

What does empty(); consider to be empty?

Here's a list of what the PHP empty(); function considers to be empty:
  • "" - An empty string
  • 0 - The integer 0, this can catch people out.
  • 0.0 - The floating point 0.0
  • "0" - The string 0
  • false - Boolean false value
  • array() - An empty array
  • null - A null value

Common mistakes

Probably the most common one that catches people out is either the integer 0 or string 0 being considered empty, even though it looks like there's a value in there. Possibly one of the most common places developers get caught out with this is validation, if you're validating a form field with a question like "How many cars are in your household," and the user enters "0" when you validate with PHP to check that they've answered the question by using only the empty(); function on the value of that field then it'll tell you it's empty even though the user has entered a value.

How to use empty();

I thought I'd also give a quick example of how to use empty(); encase someone reading this hasn't heard of this function before. It's really simple.

$val_field = empty($form_name); // $val_field will either be true or false depending on if $form_name is considered to be empty or not

Further reading

Friday, 20 September 2013

PHP double quotes vs single quotes

This is something I've been unsure about for quite a while but as it's such a minor thing really I haven't actually looked into it or given it much thought. Generally when using quotes to output a string in PHP should you use single quotes or double quotes?

Turns out in my opinion double quotes are best used most of the time, here's why.

Double quotes

With these you can put variables directly into your string and it'll display the value of that variable without the use of any concatenation. I've noticed that generally in my PHP programming when I'm putting variables in a string to be output to the browser I want the actual value of it to be displayed, not the name of the variable itself.

Single quotes

Every thing contained within these is basically displayed as it's written, for example if you put a variable in the middle of the string (without any concatenation) this'll output the name of that variable to the browser but not the value of the variable.


I thought rather than writing to much above about them I'd have a table of examples instead as trying to explain it would require some 'jargon' and depending on how well you understand programming it could become hard to understand what's going on.

For this example $test = 'Neil'; and $test[0] = 'Nand';
PHP codeOutput
Single quote
echo '$test is so, so great';$test is so, so great
echo '$testArray[0] is so, so great';$testArray[0] is so, so great
Double quotes
echo "$test is so, so great";Neil is so, so great
echo "$testArray[0] is so, so great";Nand is so, so great

Something to note

Something some of you may have noticed in my examples table that when using double quotes if you tried to echo out the string "$tests so, so great"; then PHP would look for the variable $tests (which doesn't exist) rather than $test.

Stopping this happening is easy, when you put a variable in the string surround it with {}, to correct the example about it'd be echo "{$test}s so, so great";

What is concatenation?

I mentioned concatenation earlier on in this post, encase anyone wasn't sure what it means I've put two examples below. The first is without concatenation (which will display the variable name and not its value) and the second is the same statement written with the variable & string concatenated so it'll display the variables value.
echo '$test is so, so great";
echo $test . 'is so, so great';