![]() |
||||
|
||||
|
« A Random Dispersion Array - C++ console | Changing Client Side Memory Values Using TSearch » |
Porting code from C++ to PHP
by: cable_guy_67 - Aug 03, 2005
Chapter Two : Random Dispersion Array PHPNow that there is some working (C++) code that shows how to create a random dispersion array it is time to make the crossing. Perhaps this was how the explorers felt when sailing into the unknown. They knew how to sail, but didn't know what was in store for them over the horizon. Well, even if they didn't, it's sure how I felt. Before we leave the relative safety of the harbor, let's take a look at what we hope to discover. That is the land of wonder that I hope to find. That is what got my excitement level up. That is what I will dream of on the long journey across the metaphorical sea. Like any trip there will be pitfalls and detours that need to be made. Perhaps we will have to disassemble the ship and carry it over the mountains, rebuilding it when a new body of water is discovered. Perhaps we will need a different type of vessel, and the ship will become lumber for our new homes. These things can be imagined but not known yet on our journey. What we really need to do is put up some supplies that we may need along the way. Since we are decidedly eclectic group lets use a broad brush when we pick up navigational tools. I'm sure that that will not be all we need so lets throw in this, a bit of that, a few things from here and as much as we can carry from here. Now, we don't want to lose track of our friends back home so let's sit this right on top of the pile. After all, we don't want to forget our roots. Or how to get home if we need to. :) Okay, we are loaded down. Some things we will use all the time, some we will only need on special occasions. There are things we have that we don't even know how to work yet, but we can learn. So we wave to the single person viewing and look forward... FILE : rand_prob_func.phpThe first thing we are going to do is wrap our code up so there is no doubt what we are working with. To do this, you start with <?php, give yourself some lines to work with and close with ?> somewhere below. This is considered a single PHP block. There will be times when it is important to start and finish things, all in one block. You may also have multiple blocks in your file. For now though, treat it like the C++ statement, C/CPP/C++ Code Example: using namespace std; when you were first starting out. You might not have known exactly what it was doing, but eventually you figured out that it needed to be there. We also will be using some comments to start our file just like we did with the C++ version. In PHP, you can use your old friends from C++ as well as the pound sign. PHP Code Example:
From the PHP.net manual:
Now we can get the information block at the start of the file done. I will be using the C style multi-line block since with a little modification, it will become something that is special when using Doxygen, a very cool documentation system, but I digress. For now, just remember the ways you can add comments to your code. PHP Code Example:
As you can see, except for the opening PHP tag, this would have worked without modification in C++. Error Reporting in PHPThis was one of the first problems I ran into when starting out with the C++ to PHP conversion. I missed my compiler. I use -Wall whenever I compile code. One of the first pieces of PHP code I wrote would have made g++ punch me in the forehead a few times before drinking all my beer for being an idiot. Luckily, there is a way to get information on code that just shouldn't be allowed to be run. Because it will, oh it will, but you may not like the results. PHP Code Example:
From the manual:
There are many, many things that can be set or unset. You will need to do a little research but the ones I have set up seem to give me a nice level of error message reporting echoed to the page being rendered. They come complete with filename and line numbers so you can track things down just like you would from your compiler errors/warnings. Random Number Generation C++ vs. PHPIn the C++ program there were three items for the generation of random numbers. They were, C/CPP/C++ Code Example: static bool seeded void RNDseed() const unsigned RNDgen(const unsigned MAX) We don't need these in PHP. Instead use mt_rand(). From the Manual:
FUNCTION : GetPer100() and GetMagic100() Integer MathPHP Code Example:
These two functions work identically to their C++ counterparts. The major difference is PHP's lack of an unsigned integer type. Because of this, a cast in GetPer100() is needed to keep the results from being promoted to a float. Looks like a familiar C cast to me. It is something to keep in mind when working with PHP's loose typing. There will be times (like this one) that you will need to specify your results since you can't declare the variable as a particular type. Remember to use the '$' with the variables and this type of covertion goes smooth. FUNCTION : BuildRndList() Randomized Weighted ListPHP Code Example:
Once again, this is nearly a verbatim translation from C++. The biggest difference is using mt_rand() instead of the custom routines that were in the C++ version and array_fill() to zero the array. FILE : rand_global_setup() to simulate C++ main()Since I am breaking this down into smaller and smaller units as I go, the main() testing routine from the first chapter is now it's own code module. In will be part of the next chapter that will also describe a simple HTML form to use for display purposes. In it, you will find the syntax that PHP uses to create an array(). That will be the last of the C++ to PHP chapters and should give C++ programmers enough to see the similarities and differences. To any PHP programmers out there: please comment if I have made any glaring errors. I am teaching myself as I go so it is quite possible I have made an errror or two. Hold tight, I am going to toss many of the extra planned chapters in favor of moving more directly to the PHP Extention with dl() portion of the show. Update from Table of Contents. This series will be shortened and the next chapter will be the last "C++ to PHP" article. After that, the focus will be on using this code (or your own code) to create a PHP extention. Once I have that down, I will be able to tackle using C++ code then classes as a PHP extension.
|
GIDNetwork Sites
Archives
Recent GIDBlog Posts
Recent GIDForums Posts
Contact Us
|
« A Random Dispersion Array - C++ console | Changing Client Side Memory Values Using TSearch » |