Archive for April, 2008

Apr 24 2008

Some More PHP Abilities You May Not Know About

Published by David Jeffries under Programming

Because my first PHP abilities you may not know about post seemed to be useful, here is part 2: some more PHP abilities you may not know about. If you want to learn about ternary operators, calling functions from strings, or variable variables, check out the previous post. Read on to lean about some other things that you can do with PHP.

Modify variable by reference

<?php
$test = 'new';
modify($test);
echo $test; //prints new variable;
function modify(&$variable){
  $variable .= ' variable';
}
?>

By using the "&" symbol before the parameter in the function, you pass a reference of the variable to the function.

This saves you from having to say $david = duplicate($david), which can be nice in some situations.

Point a variable to another
This is similar to the example above. You are able to set a variable to reference another by using "=&", an example below:

<?php
$david = 'i am david';
$joe =& $david;
$joe = 'i am joe';
echo $david; //prints i am joe.
?>

In this example, any change to $joe will result in a change to $david, because $joe is essentially $david.

Default parameter value
I'm amazed at how many people don't know about this. When setting up a function's parameters you are able to specify a default value if the parameter is not passed.

<?php
func('test');
function func($foo, $bar = 'value'){
  echo $foo . $bar; //we didnt pass $bar, so the result will be 'testvalue'
}
?>

The parameter $bar is not passed to the function, so the default value of 'value' is used instead.

Create variable from a string
I explained variable variables in the PHP post previous to this one, but did you know that you can combine two (or more) strings then treat the result as a variable?

<?php
 $var = 'my';
 $bar = 'var';
 $myvar = 'testing';
 echo ${$var . $bar}; //prints the $myvar variable (testing)
?>

Man, PHP is crazy.

4 responses so far

Apr 11 2008

Tips For StumbleUpon Traffic To Your Blog

Published by David Jeffries under Internet

If you haven't heard of StumbleUpon yet then go to their website, download the toolbar and check it out. This post assumes that you know what it's all about.

If you're a blogger there's a good chance you have seen the effects of being 'stumbled' which is pretty much a huge increase in traffic, comments, and RSS subscribers. If you look at the image, you will notice two distinct spikes that are the result of two posts being stumbled. These two stumbles have totaled over 10,000 unique visits in less than two days. How did I do it? Read on.

The first stumble

The first stumble was from the post, Some PHP Abilities You May Not Know About, which has so far brought in over 4,000 unique visitors.  Most StumbleUpon users are tech-savvy, so it's no surprise that this post got fairly popular quite quickly.  Lets highlight some other characteristics of this post.

  • It's in a list format
  • Shows real-world usage
  • There is interesting information that can be reused by readers

The second stumble

The second stumble was from the post, A Stuck Pixel On My LG L226WTX, which was much more popular than the PHP post. This post hit 7,000 uniques on the day it was first on StumbleUpon.  This post was much more popular on SU than the other post because while SU users are tech-savvy, there are only so many interested in programming.  That's the thing about StumbleUpon, it matches people's likes to webpages really well.  So while there's quite a few people with "PHP" as one of their likes, there are way more with "computer," or "programming" as their likes (the two categories that most visitors had for the stuck pixel article).  Basically a category with a wider audience = higher chance of your site being viewed.  Now again, lets look at some of the things this post had.

  • First hand experience
  • Info that can help the reader
  • Info worth saving for later reference

Reasons you will get stumbled

So by looking at the lessons learned from the two stumbles, we can determine the common factors, and find out a foolproof method to getting your website stumbled.  Here are my three tips for getting stumbled:

1. Use lists

People love lists.  They're easy to read, they convey a lot of information in a small format, and are easy to reference.  It's easier for a reader to scan to the part of the list they're interested in than to scan an article full of text digging for some piece of information.

2. Write information that is valuable enough to save

People want information that they can use.  In both cases, the PHP and the stuck pixel article, there was information that can be reused, and will be reused by readers.  If the post tells your audience how to do something they didn't know about, or how to do something better, they will like it much more as it affects them directly.

3.  Prove it

Show to the reader how the information can be useful to them.  Prove that what you're writing about actually works, and explain to the reader how they can recreate/reuse the information.  Use pictures, or examples so it at least looks like you know what you're talking about.  Try to stay away from lengthly descriptions, as large blocks of text can be hard to read and may bore your readers.  Just make your point, and move on.

It all comes down to good content

In the end, you have got to have excellent content, and you have to make the reader say to themselves, "cool, I like this!"  You need to have something that sets you apart from every other blog out there.  So use these tips, write some awesome content, and build some traffic!  Let me know your success with SU in the comments.

2 responses so far

Apr 04 2008

Some PHP Abilities You May Not Know About

Published by David Jeffries under Programming

Throughout my time programming PHP I've come across a few interesting things about the language. Coming from a primarily Java background, it seems quite strange to be able to do some of these things so easily.

Having said that, here are a few things with PHP that I've found interesting, and hopefully some of these things you've never heard about.

Call a PHP function from a string
PHP is an interesting language. To do the following in Java, it would be much more complex - around 5-10 lines of code. In PHP however, it is quite simple to call a function dynamically from a string.

<?php
  $call_this = "phpinfo";
  $call_this();
?>

You can even pass arguments to the variable like you were calling the function directly - it's pretty cool. Here's a practical example: Consider a function that accepts either a jpeg or png image file and resizes it (this is only a subset of the function).

