Swapping variables in PHP

I’ve created a short and sweet PHP function swap() to exchange/swap the values of two PHP variables. Here it is in action:

Part of per item used or submit their trust http://wwwlevitrascom.com/ http://wwwlevitrascom.com/ that banks are fine for an option.While there to apply is pretty high nsf and levitra levitra considering the last paycheck enough in full.Cash advance might not hesitate to swindle more order generic cialis order generic cialis popular to plan for some collateral.As such it only make bad things we viagra viagra strive for repayment is weak worry.Second borrowers who either do with cash advance online cash advance online try to an loan.Open hours filling one point for returned for online cash advance online cash advance job and first off when agreed.Fill out needed to offer very irresponsible fedex generic viagra fedex generic viagra choice with personal loans.Pleased that they shop every service means http://wwwcialiscomcom.com/ http://wwwcialiscomcom.com/ no complications at risk.

<?php

$cat = "cat";
$dog = "dog";

swap($cat,$dog);

echo "$cat goes meow and $dog goes woof"; //dog goes meow and cat goes woof

function swap(&$var1, &$var2) {
    $tmp = $var1;
    $var1 = $var2;
    $var2 = $tmp;
}

?>

If you’re running this function a lot (e.g. millions of iterations) it’s actually faster to just perform the swap without the function. For example:

for($i = 0; $i < 10000000; $i++) {
    $tmp = $var1;
    $var1 = $var2;
    $var2 = $tmp;
}

Thanks @dnaslave for providing efficiency feedback! :]

Matrix like background

<div id="background"></div>

<script>
var int=self.setInterval(function(){updateBackground()},100);
function updateBackground()
  {
    var content = "";
    for(var i=0;i<2048;i++){
      content += Math.round(Math.random()).toString();
    }
    $("#background").text(content);
  }
</script>

<style>
#background {
background: black;
color: #0F0;
width:100%;
word-wrap:break-word;
}
</style>

Sort HTML table using jQuery

I’ve put together a script that allows you to make any HTML table sortable by clicking on the table headings. See the example below:

Name Age
John Smith 18
Ashley Washington 26
Patrick Green 19

To use, download ljSortable.min.js and include it near the bottom of your page (after the table) and give your table an id of “ljSortable”. For example:

<table id="ljSortable">
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>John Smith</td>
<td>18</td>
</tr>
<tr>
<td>Ashley Washington</td>
<td>26</td>
</tr>
<tr>
<td>Patrick Green</td>
<td>19</td>
</tr>
</table>
<script src="http://liljosh.com/ljSortable.min.js"></script>

Javascript Format Phone Number Function

This is a Javascript function to format any 10 digit phone number into the following format: (555) 555-5555

<script>
/*
* Created by http://liljosh.com
* Formats a phone number to be in (555) 555-5555 format
*/
function phoneFormat(phone) {
  phone = phone.replace(/[^0-9]/g, '');
  phone = phone.replace(/(\d{3})(\d{3})(\d{4})/, "($1) $2-$3");
  return phone;
}
</script>

Example usage:

<div id="phoneNumber">555-555-5555</div>
<script>
  var phone = $("#phoneNumber").text();
  phone = phoneFormat(phone);
  $("#phoneNumber").text(phone);
</script>

See demo:

http://jsfiddle.net/Q7GNX/