Archive

Archive for the ‘Other’ Category

Understanding Arrays and How to Use Them

June 24th, 2009 admin 1 comment

So in a recent project I have just finished up (again, will let you guys know what it is when it’s officialy released), arrays had become my best friend along with some regex, but that’s a topic for another post. I needed to turn strings into arrays, which I would then put back into arrays, and finally back into strings. I did this in a few different ways, but let’s go into some examples of why one would need to do this, then we’ll begin the how; seeing as the how without the why makes very little sense.

Well, I mainly use all this to transfer large amounts of separated data from function to function. In my most recent endeavor it was to split a specific type of file, parse it through 3 different functions (each function returned an array, but dealt with the data in strings which I will explain) then finally took all the information in the provided array and successfully dumped the data to a database. This is only one of the numerous reasons a person could/would use an array, but they help like one couldn’t imagine.

Let’s begin with the basics. For sake of example, we’ll keep things in PHP. The first example below just explains an array. No more, no less.

<?php
// Ways to add data to arrays!
$array1 = array("Entry 0","Entry 1","Entry 2","Entry 3"); // Just put data in
$array2 = array(
0 => "Entry 0",
2 => "Entry 2",
1 => "Entry 1"
); // Define arrays by opening and stating index.
$array3[0] = "Entry 0";
$array3[1] = "Entry 1"; // Enter directly into index

?>

Another way to build an array, more or less indirectly, is to use php’s “explode();” function. This will take text with a defined delimiter and separate each set of data (at the delimiter) into its own index. For example, consider the following.

<?php
$string = "This is a string where we will, use commas, for delimiting the text and to, split this into an, array!";
$array = explode(",",$string);
print "Original: ".$string."<br /><pre>Array: ";
print_r($array);
print "</pre>";
?>

To call these defined arrays we would use $array[INDEX]. Now, there are many ways to access this data and filter it. Let’s begin by calling one value from an array.

<?php
$array = ("Entry 0","Use this text","Entry 2","Entry 3","This text isn't being called either");
print $array[1];
?>

The script above with print “Use this text” because it is in the array at the index value 1. It is important to remember that indexes start at 0 unless defined otherwise. Now, we’ll explain some more intricate ways of getting all your values. The first way is iteration using the “for();” loop. Here goes:

<?php
$array = ("Test","Test2","Word3","Keep 'em coming!","white space next :P ","","No need for that","We <3 trim");

// Iterate.
for($i=0;$i<count($array);$i++){
print $i.") ".$array[$i]."<br />";
}
?>

Now this will just result similarly to the foreach(); which I am about to explain. However, sometimes iteration is more effective than foreach(); because it gives you more liberty. Most of the time I would recommend iteration for multi-dimensional arrays (arrays within arrays and so on), but usually two different ones. For example:

<?php
$array = array(array("Array in index","number 0"),array("Array in index","number 2"));

for($i=0;$i<count($array);$i++){
// Now, let's get our multidimensional values! :D
for($ii=0;$ii<count($array);$ii++){
print "[".$i."][".$ii."]: ".$array[$i][$ii]."<br />";
}
}
?>

Now, that will grab all the data within the arrays within the arrays. Wow, sounds confusing, but once you get a better grasp, it’s really quite simple, but I cannot lie; it all seems very daunting at first. Now we’ll explain the foreach(); function. This function basically does what we just did manually, but sometimes is easier and more effective. A lot of times, you will use the two in conjunction (even if for different purposes), so become friendly with both of them!

<?php
$array = array(
0 => "Entry 0",
1 => "Another entry",
2 => "Again...",
3 => "And finally :P "
);

foreach($array as $key => $value){
print "Key/Index: ".$key.": ".$value."<br />";
}
?>

From the code above, you can see that the foreach(); function has accomplished the same feat we did earlier with iteration. On a simple array such as this, it would be best to use foreach, but like I said: it may be necessary at times to use iteration.

As I mentioned earlier, both can be used in conjunction. Just a quick example (we won’t go through the actual coding of it) is if you have a multi-dimensional array where you feel the need to use foreach, you can use foreach($var[$i] as $key => $val){} and work it that way.

So all in all, everything boils down to: love iteration, love foreach(); on the programming languages it’s available, and above all LOVE arrays! They are right next to regex in the developer’s “most useful tools” section of his or her tool belt!

Regards,
Dennis M.