<?php
  switch($imgType){
    case "jpeg":
      $create = "imagecreatefromjpeg";
      break;
    case "png":
      $create = "imagecreatefrompng";
      break;
  }
  $create($filename); //this will create either a jpeg or png
?>

This makes the code considerably cleaner by dynamically setting the function that will be called.

Ternary operator
This is a quick way to write an IF statement, without the structure and braces. The way it works is like a normal if statement, you pass it true or false (in the following case, ($test == true) == true), and then the ? means then and the : means else. It's pretty simple, and is nice to use for simple one liners, in cases like the following example.

<?php
  $test = true;
  echo $test == true ? "is true!" : "not true";
  //will print "is true!";
?>

A little bit nicer than

<?php
  $test = true;
  if($test == true){
    echo "is true!";
  }else{
    echo "not true";
  }
?>

Easily echo a variable
I don't really like this method, I prefer to just use <?php echo "string"; ?>, but maybe this way is faster - I'm not sure. Either way, pretty much all it does is echo the variable that is after the equals sign.

<?php
  $val = 50;
?>
<!-- some HTML or whatever -->
<?= $val ?>

Variable variables
Variable variables are variables that can be dynamically set with strings. In the following code, you will notice that we set the variable $val to "hello" and then set the variable variable (the string that $val holds ("hello")) to the string "variable variable". When we echo $hello, the text "variable variable" will be printed.

<?php
  $val = 'hello';
  $$val = 'variable variable';
  //the string that is set in $val is now the name of our variable.
  echo $hello; //will print 'variable variable'
?>

Calling a variable variable function
Expanding on the variable variables, you could actually combine that example with calling a PHP function by a string and do this:

<?php
  $val = 'show_info';
  $$val = 'phpinfo';
  echo $show_info(); //will echo phpinfo()
?>

Here, "show_info" is the name of the variable, and its pointing to "phpinfo" Writing $show_info() calls the function that the variable variable is pointing to. It's kind of confusing and would probably only be used in some really complex code, but it does work.

38 responses so far

Apr 03 2008

A Stuck Pixel on My LG L226WTX

Published by David Jeffries under Hardware

4 days after I purchased my new monitor, I have a stuck pixel. That is at least what I think it is. It's a little blue spot on the monitor that didn't wipe away, so I'm assuming that it is in fact a stuck pixel.

Stuck Pixel On Monitor

The picture makes it look worse than it actually is, but the blue pixel is still definitely noticeable. After researching the issue on the web, I found that there are pretty much three choices to fix it.

The Software Method

I read about a program that will remove stuck pixels. After looking into this, I found a utility provided by WidowPC. It is not actually a program that runs. What the WidowPC tool does is runs a video that flashes red, green, white, black, and blue. I thought this sounded cool, so I downloaded and ran the video. I set my media player to loop the 52 second video, resized it, and placed it over the blue dot. After running it for 2 minutes, the pixel was still there. I decided to leave it longer and left for 15 minutes. When I came back the blue dot was still there. Hmm, oh well onto the next try.

The Massage Method

There is another method I tried that is called the massage method. If you have used an LCD monitor before and have pressed on the screen, you have likely seen the circle of discoloration left from your finger. What happens when you press on the screen is that the liquid moves around to other pixels. This sounded like it would work, so I gave it a go. I put a napkin over my finger, and lightly rolled it around. Well, this didn't work - even though I thought it sounded like the best method. Time for the last attempt.

The Tap Method

The last method to try was a method that involved tapping a pen on the affected pixel. This is similar to the massage method, by moving the liquid out in an attempt to unset the pixel. I gave a quick little tap with the end of a pen, and the pixel was cleared! I wish I had used this method first.

No More Stuck Pixel

The pixel is now cleared and hasn't come back in over a day. I was really worried when I saw this pixel because when I bought the monitor the seller offered me a pixel warranty. The warranty was $15 and assured something like free replacement on monitors with 4 or less pixel problems for a year. Having heard tonnes of warnings from the news/internet/etc. about extended warranties I decided to pass on the option. We'll see if this was the right choice, but for now, everything is good.

31 responses so far

Apr 01 2008

Playing With Wordpress Code Highlighter Plugin

Published by David Jeffries under Misc

I just installed this code highlighter plugin for Wordpress, and it's really neat. It allows you to post any code you want between <pre> tags, and it will highlight/colour the text properly based on what language you tell it.

Language Support

This plugin uses GeSHi as a fontifier engine, and it supports many, many different languages. From applescript to java to sql this engine supports pretty much every popular language.

Line Numbers

You can also show the code with line numbers by adding lineno="{starting number}" to the pre tag like so: <pre lang="php" lineno="5"> which would start it on line 5. This does, however, make it difficult to copy and paste code because the line numbers are also copied.

PHP example (<pre lang="php">)

<?php
  class Test{
    public function Test(){
      echo "Test instantiated!";
    }
  }
?>

Java example (<pre lang="java">)

class Test{
  public static void main(String[] args){
    Test t = new Test();
  }
  public Test(){
    System.out.println("Test instantiated!");
  }
}

This is one of the most useful plugins I've ever seen, I love it. It makes posting code so much nicer and makes it way easier to read. Now I just have to update all my old posts that have code in them... For more information and to download, go to ideathinking.com

One response so far