Hey there, this site is pretty old now. I've decided to leave it up as I put a lot of work into it and would hate to see it disappear.

Display Tags For 'php'

ASP/PHP Cross-Reference

Chris Retlich
2/25/2009 2:55:42 PM
A list of fairly common actions with both the ASP and PHP equivalents listed. Good resource if you are familiar with one of the languages, but can't remember the equivalent function in the other.

Converting A Site From Classic ASP To PHP: Some Things To Consider

Neal Grosskopf
8/19/2009 12:37:37 PM

Well, after a month of spending my evenings coding till the wee hours of the night, I've finished converting my website NealGrosskopf.com from Classic ASP to PHP. After going through this grueling ordeal I thought I'd write up a quick article detailing my experience and perhaps save a fellow web developer some time should they ever endeavor this process.


Learn The Basics

If you're not familiar with PHP like I was, I'd suggest you learn the basic syntax differences between ASP and PHP. I pretty much self taught myself PHP using the tutorial website Tizag. I feel like I'm a pretty competent ASP coder so learning the basics of PHP wasn't too hard for me. Here's a few observations I found.

  1. Built In Functions

    PHP seems to like ordering parameters in built in functions slightly different than ASP. For instance in ASP you have:

    replace("Input String","Find","Replace With")

    In PHP the order is slightly different as well as having a slightly different function name:

    str_replace("Find","Replace With","Input String")

    Some other examples of this in PHP are str_len (len in ASP), strpos (instr in ASP) and explode (split in ASP)

  2. Parse Errors

    Forgetting to add a semi-colon ';' at the end of your PHP line = 'white screen of death'

    At least with my host's configuration (or possibly my own stupidity) parsing errors such as forgetting the semi-colon results in a very not-useful white, blank, empty PHP error page. This can make troubleshooting the error very, very hard.

    Fortunately I found a solution to this. Portable XAMPP. I essentially ran web server locally which allowed me to test out my PHP pages (minus the My_SQL database). For whatever reason, my local copy did not give me the white screen of death and let me see the exact error message. This helped me find the error 10x faster. I suggest if you're starting out with PHP, download Portable XAMMP and tinker with it locally, for free!

  3. Code Order

    One problem that I ran into was the placement of my functions within my code. In ASP I could include files at the end of my page, and even have functions within these includes be called even before the code has reached that area of the script. I suspect ASP pre-compiles the functions ahead of time while PHP does not. My solution to this was to re-organize my code so that all the functions appeared first within my include structure.

  4. Variable Scope, and isset()

    Scope Another problem I ran into was variable scope. In Classic ASP I'd often something like this:

    FirstName = "Neal" ... somefunction("Grosskopf") function somefunction(LastName) response.write FirstName & " " & LastName end function

    In PHP this will not work unless you use the $GLOBALS[] array to your variable in the function like so:

    $FirstName = "Neal"; ... somefunction("Grosskopf"); function somefunction($LastName) { $GLOBALS["FirstName"]; echo $FirstName . " " . $LastName; }

    I'd also change global variables inside functions in Classic ASP. To fix this in PHP simply add the global keyword to the variable:

    function somefunction() { global $FirstName"; $FirstName = "Some Other Name"; }

    I'm sure these aren't good programming practices, but hey it works!

    isset() In ASP you could do the following even if the variable has yet to be set:

    if variable <> "" then 'do something end if

    In PHP this is not acceptable. Instead you'll need to use a built in function called isset(). This basically checks to see if the variable has been set yet:

    if(isset($variable)) { //do something }

    This isn't a huge difference, but since I was using the ASP method quite a bit, I needed to re-learn the new way to accomplish this in PHP.

  5. Redirects

    Another common problem you'll run into when converting from ASP to PHP is redirects. In Classic ASP you could do a redirect using the following:


    In PHP if you need to use the header function

    header("Location: thank-you-page.php");

    Another thing to consider is if you place your header function further down your page, possibly after you've already output content to your page. If this happens then you'll need to use the output buffer, at the top of your page:

    ob_start(); I'd also recommend saving your session data before you do a redirect in PHP like so: session_write_close();
    Here's all the code at once:
    ob_start(); //Some other code here session_write_close(); header("Location: thank-you-page.php");

Redirect Those Old ASP Pages To PHP

The great thing about switching to PHP is you now have the ability to use the much hailed .htaccess file. My very limited perception of this file is it acts as a gatekeeper between every file on your website and the end-user requesting the files. It allows you to intercept any request made to the server. One sweet rule I found for .htaccess is the ability to redirect my old ASP pages to their PHP equivalent:

RedirectMatch 301 /(.+)asp$ /$1php

Its sad but that one line takes care of it all for me. Google will now crawl my new PHP pages, and users with bookmarks to the old pages will still find them. It's almost too easy!

Final Thoughts

Those are just a few of the differences I've experienced between PHP and ASP. I'm sure there's thousands more. Overall I'm very happy with my new PHP website and I'm happy I've finally taken the time to switch.

Anyone else convert an ASP site to PHP before? How was your experience?


Easy Way To Get Your Twitter Followers Count/Custom Twitter Badge Using PHP

Neal Grosskopf
8/24/2009 9:09:00 PM
Example Twitter Follower Counter

Custom Twitter Badge

An example of what can be accomplished by creating your own Twitter Badge.

So you're on Twitter now? Like back in high school, we all want to be popular on Twitter as well. Prove it by displaying your followers count with this simple PHP script.

One thing that annoys me with all third party web services is they feel the need to brand their widgets. I don't blame them for doing this but it deters me from ever using them. What usually results from this is I tinker around and figure out a way to get the data I need without all their ugly branding.

The Setup

First we'll need to use PHP's built-in curl function. This will grab the output of other web pages, files, etc. Note I lifted the script below from another website so feel free to correct me on it.

function curl($url) { $ch = curl_init($url); curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); curl_setopt($ch,CURLOPT_HEADER, 0); curl_setopt($ch,CURLOPT_USERAGENT,"www.YOURDOMAIN.com"); curl_setopt($ch,CURLOPT_TIMEOUT,10); $data = curl_exec($ch); curl_close($ch); return $data; }

Custom Twitter Badge Function

Next, we create the function that parses the curl output. This uses the Twitter API. Simply call the function with your own Twitter username and it will return the number of followers you have:

function GetTwitterFollowerCount($username) { $twitter_followers = curl("http://twitter.com/statuses/user_timeline/".$username.".xml?count=1"); $xml = new SimpleXmlElement($twitter_followers, LIBXML_NOCDATA); return $xml->status->user->followers_count; } echo GetTwitterFollowerCount("YourTwitterName");


Please take into consideration that you can only ping Twitter 150 times per hour. Because of this, it would be wise to create a text file and use it to cache your followers count. Also the function will only return a plain ol' number. Its up to your to think of a creative way to display this to your site's visitors. The more attractive you make it, the more likely your users will click it.

Related Posts:


<< Back