Categories: Other, PHP Tags: , , , ,

PHP vs Ruby on Rails (RoR)

June 2nd, 2009 admin 4 comments

Well people have been asking me lately about this Ruby on Rails (RoR). I often receive questions such as “What is RoR?” or “Is it better than PHP?”. Well, Ruby on Rails is another web programming language, but is not widely compatible across all web servers, but is increasingly becoming so. Personally, I am more of an advocate to PHP, but we’ll try to get the facts straightened out.

To begin, I’m going to assume everyone knows the basics of PHP. It’s fast, efficient, integrated with the webserver, etc. But for those of you completely unfamiliar with Ruby on Rails it is made by the “Ruby Core Team” (of the Ruby Programming Language). The format of the language is similar to Python if any are familiar with that. Ruby generally runs on its own server, separate from the general webserver-to my experience anyway.

So now that we have the basics down about each let’s go over the weak points in each and strong points. We will begin with the weak points to Ruby on Rails. Since it runs on it’s own server, it makes it more difficult to readily integrate with a site: obviously. Also, for those of us who prefer the nice C/C++ style programming, you can forget it with RoR. You will definitely end up using “end” much more than you would care to admit with this language. Compatibility across servers is more or less low. Most servers do not provide the RoR service, so that is also a downfall of RoR.

Likewise, PHP also has its faults. Since the language is so widely used and installed on practically every serer on the web, it allows for more holes to be more easily and quickly found. This means more frequent updates (which could be good if it does not become a nuisance) and since it is integrated with the web server, that also means long patch times. Depending on how you have setup PHP, you may even have to recompile the webserver. Normally it doesn’t cause a great problem for most servers because the old version doesn’t remove until the new version is finished compiling, but it greatly increases server load. For webhosts, this could cause a problem because this means that they have to go down for more server maintenance time. Also, unfortunately for PHP, every time one wants to add a new feature (example add ziplink support or something) they would have to recompile PHP.

Although both have their faults, both also have very strong points. Again, we’ll begin with RoR. RoR, again, since it’s not integrated means little downtime if it needs to be updated. Also, it is similar to Perl in the respect that it can download modules quickly and easily. Ruby, growing more popular, also is easier for users who are entering programming. It definitely scores an A+ on the entry level programming language level and pretty powerful with simple code.

PHP, on the otherhand, though a little more complex has its key aspects. There is mass compatibility with PHP. As mentioned earlier, pretty much every server on the web is PHP enabled. It has become the web standard over the years. Another benefit, since most open source software is programmed in PHP, is one who knows it can make their own program modifications and customizations to most open source programs. Above all, the support over the web is great because users all over the web have a deep understanding and knowledge of the language and are willing to offer free help on forums all over the web.

So all in all, both languages have their ups and downs. Ultimately, it is your choice to decide which you would rather learn/install on your server. Peronsally, I would go with PHP simply because it’s more available and there is a greater user base to help you. If you are looking for custom projects to be made, it may also be much easier to find a programmer to do work for you if you’re looking for PHP. But if you have never looked at programming before and cannot seem to grasp the concepts of PHP, Ruby may be a good alternative to start learning how programming should look and feel.

Regards,
Dennis M.

Categories: Other, PHP, Software Reviews Tags: , , , ,

Some Topic Ideas!

May 23rd, 2009 admin No comments

Hey guys, sorry for the long delay in updates, I’ve been taking topic ideas. None have quite struck my fancy (so to speak) where I feel they would make some great discussion topics. So please, send me some suggestions via email (dennis [at] microsonic [dot] org)! If I don’t get any great ones in the next day or so (well, either way really), I’ll be posting my “Deep Links Mod” for phpLD 2.x.x. Since there already is a great working one for phpLD 3.x.x, I will be releasing the one I wrote for a recent project I just finished up!

Regards,
Dennis M.

Categories: Other Tags: , , , , ,

Using the Internet as a Revenue Generator

May 18th, 2009 admin 2 comments

So I get many inquiries about how to make money on the web. Obviously, my first (and instinctive) response is to say, “Oh, it’s easy! Just develop.” But then, when I come down from cloud nine, I realize that not everyone is a developer (thus the reason I have work). Yes, anyone can certainly do it, but it is a skill acquired over a long period of time to get really good at. So what can the “average joe” do to make money on the web?

