Archive

Archive for the ‘PHP’ 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: , , , ,

Importance of a Portfolio

June 10th, 2009 admin 1 comment

Portfolio. The word sometimes lingers in one’s mind, but usually is associated with artwork, or a collection of works from your high school English class. But really, an online portfolio (whether it’s only artwork/graphics or programming) is very important when trying to find interested clients.

Generally, most people can only establish their names by doing quality work at a great price (or in a few cases, great work and advertising at a high price pays off too – but those are the few giants of the web today). Word of mouth is good, but when you have a website, you should be showing off more than that to your potential clients. When a client visits your site (no matter which way – browsing, by accident, referal, etc.) their first opinion is your site itself. From there, they are looking for a giant link or button that says “Portfolio.”

When the user goes to this link, they don’t want to see just links, but they want a thumbnail shot of what to expect, a link to a working example, and a description of the purpose of the work. Now I know I do not have all of those things right now (just yet, anyway), but they are well worth it. Since most users see other works I’ve done (and after speaking to me on MSN) they are always satisfied with what I have to offer, but I am working on my designing skills and a portfolio script which I shall be launching very soon so I can practice what I preach.

So now that everyone understands that a portfolio should be more than just a collection of works you done, everyone can start really presenting themselves effectively. Like I said, soon I will be finishing my portfolio script and perhaps releasing it to the public, so if you are unsure about how to get yours started, that may be a great starting point for you!

Regards,
Dennis M.

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: , , , ,

Knowing What to Use (or make) for Your Site

May 26th, 2009 admin 1 comment

So it seems quite often new webmasters are at their wit’s end when the question arises: what features does my site need? Well, this common question can be quickly turned into a daunting test of thought to new and experienced webmasters alike. So how can one even begin to approach this question? Well, why am I asking you? That’s what you’re reading this for ;) .

Alright, so you are creating a new site and are not sure what features you should start packing it with. Well, the most important thing to keep in mind is that the site should always be updated. So if at first you forget some features, don’t rush to get them finished right away; save them for future updates. By keeping sites updated, as I’ve mentioned in previous posts, you keep users coming back to your site. Once you have that out of the way, you can move on to deciding site essentials.

Deciding site essential features is an absolutely necessary step to creating a successful site. Consider your site’s purpose, function, and most importantly, what makes it unique. Whatever makes it unique should be the most important feature you first include. Followed by what it does and its purpose. For example, I have created a blog here. So I would not make this site a giant forum and expect people to simply read my blog posts, but rather I setup the blog and add an RSS feed so people can subscribe and stay updated.

So more or less, when a user is trying to figure out what is necessary, those functions that make your site worth viewing are what you need to include. Then, if you feel you’ve forgotten something, add them in for later additions so people have yet another reason to keep coming back to your site! Remember, with the web changing literally every second, one needs to keep their content and sites updated frequently (hmm maybe I should follow my own advice, eh? :p).

Regards,
Dennis M.

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

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: , , , , , ,