Here's a nifty one liner in PHP which let's you swap 2 variables values.
PLAIN TEXT
PHP:
list($a,$b) = array($b,$a);
Much like the PHP XOR swap there's no real reason this is any better than using a temporary variable, but you might be able to impress chicks with it or something.
/* Pete Graham */
If you want to hide certain Navigational Blocks on your Drupal sites forum(s) here’s how to do it:
1. Go to "Administer » Blocks"
2. Click "configure" on the block you want to hide.
3. On Page specific Visibility Settings click the option for "Show if the following PHP code returns TRUE"
4. Enter the following code in the [...]
The XOR swap lets you swap the value of integer variables without using a temporary variable, it's also slightly faster.
PLAIN TEXT
PHP:
$x ^= $y;
$y ^= $x;
$x ^= $y;
I can't actually think of a good reason Why you'd want to use this in PHP since memory isn't normally a big issue, however it might be good [...]
I wrote last week about how I’m trying to improve my JavaScript skills. As a small exercise I have written this function for unobtrusively selecting/deselecting checkboxes in a form.
PLAIN TEXT
JAVASCRIPT:
function selectAll(box)
{
// get the elements name
var id = box.id;
var name = /^([a-z]+)(_select_all)$/i.exec(id);
name = name[1];
re = new RegExp('^'+name+'_[0-9]+$');
// get the elements stauts
var state = box.checked;
// get the [...]
Here are 3 different ways to perform a DB select when you want to specify two or more values for a field.
1. Use th OR operator
PLAIN TEXT
SQL:
SELECT * FROM people
WHERE (first_name = 'pete'
OR first_name = 'paul');
2. Use a Regular Expression
PLAIN TEXT
SQL:
SELECT * FROM people
WHERE first_name ~'^pete|paul$';
3. Use an array
PLAIN TEXT
SQL:
SELECT * FROM people
WHERE [...]
If you need to remove elements from an array that have a particular value(s) here’s a neat way of doing it without any looping:
PLAIN TEXT
PHP:
// our initial array
$arr = Array("blue", "green", "red", "yellow", "green", "orange", "yellow", "indigo", "red");
print_r($arr);
// remove the elements who's values are yellow or red
$arr = array_diff($arr, array("yellow", "red"));
print_r($arr);
// optionally you could reindex [...]
There’s an Urban Myth that Bill Gates is so rich that if he was to drop a $1000 bill on the ground, it's just not worth his time to bend over and pick it up. Now it is true that he wouldn’t pick it up, however the real reason for this is he was wedgied [...]
I started work in web development in 2004 at this point in time JavaScript was hugely unpopular in terms of web trends, this is probably because it was mostly used for annoying pop-ups, animations and whole list of other horrendous Internet crimes. I can recall having a King Herod style culling of all JavaScript from [...]
People seem to love my lists. Here is one of my favourite electrical connections:
Scart Socket
USB Port
3.5mm Stereo jack
Double phono
2.5mm Stereo jack
Parallel port
Ethernet
PS/2
/* Pete Graham */
I used to think that it would be a dragons-den-tastic idea to manufacture computer cases with extension plugs built into the back of the case, this would mean you wouldn’t need to buy a separate extension plug (for your printer, scanner, modem, router, etc) when you buy a new PC. Although I still think this [...]