Well, really, there are many things users can do. With the recent boom of open source programs (and if you’re willing to invest, the paid programs normally are worth the extra bucks) you can open all different sorts of sites. If you’re not selling any particular product, you have to make your site appeal to the masses. The name of the game is traffic. It is the most important aspect to any successful site on the web. With no users, there is no revenue.

Let me explain. I write this blog to help people, however, I have ads. Now, they do not generate much money for me (hardly anything really), but for a site with major traffic (like myspace or facebook), these ads are generating thousands per month. The more traffic you receive, the greater than chance your ads will be clicked. The more clicks they receive, the more money you earn. That’s the basis to internet revenue if you’re not selling something.

Now, there are many great kinds of sites. Blogs are useful if you have unique content. Directories are nice if you can get your PR (Google Pagerank) up high (do this with backlinks).  But among the most effective traffic generators (not to mention the most CPU intensive and generally require a dedicated server of Virtual Private Server) are web proxies. People love these, so load these with ads and allow more or less unrestricted web access and you will probably be getting a nice chunk of change in return.

So I’ve been mainly discussing Google adsense and generally ad generating services, but do not forget keeping stats on your own (awstats is pretty good) is an always valuable technique. If you have proof (like the awstats page) of high traffic, you can sell ads directly to interested users. Say you have 1 million unique hits a month, you can sell a top banner ad for $X,XXX per month or more.

As I mentioned before, traffic is the name of the game. It bears repeating because it is the only reason for internet web site success, on all levels. Whether you are selling a product or just serving free content with some ads, the more users you get, the more products you will sell or ad clicks you will get. This means, however, you must invest time and money into advertising yourself to get your site known.

For those of you who would like to claim that advertising doesn’t really work, I challenge you to then explain to me what companies have the best sell quantities. The most heavily advertised businesses are simply among the most profitable due to the advertising and offering a (in most cases: fairly) decent product. So get out there, make sure your site is presentable, and get to advertising!

Regards,
Dennis M.

The For(); Loop

May 13th, 2009 admin No comments

Many new programmers are perplexed by the “for” loop. However, it is one of the most essential and powerful tools in any developer’s arsenal. Without it, efficient and clean programming would not be as possible. Early on, most developers seem to stick to the while(); loop. I mean sure, while(); is great for some things, but may not exist in some programming languages and not to mention won’t work for everything you need.

Let’s begin by going into the basics about the for(); loop. Well really, a lot depends on the programming language (I will provide both PHP and C++ examples in the source file). But more or less, the for(); loop is simply iteration. It will run until its defined limit is hit providing whatever results you wish. We’re going to do this in more standard languages (e.g.: C/C++ and Java) instead of PHP for mass compatibility. Just note, if you’re looking to do this for php, you do not need to define the type of variable, and your variable will just start with the “$” symbol. So for example, PHP would be for($i=0;…;…){}.

Now we’re going to write a sample program and break it down:

for(int i=0;i<=5;i++){
std::cout<< "This is line number "<< i << std::endl;
}

Now as most of you may have noticed, this is a C++ example, but will effectively explain how the loop works for ALL programming languages!

int i=0; – This simply defines the variable and starting place (0 in this case). In PHP you would simply use $i=0; and so on for the rest of the statements.
i<=5 – This tells the loop when to stop (This <= symbol means less than or equal to). When i>5, the loop will stop and proceed on to the rest of the code.
i++ – This tells what the loop should do after every time it is run. In this case, we’re using i++ which means to add 1 each time around.

Now those are the basics to the loop. What lies within is just simply the code to be executed, which could of course have conditionals and such within it as well (e.g.: if(), etc.). So now we’ll move on to a more complex example.

unsigned int choices=5;
char* choice = new char[choices];
for(int i=0;i<=choices;i++){
std::cout<<"Please enter a value: ";
std::cin>>choice[i];

if(!choice[i]){
std::cout<<"No value for choice "<< i<< std::endl;
}
}
for(int i=0;i<=choices;i++){
std::cout<<"Choice "<< i<<" was "<< choice[i] << std::endl;
}

This will store an array of choices and then after entering them, they will be printed back!

In the source below, all working examples will be provided!
For(); Loop Tutorial

So, that basically wraps everything up! If you need anymore clarification, just let me know.

Regards,
Dennis M.

Categories: C/C++, Java, Other, PHP Tags: , , , , , ,