Archive Page 2

Sometimes you have an array of elements that may contain duplicate entries, but you do want any duplication. Randomly selected elements are a good example of this. If you add elements to an array one at a time using the random function, it can return the same value more than once.

Removing any duplicates is easy … just use array_unique().

$newarray=array_unique($oldarray);

Any time to identical entries are identified, the second one will be removed from the array. Remember that this may leave you with an array having missing index values. That is no worry if you are going through the array using the foreach() function, but if you want to use for($x=0;$x<12;++$x) you will have problems.

You can fix that by reordering your array keys using array_values():

$correctarray=array_values($newarray);

This will take the $newarray created in the earlier line, and replace the key values with new ones, starting at zero and proceeding sequentially.


Sometimes when coding a PHP script, you find you need to manipulate strings of text or data. Part of that process may require that you find a particular part of a string, containing known characters. The strpos function lets you find the location of one string within another.

The basic format is:

$pos=strpos($mystring,$searchstring,$start);

First, remember that PHP begins counting with zero, so the first character in a string is the zero position, the second is one, etc. Confusing, but practical, as we will see. So in our sample, $pos is the position (an integer number) returned by the function; $mystring is the string you will be searching; $searchstring is the string you will be searching for, and $start is an optional offset value, that begins the search at a particular location within the string.

With no $start, the default is, logically enough zero — which means the first character of the string. So if you had a web page HTML code in a variable called $page, for example, and you want to find where the page code starts, ignoring the header, you might search for:

$body=strpos($page,"<body");

Then later, when looking for a link, you could begin your search in the body of the page, ignoring the header:

$linkpos=strpos($page,"<a ",$body);

Tricky Part

If you want to use the results of the strpos() function in a logical statement, you need to be careful. Suppose you want stop execution after the last link on the page is found:

$linkpos=strpos($page,"<a ",$body);
if ($linkpos==false){exit;}else{ some code here }

But if there is a link at the very start of the $page variable (suppose after each link you find you truncate the $page variable at the end of that link, to speed search time) — the strpos function will return zero if the $page string starts with <a — and zero is interpreted as false in the if() construct.

PHP has a solution though — instead of using the == comparison operator, you need to use three equal signs, for a logical comparison. That does not evaluate zero as false, only a variable set to false will evaluate properly. So we use:

if ($linkpos===false){exit;}else{ some code here }

The string position function is a hand tool in your PHP toolkit, just remember to start counting at zero!


If you want an easy way to make money with PHP, even before you learn all of the ins and outs of programming scripts of your own, there are some handy resources available to help make your website profitable. Easy Niche Store is one of those.

This Easy Niche Store review will explain how it works. First, you need either an eBay store, or you need to become an eBay affiliate. Next, order Easy Niche Store and read the simple directions. There are a few variables you need to personalize, such as your eBay store number or your eBay affiliate ID. If you don’t know how to find those, see the instructions that come with Easy Niche Store.

Next just use a simple PHP line in your web page where you want the Easy Niche Store results to be displayed:

include('easynichestore.php');

That can go in a side-bar, at the bottom of a page, or even devote an entire page just to your Easy Niche Store. Then, whenever anyone clicks on one of the eBay auction or store items you can earn money! It is just that easy, and really works great. Believe me, I wouldn’t write this Easy Niche Store review if I weren’t 100% sure that you can earn more income by using it.

Several years ago I wrote a similar, but simpler, script to display eBay auction items on my web page. It was too successful! I had to remove it because too many people left my site, never to return from eBay… Back then there was no eBay affiliate program available, and these listings were not my own, so I didn’t profit by it. The script just took potential customers away.

But with Easy Niche Store, you won’t mind losing visitors when you see how many more sales you make from your eBay store, or you see your affiliate earnings from eBay. That’s monetization — turning visitors into a source of income — and the goal of every profitable website. Offer your visitors hot items from eBay by using this PHP script, and you will be happy you read my Easy Niche Store review — I guarantee it!


One of the most powerful functions in PHP is the simple file_get_contents(). With it, you can load an entire file, text, image or whatever type you want, into a variable for later display or manipulation. Better yet, that file can be anywhere on the Internet, just use an URL instead of file path, and it will return a web page.

$work = @file_get_contents('http://www.ranchocorrecaminos.com/index.htm');

The @ symbol before the command tells PHP not to send error messages. You use that when the PHP code is being executed on a web page intended for public viewing. If you are retrieving the file for your own use, you leave the @ out so that you will be informed if an error occurs.

This simple command is the basis for most PHP scrapers, bots and dynamic aggregation sites. Simple, yet very powerful!


One of the most common programming functions we need to use in PHP is the simple FOR() loop, which repeats a particular section of code several times. You can make it repeat the code of fixed number of times, or just keep looping through until a particular condition is met. Here is the basic format:

for ( initialize a counter; conditional statement; increment a counter)
{
do this code;
}

This loop, for example, will loop through nine times, with $x value set to 1, 2, 3, 4, 5, 6, 7, 8 and 9. The $x=1 sets $x initially to 1. The $x++ increments $x after each time through the loop. $x<10 checks at the beginning of each loop, and if true, executes the code again. When no longer true (i.e. $x is equal to 10) the execution continues after the last curly bracket of the loop.

for ($x=1; $x<10; $x++)
{
do this code;
}

So, if you want to meet some arbitrary condition that is changed within the code inside the loop, you just change $x>10 to a different variable and value, say $y>0 — but be careful! It is easy to get into an ‘infinite loop’ if your conditional statement is always true. With PHP, that means the code will continue to run until you reach the maximum time allotted to any one routine, and then will end with an error message — usually not a good thing!