JS logging

I’ve been doing a fair about of js development lately and console.log has been my friend. However, writing console.log() is a bit heavy. So instead I’ve been adding this to my files as  a quick way to log in javascipt.

function log() {
if (window.console && console.log)
console.log(‘{product or sitename} ::  ’ + Array.prototype.join.call(arguments,’ ‘));
}

 

Remove empty values from an array.

I developed this little one liner to remove empty values from an array in php.

$tags = array( ‘testtag’, ‘testtag2′, ‘ ‘ );

$tagsCleaned = array_filter( array_map( ‘trim’, $tags ),
create_function( ‘$a’, ‘return $a!=”";’ ) );

Breaking that down inside out.

array_map( ‘trim’, $tags )

Array map the trim function to all of the values within the array, removing removing pre/post spaces. In this case it removes the space in the third item.

array_filter( array_map( ‘trim’, $tags ),
create_function( ‘$a’, ‘return $a!=”";’ ) )

Array filter allows us to remove the elements that do not match the function. In this case, I’ve created a function to test if the value is an empty string. If it is, the function returns true if it is not blank and false if it is. The false return removes the value from the array.

The result will be an array of two items.

$tags = array( ‘testtag’, ‘testtag2′, ‘ ‘ );
$tagsCleaned = array_filter( array_map( ‘trim’, $tags ), create_function( ‘$a’, ‘return $a!=”";’ ) );
print_r($tagsCleaned);

 

Array
(
[0] => testtag
[1] => testtag2
)

Using the + sign to merge arrays in php

Today came across code that used the + sign to merge two associative php arrays. At first I thought it was broken but turns out you can indeed merge two arrays with the + sign.

$album = array(
‘title’ => ‘King of Limbs’,
‘band’ => ‘Radiohead’,
);
$album_meta_data = array(
‘upc’ => ’486898161589′,
‘price’ = ’11.98′
);

$output = $album + $album_meta_data;

——–OUTPUT——–
array(
‘title’ => ‘King of Limbs’,
‘band’ => ‘Radiohead’,
‘upc’ => ’486898161589′,
‘price’ = ’11.98′
)

Another way to use it would be to add the album meta data directly to the album array.

$album = array(
‘title’ => ‘King of Limbs’,
‘band’ => ‘Radiohead’,
);
$album_meta_data = array(
‘upc’ => ’486898161589′,
‘price’ = ’11.98′
);

$album += $album_meta_data;

——–OUTPUT——–
array(
‘title’ => ‘King of Limbs’,
‘band’ => ‘Radiohead’,
‘upc’ => ’486898161589′,
‘price’ = ’11.98′
)

If the key exists in both arrays then the first if used and the second is discarded, same as with array_merge.

$album = array(
‘title’ => ‘King of Limbs’,
‘band’ => ‘Radiohead’,
);
$album_meta_data = array(
‘title’ => ‘Coldplay’,
‘upc’ => ’486898161589′,
‘price’ = ’11.98′
);

$album += $album_meta_data;

——–OUTPUT——–
array(
‘title’ => ‘King of Limbs’,
‘band’ => ‘Radiohead’,
‘upc’ => ’486898161589′,
‘price’ = ’11.98′
)

the reverse

$album = array(
‘title’ => ‘King of Limbs’,
‘band’ => ‘Radiohead’,
);
$album_meta_data = array(
‘title’ => ‘Coldplay’,
‘upc’ => ’486898161589′,
‘price’ = ’11.98′
);

$album_meta_data += $album;

——–OUTPUT——–
array(
‘title’ => ‘King of Limbs’,
‘band’ => ‘Coldplay’,
‘upc’ => ’486898161589′,
‘price’ = ’11.98′
)

VMWare Fusion 4 – Set static IP address

In vmware Fusion 4 there is no boot.sh that is found in previous versions

sudo “/Library/Application Support/VMware Fusion/boot.sh” –restart

So if you make a change to the network settings like setting a static ip address you’ll need to restart your computer for the settings to take effect.

To set a static ip in VMWare Fusion 4 do the following:

First on your vm in terminal run

ifconfig

Then copy the HWaddr

Link encap:Ethernet  HWaddr 00:0c:29:f4:13:e4

Next copy the dhcp.conf file on your mac

sudo vim /Library/Preferences/VMware\ Fusion/vmnet8/dhcpd.conf

After the

####### VMNET DHCP Configuration. End of “DO NOT MODIFY SECTION” #######

Add your new configuration, replace “vmnamehere” with the name of your vm.

host vmnamehere {
hardware ethernet 00:0c:22:f6:11:e8;
fixed-address 192.168.115.50;
}

Now reboot your Mac.

Your vm’s ip address should be what you set.

If that doesn’t work, you may need to remove the network adapter and then re-add it. Make sure that your network adapter is connected to the virtual machine by removing and re-adding it. See Step 12

 

How to add git to an amazon ami ec2 instance

To install git on amazon ami is really simple. Make sure you have sudo and then run yum to install it.

sudo yum install git

That’s it. The output should be something like:

$ sudo yum install git
Loaded plugins: fastestmirror, priorities, security
Determining fastest mirrors
amzn-main | 2.1 kB 00:00
amzn-main/primary_db | 1.6 MB 00:00
amzn-updates | 2.3 kB 00:00
amzn-updates/primary_db | 233 kB 00:00
Setting up Install Process
Resolving Dependencies
–> Running transaction check
–> Processing Dependency: git = 1.7.2.5-1.16.amzn1 for package: perl-Git-1.7.2.5-1.16.amzn1.i686
—> Package git.i686 0:1.7.4.5-1.21.amzn1 set to be updated
–> Running transaction check
—> Package perl-Git.i686 0:1.7.4.5-1.21.amzn1 set to be updated
–> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================================
Package Arch Version Repository Size
==============================================================================================================================
Updating:
git i686 1.7.4.5-1.21.amzn1 amzn-main 4.4 M
Updating for dependencies:
perl-Git i686 1.7.4.5-1.21.amzn1 amzn-main 16 k

Transaction Summary
==============================================================================================================================
Install 0 Package(s)
Upgrade 2 Package(s)

Total download size: 4.4 M
Is this ok [y/N]: y
Downloading Packages:
(1/2): git-1.7.4.5-1.21.amzn1.i686.rpm | 4.4 MB 00:00
(2/2): perl-Git-1.7.4.5-1.21.amzn1.i686.rpm | 16 kB 00:00
——————————————————————————————————————————
Total 7.9 MB/s | 4.4 MB 00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Updating : git-1.7.4.5-1.21.amzn1.i686 1/4
Updating : perl-Git-1.7.4.5-1.21.amzn1.i686 2/4
Cleanup : git-1.7.2.5-1.16.amzn1.i686 3/4
Cleanup : perl-Git-1.7.2.5-1.16.amzn1.i686 4/4

Updated:
git.i686 0:1.7.4.5-1.21.amzn1

Dependency Updated:
perl-Git.i686 0:1.7.4.5-1.21.amzn1

Complete!

Find loaded modules in Apache 2.x

I was trying to figure out how to find the loaded modules in Apache and came across this nice command.

httpd -M

Used like:

$ httpd -M
Loaded Modules:
core_module (static)
mpm_prefork_module (static)
http_module (static)
so_module (static)
authn_file_module (shared)
authn_default_module (shared)

……

rewrite_module (shared)
php5_module (shared)
extract_forwarded_module (shared)
geoip_module (shared)
Syntax OK

Display phpinfo from the command line

This afternoon I was wanting to get a couple things from phpinfo but didn’t want to do the typical make a php file, put phpinfo, run it from a browser or command prompt. There’s a better, quicker way if you know what you want. One will output it to a file for review:

echo “<?php phpinfo(); ?>” | php > phpinfo.txt

Second is the quickest. It output the info and the grep for the information you need:

php -i | grep ‘version’

Replace version with your own search query.

Scaling PHP Applications with Redis

Redis is an important technology when dealing with caching. This was one of the talks that I really wanted to attend at ZendCon. It was given by Josh Butts of Vertive LLC.

Redis is a NoSQL technology that rides a fine line between database and in-memory cache. Redis also offers “remote data structures”, which gives it a significant advantage over other in-memory databases. This session will cover several PHP clients for Redis, and how to use them for caching, data modeling and generally improving application throughput.

Redis is an open source in memory key value store with optional persistance. The current version is 2.4.2 as of this writing. The latest stable version is 2.2.15 which what Josh recommended as the minimum version you would want to use. He noted that 2.4 was pretty stable in it’s own right. Hoever, I have notice that since 2.4.0 release 10 days ago they have already release 2 patches (now at 2.4.2). Thus, personally, unless a feature of 2.4 is necessary I’d say go with 2.2 latest or 2.3 latest.

Once Redis is installed how to connect to it becomes an issue. According to Josh, there’s not a good GUI tool to use with Redis. Instead it’s best to use the command line interface.

On the actual functioning of Redis, it functions on hash keys. Think of it like associative arrays in PHP. However, there’s a huge difference. There is NO nesting. The array is only one level deep.

As far as connecting PHP to Redis there’s two options that Josh mentioned. Rediska and Predis.

Rediska is compatible with PHP 5.2+ and Zend Framework 1.x. A huge feature is the profiler. It allows you to understand how your application is using Redis and optimize it from there. Another positive is Rediska has native session handling.

Predis is built for PHP 5.3+ giving it  the ability to being developed for the future and not being locked in to older technology. It has profiling and lazy connections to the redis server.

Josh’s recommendation between the two was use Predis if you have 5.3 available to you, but Rediska was okay too. It seemed that one of his main reason behind this was Predis has been seeing more recent activity and was thus more likely to be maintained in the future.

My take, at the moment, is also to go with Predis since it seems like a more active project. It also looks as if it is looking forward and being developed for feature redis improvements. I haven’t had an opportunity to actively  test between the two, so my decision is temporary until I have the opportunity to factually compare instead of going on hunches.

HTML5 WebSockets – Presented by Scott Mattocks

This was the first talk I went to at ZendCon. The provided description:

The emergence of HTML 5 and related technologies opens up a new world of possibilities for web applications. Among those new technologies are WebSockets, which allow for bi-directional communication between the browser and the server. This session will introduce WebSockets by exploring a few practical applications before diving into the JavaScript API and the WebSocket communication protocol.

What I gained most from the talk was when he talked about websockets. In a gross generalization, they can be used to replace some common applications of ajax communication between the server and the client. Instead of using long polling in a web application, which has unnecessary overhead when sending small bits of data, using a web socket connection for the communication is a better solution.

Web socket communication requires work on both the server side and client side to enable the two way communication.

On the client side, like ajax, it relies heavily on javascript. Processes information in a similar fashion to AJAX. Web sockets at it’s heart is about the communication of data between the server and client. There’s some work on the client side to allow the setting up and maintaining of the connection, but really less than with a long polling ajax setup.

On the server side it’s a little bit more work. The original request for a connection is sent over the standard http port for the site. Then the web socket server will respond with the port to set the connection up with the web socket server.

 

More Information: