Accessibility and Dynamic Variable names

First a couple of PHP tips:

You don’t have to use curly braces (for example in an “if” statement) if they only contain one statement. For example:

if ($suger == 'true'){
    $cup += $twosugars;
} else {
    return $cup;
}

could be written more concisely as:

if ($suger == 'true')
    $cup += $twosugars;
else
    return $cup;

This looks neater and can make things more readable. Be careful though. If you then add another statement and forget to add the braces then you’ll run into problems. For example:

if($sugar == 'true')
    echo 'Heres your sugar';
    $cup += $twosugars;
else
    echo 'No sugar for you.';

In this example the user will always have sugar in their tea, which is fine by me but some people don’t like having holes in their teeth. Hopefully you get the idea because I can’t be bothered to explain it more. This guy explains it quite well.

Next up: Dynamicly named variables

Straight to the code:

$rofl = 'lol';
${$rofl} = 'wtf';
echo $lol;

This code would output: “wtf” – possibly an appropriate response. Here’s why:
The contents of the curly braces are evaluated before the assignment is performed (“=” is the assignment operator, remember?) which means that line effectively reads “$lol = ‘wtf'”. Of course in the above example it’s a pointless way of doing things but there are times when it could be useful. Say if you want to make a load of variables at runtime ($var1, $var2 etc.) but you’re scared of arrays. Or if you want to make your code that leaves anyone that looks at it completely bewildered. Who knows, maybe even something sensible. This guy seems to be getting quite excited about it.

Lastly: Accessiblity

So you’ve made a brilliant website and then you realise that it only works in Firefox and fi you turn off css or javascript it devolves into a jumble of unusable overlapping images and text. It’s time to read up on accessiblity. Here’s a checklist to make sure you’ve done everything you need to.

Advertisements

About Mr Chimp

I make music, draw pictures, browse the internet, programme, and make sweet, sweet cups of tea until the early hours.
This entry was posted in Programming and tagged , , , , , , . Bookmark the permalink.

One Response to Accessibility and Dynamic Variable names

  1. Pingback: Php use variable content as variable name - BlogoSfera

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s