<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PHP Programming 101</title>
	<atom:link href="http://phpprogramming101.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://phpprogramming101.com</link>
	<description>Your basic PHP tutorial site.</description>
	<lastBuildDate>Thu, 18 Aug 2011 14:36:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Page Not Found!!!!</title>
		<link>http://phpprogramming101.com/information/page-not-found/</link>
		<comments>http://phpprogramming101.com/information/page-not-found/#comments</comments>
		<pubDate>Thu, 18 Aug 2011 14:36:39 +0000</pubDate>
		<dc:creator>Conrad</dc:creator>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[Computers And Technology]]></category>
		<category><![CDATA[Information]]></category>
		<category><![CDATA[Internet And Businesses Online]]></category>

		<guid isPermaLink="false">http://phpprogramming101.com/?p=281</guid>
		<description><![CDATA[The annoying result of clicking on an image or link that presents nothing that can sway potential readers from continuing to explore a page. This would take a bit more reviewing of the code and other content but it ensures reliability and functionality enough to get your followers the information they need when they need [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://t0.gstatic.com/images?q=tbn:ANd9GcRX0WQaDx0HfesIxlRdYvPCfl4R79HeIbAemFF_8NBoodPyC9B1" align=right alt="" />The annoying result of clicking on an image or link that presents nothing that can sway potential readers from continuing to explore a page. This would take a bit more reviewing of the code and other content but it ensures reliability and functionality enough to get your followers the information they need when they need it. </p>
<p>Sure you cannot anticipate or review them all but a periodic check should always be done to ensure this. On blogs, the <a href="http://www.guidetolatestfirewall.com">short-links</a> may have been erroneously saved when you changed the title of that article you saved as draft then published. Take time to place yourself in the footstep of the user and try out your page to isolate and fix such issues.</p>
]]></content:encoded>
			<wfw:commentRss>http://phpprogramming101.com/information/page-not-found/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Print Style Sheets</title>
		<link>http://phpprogramming101.com/information/print-style-sheets/</link>
		<comments>http://phpprogramming101.com/information/print-style-sheets/#comments</comments>
		<pubDate>Wed, 18 May 2011 14:33:43 +0000</pubDate>
		<dc:creator>Conrad</dc:creator>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[Computers And Technology]]></category>
		<category><![CDATA[Information]]></category>
		<category><![CDATA[Internet And Businesses Online]]></category>

		<guid isPermaLink="false">http://phpprogramming101.com/?p=279</guid>
		<description><![CDATA[It may not be essential for a page to function but users of content / blogging sites might want to print your content would greatly appreciate it. May it be for review later or uploading to their e-book for reading on the go, having this function provides a portability factor to your content allowing it [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://t2.gstatic.com/images?q=tbn:ANd9GcQ0v9MOOgi0R1WBF_uH43FVEZ_ooDGF918viltP-qv-rZxynlel" align=right alt="" />It may not be essential for a page to function but users of <a href="http://www.latestlaptopreview.com">content / blogging</a> sites might want to print your content would greatly appreciate it. May it be for review later or uploading to their e-book for reading on the go, having this function provides a portability factor to your content allowing it to go farther.</p>
<p>Yes, plagiarism is issues all over the internet but hey, if you don’t want that information to be used then why place it into the internet. We all know that anything on the internet is fair game for everyone so having a decent site with decent followers can be insurance enough to avoid the issue.</p>
]]></content:encoded>
			<wfw:commentRss>http://phpprogramming101.com/information/print-style-sheets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>History – a story of breadcrumbs</title>
		<link>http://phpprogramming101.com/information/history-%e2%80%93-a-story-of-breadcrumbs/</link>
		<comments>http://phpprogramming101.com/information/history-%e2%80%93-a-story-of-breadcrumbs/#comments</comments>
		<pubDate>Mon, 18 Apr 2011 14:32:11 +0000</pubDate>
		<dc:creator>Conrad</dc:creator>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[Computers And Technology]]></category>
		<category><![CDATA[Information]]></category>
		<category><![CDATA[Internet And Businesses Online]]></category>

		<guid isPermaLink="false">http://phpprogramming101.com/?p=277</guid>
		<description><![CDATA[We all know the fairytale Hansel and Gretel where two kids use breadcrumbs they left along the way to get back home. That would greatly help users of your page to get a sense of control of where they are on your page. Most pages fail to do this preventing proper usage or not maximizing [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://t3.gstatic.com/images?q=tbn:ANd9GcQslXjB0VW5lwl-zN3DxUaHHZKBvCJO8UJD7sOFIg89cOWdfZU3" align=right alt="" />We all know the fairytale Hansel and Gretel where two kids use breadcrumbs they left along the way to get back home. That would greatly help users of your <a href="http://www.phpprogrammingtips.com">page</a> to get a sense of control of where they are on your page. Most pages fail to do this preventing proper usage or not maximizing all the potential information your user can get from your page.</p>
<p>Control allows users to view through a simple “Home > About > Sub Page Title” line where they are at and should they want to get back to the second tier of the page, a simple click is all it takes rather than several on the browser history button.</p>
]]></content:encoded>
			<wfw:commentRss>http://phpprogramming101.com/information/history-%e2%80%93-a-story-of-breadcrumbs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Page Registration</title>
		<link>http://phpprogramming101.com/information/page-registration/</link>
		<comments>http://phpprogramming101.com/information/page-registration/#comments</comments>
		<pubDate>Fri, 18 Feb 2011 14:30:01 +0000</pubDate>
		<dc:creator>Conrad</dc:creator>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[Computers And Technology]]></category>
		<category><![CDATA[Information]]></category>

		<guid isPermaLink="false">http://phpprogramming101.com/?p=274</guid>
		<description><![CDATA[This tends to be a no-brainer but some pages do ask a tad bit too much from possible readers/users by asking too many questions. The easiest and simplest way of verification would be the basic username, email and password and you could go a long way with those three. Adding more makes a page/site look [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://t2.gstatic.com/images?q=tbn:ANd9GcT3xPP8kvM2KR8_cD4N7GXcRM_Ec4LUnDZwXpboFOTFJAir31gbBw" align=right alt="" />This tends to be a no-brainer but some pages do ask a tad bit too much from possible readers/users by asking too many questions. The easiest and simplest way of verification would be the <a href="http://www.opensourcedailyjournal.com">basic username</a>, email and password and you could go a long way with those three. Adding more makes a page/site look hostile thus swaying potential users form even getting in.<br />
Imagine trying to get membership into a page that slaps you with a 15 to 25 field questionnaire, discouraging, right?  Keep it simple and effective for all these information once collated can be a lot to handle even for the best of pages and hosts.</p>
]]></content:encoded>
			<wfw:commentRss>http://phpprogramming101.com/information/page-registration/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Array_diff_uassoc Function</title>
		<link>http://phpprogramming101.com/basic-programming/array_diff_uassoc-function/</link>
		<comments>http://phpprogramming101.com/basic-programming/array_diff_uassoc-function/#comments</comments>
		<pubDate>Fri, 10 Dec 2010 18:17:35 +0000</pubDate>
		<dc:creator>Conrad</dc:creator>
				<category><![CDATA[Basic Programming]]></category>
		<category><![CDATA[Sample Code]]></category>
		<category><![CDATA[php array_diff functions]]></category>
		<category><![CDATA[php basics]]></category>

		<guid isPermaLink="false">http://phpprogramming101.com/uncategorized/array_diff_uassoc-function/</guid>
		<description><![CDATA[Next in line is array_diff_uassoc() function which compares two or more arrays while checking for differences before comparing the keys with a user-defined location. It then returns an array withthe keys and values from the first array(to which all the values were comapred against) it the function allows it. Syntax is as follows : array_diff_uassoc(array1,array2,array3&#8230;.,function). [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.mindphp.com/modules.php%3Fname%3DPHP_Manual%26page%3Dfunction.array-diff-uassoc.html"><img src="/wp-content/uploads/scraped/25.jpg"/></a>
<p>Next in line is array_diff_uassoc() function which compares two or more arrays while checking for differences before comparing the keys with a user-defined location. It then returns an array withthe keys and values from the first array(to which all the values were comapred against) it the function allows it. Syntax is as follows : array_diff_uassoc(array1,array2,array3&#8230;.,function). with a sample below of how it is used.</p>
<p><?php<br />
function userdefined($v1,$v2)<br />
{<br />
if ($v1 === $v2)<br />
	{<br />
	return 0;<br />
	}<br />
if ($v1 > $v2)<br />
	{<br />
	return 1;<br />
	}<br />
else<br />
	{<br />
	return -1;<br />
	}<br />
}<br />
$a1=array(0=>&#8221;Dog&#8221;,1=>&#8221;Cat&#8221;,2=>&#8221;Horse&#8221;)<br />
$a2=array(3=>&#8221;Dog&#8221;,1=>&#8221;Cat&#8221;,5=>&#8221;Horse&#8221;)<br />
print_r(array_diff_uassoc($a1,$a2,&#8221;userdefined&#8221;));<br />
?></p>
<p>which results in the following output : Array( [0] => Dog [2] => Horse). For an example of the same function with two or more assigned arrays to the function:</p>
<p><?php<br />
function userdefined($v1,$v2)<br />
{<br />
if ($v1 === $v2)<br />
	{<br />
	return 0;<br />
	}<br />
if ($v1 > $v2)<br />
	{<br />
	return 1;<br />
	}<br />
else<br />
	{<br />
	return -1;<br />
	}<br />
}<br />
$a1=array(0=>&#8221;Dog&#8221;,1=>&#8221;Cat&#8221;,2=>&#8221;Horse&#8221;)<br />
$a2=array(3=>&#8221;Dog&#8221;,1=>&#8221;Cat&#8221;,5=>&#8221;Horse&#8221;)<br />
$a3=array(6=>&#8221;Onyx&#8221;,0=>&#8221;Dog&#8221;,5=>&#8221;Horse&#8221;)<br />
print_r(array_diff_uassoc($a1,$a2,$a3,&#8221;userdefined&#8221;));<br />
?></p>
<p>Which in turn, gives you : Array ([2] => Horse )</p>
<p>So we see the different array_diff function variants and the diffeerent ways they are used to compare the values of one or more arrays with one another.</p>
]]></content:encoded>
			<wfw:commentRss>http://phpprogramming101.com/basic-programming/array_diff_uassoc-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Marketing Your Skills as a Freelance Programmer</title>
		<link>http://phpprogramming101.com/information/marketing-your-skills-as-a-freelance-programmer/</link>
		<comments>http://phpprogramming101.com/information/marketing-your-skills-as-a-freelance-programmer/#comments</comments>
		<pubDate>Thu, 18 Nov 2010 16:59:46 +0000</pubDate>
		<dc:creator>Conrad</dc:creator>
				<category><![CDATA[Computers And Technology]]></category>
		<category><![CDATA[Information]]></category>
		<category><![CDATA[Internet And Businesses Online]]></category>
		<category><![CDATA[Business Solutions]]></category>
		<category><![CDATA[Freelance Jobs]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://phpprogramming101.com/?p=262</guid>
		<description><![CDATA[The popularity of PHP as a solution for websites with dynamic contents has created a huge market for people skilled enough in writing complex applications in PHP, covering a wide range of solutions. If you think you’ve got enough experience under your belt, you’ve probably already considered the idea at least once. However, simply knowing [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://t3.gstatic.com/images?q=tbn:ANd9GcRd0qydz5piFJ8mCpQbaa_9KL6v5XHClAWndvkilYyFi6F-ODVf" align=right alt="" />The popularity of PHP as a solution for websites with dynamic contents has created a huge market for people skilled enough in writing complex applications in PHP, covering a wide range of solutions. If you think you’ve got enough experience under your belt, you’ve probably already considered the idea at least once. However, simply knowing how to write good PHP scripts won’t be enough to make it as a successful <a href="http://www.peopleperhour.com/find/programmer">freelance programmer</a>. This post is a bit more abstract as it doesn’t cover <a href="http://www.peopleperhour.com/find/programmer">programming</a> specifically, but more general ideas that can help you get a better exposure for your skillset.</p>
<p>First, consider your weak points – this is crucial when aiming to be a successful freelancer. With a highly competitive market like the one we have, you simply can’t afford to lack in any qualification when it comes to writing PHP scripts. If your knowledge of database interaction is weak, you’d better grab a book on databases and dig in for the next few weeks – otherwise, that other guy who doesn’t write scripts quite as well as you, but knows MySQL by heart will keep getting the good jobs and you’ll keep treading in the same place.</p>
<p>Also, figure out what exactly you want to offer as a service – the applications of PHP are quite varied and this allows you to specialize in a particular type of design. Figure out what you do best and market yourself to that crowd – it’s always better to offer the type of service you’re best at (something which really shouldn’t even need mentioning, but somehow keeps getting ignored by many people).</p>
]]></content:encoded>
			<wfw:commentRss>http://phpprogramming101.com/information/marketing-your-skills-as-a-freelance-programmer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Good Domain Name (Part 1)</title>
		<link>http://phpprogramming101.com/information/good-domain-name-part-1/</link>
		<comments>http://phpprogramming101.com/information/good-domain-name-part-1/#comments</comments>
		<pubDate>Wed, 10 Nov 2010 16:05:55 +0000</pubDate>
		<dc:creator>Edi</dc:creator>
				<category><![CDATA[Information]]></category>

		<guid isPermaLink="false">http://phpprogramming101.com/uncategorized/good-domain-name-part-1/</guid>
		<description><![CDATA[Before choosing a domain name, consider the following: 1. Domain Name as your website Name It is important to name your site after your domain name so that when people think of your website, they’ll remember it by name. If it is also your URL then it is much more easier to remember. Your domain [...]]]></description>
			<content:encoded><![CDATA[<p><center><a href="http://smg.photobucket.com/albums/v299/karlazhu/?action=view&#038;current=httpWWW.jpg" target="_blank"><img src="http://img.photobucket.com/albums/v299/karlazhu/httpWWW.jpg" border="0" alt="Photobucket"></a></center></p>
<p>Before choosing a domain name, consider the following:<br />
1. Domain Name as your website Name<br />
It is important to name your site after your domain name so that when people think of your website, they’ll remember it by name. If it is also your URL then it is much more easier to remember. Your domain name should reflect your site or business.</p>
<p>2. Brand Name Domains<br />
Domain name that matches your brand name is better as usually if people are thinking of buying something they already have a particular brand in mind.</p>
<p>3. Hyphenated Names<br />
Hyphens in websites are usually easier to forget. But if you really want a particular domain name but it is already taken, with a hyphen you can shave the domain name you want.</p>
]]></content:encoded>
			<wfw:commentRss>http://phpprogramming101.com/information/good-domain-name-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Export Data From MySql to CSV</title>
		<link>http://phpprogramming101.com/php/how-to-export-data-from-mysql-to-csv/</link>
		<comments>http://phpprogramming101.com/php/how-to-export-data-from-mysql-to-csv/#comments</comments>
		<pubDate>Sun, 10 Oct 2010 05:12:39 +0000</pubDate>
		<dc:creator>Edi</dc:creator>
				<category><![CDATA[Codes]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://phpprogramming101.com/uncategorized/how-to-export-data-from-mysql-to-csv/</guid>
		<description><![CDATA[This is a simple code that exports mysql database data to csv (delimated) data.]]></description>
			<content:encoded><![CDATA[<p><em></em></p>
<p><img src="http://www.znt.de/_ENGLISH/database.jpg" alt="" /></p>
<p>This is a simple code that exports mysql database  data to csv (delimated)  data.</p>
<p><?php<br />
//a small class for database connectivity<br />
class database {<br />
private $db_handle;<br />
private $user_name;<br />
private $password;<br />
private $data_base;<br />
private $host_name;<br />
private $sql;<br />
private $results;<br />
function __construct($host = “localhost”, $user, $passwd) {<br />
$this->db_handle = mysql_connect($host, $user, $passwd);<br />
}<br />
function dbSelect($db) {<br />
$this->data_base = $db;<br />
if (!mysql_select_db($this->data_base, $this->db_handle)) {<br />
error_log(mysql_error(), 3, “/phplog.err”);<br />
die(”Error connecting to Database”);<br />
}<br />
}<br />
function executeSql($sql_stmt) {<br />
$this->sql = $sql_stmt;<br />
$this->result = mysql_query($this->sql);<br />
}<br />
function returnResults() {<br />
return $this->result;<br />
}<br />
}<br />
//database variables<br />
$host = “localhost”;<br />
$user = “root”;<br />
$passwd = “”;<br />
$db = “bg_db”;<br />
$sql = “SELECT * FROM table ORDER BY id”; // a query to fetch records from database<br />
$dbObject = new database($host, $user, $passwd);<br />
$dbObject->dbSelect($db);<br />
$dbObject->executeSql($sql);<br />
$res = $dbObject->returnResults(); // result reasource<br />
$newFileName = “emp_names.csv”; //file name that you want to create<br />
$fpWrite = fopen($newFileName, “w”); // open file as writable<br />
$nameStr = “”;<br />
$rows = mysql_fetch_assoc($res); // fetching associate records<br />
$sStr = “”;<br />
//first store the fields name as header of csv in $sStr<br />
foreach($rows as $key=>$val) {<br />
$sStr .= $key.”,”;<br />
}<br />
//then store all records<br />
do {<br />
$sStr .= “– —–\n”; //to seprate every record<br />
foreach($rows as $key=>$row) {<br />
$sStr .= $row.”,”;<br />
}<br />
}while($rows = mysql_fetch_assoc($res));<br />
$sStrExp = explode(”– —–”, $sStr);//separate every record<br />
foreach($sStrExp as $val) {<br />
$sStr2 .= rtrim($val, “,”);<br />
}<br />
echo $sStr2;<br />
fwrite($fpWrite, $sStr2); //now write to csv file<br />
fclose($fpWrite);//close file<br />
?></p>
]]></content:encoded>
			<wfw:commentRss>http://phpprogramming101.com/php/how-to-export-data-from-mysql-to-csv/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Random Numbers</title>
		<link>http://phpprogramming101.com/php/random-numbers/</link>
		<comments>http://phpprogramming101.com/php/random-numbers/#comments</comments>
		<pubDate>Fri, 10 Sep 2010 14:14:42 +0000</pubDate>
		<dc:creator>Conrad</dc:creator>
				<category><![CDATA[Basic Programming]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://phpprogramming101.com/php/random-numbers/</guid>
		<description><![CDATA[int rand ( [int min, int max]) int mt_rand ( [int min, int max]) int getrandmax ( ) int mt_getrandmax ( ) void srand ( [int seed]) void mt_srand ( [int seed]) Sometimes you want to take random actions in your code &#8211; it might be to give your web site visitors a different greeting [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://hoppingintopuddles.wordpress.com/2008/03/26/randomness/"><img src="/wp-content/uploads/scraped/34.jpg"/></a>
<p><code><br />
int rand ( [int min, int max])<br />
int mt_rand ( [int min, int max])<br />
int getrandmax ( )<br />
int mt_getrandmax ( )<br />
void srand ( [int seed])<br />
void mt_srand ( [int seed])<br />
</code></p>
<p>Sometimes you want to take random actions in your code &#8211; it might be to give your web site visitors a different greeting each time they visit, you might be programming a game, or you might be trying to secure data by hashing it. Either way, randomisation is simple and helpful thing to remember, and has just two functions: rand(), and mt_rand().</p>
<p>Both functions do the same thing, and both take the same parameters, so what is the difference between the two? Well, rand() is a basic randomisation function that is very quick but not very &#8220;random&#8221; &#8211; the numbers it generates are slightly more predictable. Mt_rand() on the other hand, is more complicated &#8211; the &#8220;mt&#8221; parts means Mersenne Twister, as that is the name of the randomisation algorithm it uses. Mt_rand() returns much more &#8220;random&#8221; numbers, but does so at the expense of some speed.</p>
<p>As mentioned, both functions have the same parameters &#8211; two optional numbers, for the minimum number to return and the maximum number to return. Either you supply no parameters, which will result in PHP returning a random number between one and a very high number, or you can supply the two parameters. Here is an example:<br />
<code><br />
<?php<br />
   $random = rand();<br />
   $randrange = rand(1,10);<br />
   $mtrandrange = mt_rand(1,100);<br />
?><br />
</code></p>
<blockquote><p>Note that the two numbers passed in are inclusive. That is, our $randrange number could be anywhere between 1 and 10 including 1 and 10.
</p></blockquote>
<p>As mentioned, if you do not pass any parameters to your rand() and mt_rand() calls, PHP will generate a random number from 1 to a high number. If you want to find out the maximum number PHP can return from a rand() call, use getrandmax(). There is a similar function, mt_getrandmax() for mt_rand().</p>
<p>Now you know how randomisation works, here is a quick example to show you how you can make use of randomisation to greet web site visitors in various ways:</p>
<p><code><br />
<?php<br />
   switch(rand(1,6)) {<br />
      case 1:<br />
         $greet = 'Hello!'; break;<br />
      case 2:<br />
         $greet = 'Welcome!'; break;<br />
      case 3:<br />
         $greet = 'Greetings!'; break;<br />
      case 4:<br />
         $greet = 'Salutations!'; break;<br />
      case 5:<br />
         $greet = 'Good day!'; break;<br />
      case 6:<br />
         $greet = 'Yo!'; break;<br />
   }</p>
<p>   print $greet;<br />
?><br />
</code></p>
<p>Here we have not bothered assigning the result of rand() to a variable before putting it into the switch statement, but you can do it whichever way is easier for you to read.</p>
<p>One important thing to note is that the speed of randomisation does not depend on the sizes you pass into it &#8211; rand() is just as fast in rand(1,3) as it is in rand(1, 10000000). Mt_rand() works just short of 50% slower than rand(), which means you should only be using it if you particularly need the extra randomisation it brings.</p>
<p>To give you an idea of how fast the two run and how using larger values for randomisation makes no difference, try this script:</p>
<p><code><br />
<?php<br />
   $START = time();<br />
   for ($i = 1; $i < 1000000; ++$i) {<br />
      $j = rand(1,100);<br />
   }<br />
   $END = time() - $START;<br />
   print "Short rand() took $END seconds\n";</p>
<p>   $START = time();<br />
   for ($i = 1; $i < 1000000; ++$i) {<br />
      $j = mt_rand(1,100);<br />
   }<br />
   $END = time() - $START;<br />
   print "Short mt_rand() took $END seconds\n";</p>
<p>   $START = time();<br />
   for ($i = 1; $i < 1000000; ++$i) {<br />
      $j = rand(1,10000000);<br />
   }<br />
   $END = time() - $START;<br />
   print "Long rand() took $END seconds\n";</p>
<p>   $START = time();<br />
   for ($i = 1; $i < 1000000; ++$i) {<br />
      $j = mt_rand(1,10000000);<br />
   }<br />
   $END = time() - $START;<br />
   print "Long mt_rand() took $END seconds\n";<br />
?><br />
</code></p>
<p>Most random number generators require &#8220;seeding&#8221; &#8211; initialising with a starting value &#8211; because the numbers they generate are not truly random. Instead, they are known as pseudo-random, meaning that they appear to be random. The seed value is used to generate the first number, the first number is used to generate the second number, the second for the third, etc, meaning that if you always supply the same seed value you will always get the same string of &#8220;random&#8221; numbers. This is actually advantageous. Many years ago there was a popular game called Elite available on the BBC Micro, where the player was allowed to fly around a large universe of eight galaxies, each with thousands of star systems. Each star system had a very precise number of planets, a distinct economy situation, etc, and yet the entire universe fit into just 22K of memory. How was this possible? Simple: by providing the same seed to their random number generator, the exact same universe could be generated each time.</p>
<p>Of course, this is a fairly rare situation. More often than not you will want numbers that look random as opposed to numbers that are always the same, and this is where random seeding comes in. If you provide a random number to the random number generator as its seed, you will have a new and original string of random numbers coming out. Does this sound like a chicken and egg situation to you? That is, how do we get the random number to provide to the random number generator? Well, think of what randomness &#8211; usually called entropy &#8211; you can draw upon in your scripts.</p>
<ul>
<li>The number of files in your temp directory?</li>
<li> The number of rows in your database?</li>
<li>The time your script was called?</li>
</ul>
<p>Of all three of these, the latter is potentially the most random &#8211; you do not control when your script is called, and you are certainly never likely to have the same script called in the exact same microsecond, so you could use the return value from microtime() as your initial random seed.</p>
<p>The seed function for rand() is srand(), and it takes one parameter &#8211; an integer to use as the seed value. If you are using mt_rand(), you should use mt_srand() for seeding. If you recall from earlier, microtime() returns a floating-point number &#8211; this is no good for use as the parameter to srand() (or mt_srand() &#8211; it is exactly the same), so you need to make it into an integer before use.</p>
<p>Now, as we know that microtime() returns the time in millionths of a second, we can convert its return value to an integer by multiplying it by a million, like this:</p>
<p><code>srand((double)microtime()*1000000);</code></p>
<p>The code above should seed the standard random number generator fairly well. You can do the same for the Mersenne Twister generator with this code:</p>
<p><code>mt_srand((double)microtime()*1000000 );</code></p>
<p>If you want your random number to always generate the same string of numbers, simply supply a seed that is a known value. For example, no matter how often you run it, this next script will always generate the same &#8220;random&#8221; numbers:</p>
<p><code><br />
<?php<br />
   mt_srand(123456);<br />
   echo mt_rand(1, 100), "\n";<br />
   echo mt_rand(1, 100), "\n";<br />
   echo mt_rand(1, 100), "\n";<br />
?><br />
</code><br />
The last option is just to call srand()/mt_srand() without any parameters at all. In this situation, PHP will attempt to generate a random seed for you &#8211; not much good if you want reliably random numbers or if you have a particularly good source of entropy for your seed value, but generally good enough for most people.</p>
<p>As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is done automatically. </p>
<p><a href="http://hudzilla.org/phpwiki/index.php?title=Generating_random_numbers">Source</a></p>
]]></content:encoded>
			<wfw:commentRss>http://phpprogramming101.com/php/random-numbers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Array Chunk Function</title>
		<link>http://phpprogramming101.com/basic-programming/array-chunk-function/</link>
		<comments>http://phpprogramming101.com/basic-programming/array-chunk-function/#comments</comments>
		<pubDate>Tue, 10 Aug 2010 11:14:57 +0000</pubDate>
		<dc:creator>Conrad</dc:creator>
				<category><![CDATA[Basic Programming]]></category>
		<category><![CDATA[Sample Code]]></category>
		<category><![CDATA[Array manipulation]]></category>
		<category><![CDATA[Programming PHP]]></category>

		<guid isPermaLink="false">http://phpprogramming101.com/uncategorized/array-chunk-function/</guid>
		<description><![CDATA[The array_chunk() function on the on the other hand as the name implies, divides an array into chunks or several tables from the source table. The syntax goes something like array_chunk(array,size,preserve_key), wherein the array is the table that would be divided, the size is the number of elements which the new arrays are to contain [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://smallcode.weblogs.us/2006/07/"><img src="/wp-content/uploads/scraped/20.jpg"/></a>
<p>The array_chunk() function on the on the other hand as the name implies, divides an array into chunks or several tables from the source table. The syntax goes something like array_chunk(array,size,preserve_key), wherein the array is the table that would be divided, the size is the number of elements which the new arrays are to contain and the preserve key which can either be true or false is used to either retain or revise the key or pointer value of the original table. An example is shown below:</p>
<p><?php<br />
$a=array(�a�=>�Cat�, �b�=>�Dog�, �c�=>�Horse�,�d�=>�Cow�);<br />
print_r(array_chunk($a,2);<br />
?></p>
<p>The code would have an output of:</p>
<p>Array (<br />
[0] => Array ( [0] = > Cat    [1] => Dog )<br />
[1] => Array ( [0] => Horse [1] => Cow )<br />
)</p>
<p>As we can see, the original array has been divided into two arrays array0 and array1 and a value that is not given for the key had it assigned a new key for each of the tables. Another example would be :</p>
<p><?php<br />
$a=array(�a�=>�Cat�, �b�=>�Dog�, �c�=>�Horse�,�d�=>�Cow�);<br />
print_r(array_chunk($a,2,true);<br />
?></p>
<p>This would then give us ;<br />
Array (<br />
[0] => Array ( [a] = > Cat    [b] => Dog )<br />
[1] => Array ( [c] => Horse [d] => Cow )<br />
)</p>
<p>This shows the significance of the retain key field wherein the two new arrays retained their original keys. The reverse of which would be the array_combine() which divided the array into one which holds the keys and one with the values.</p>
]]></content:encoded>
			<wfw:commentRss>http://phpprogramming101.com/basic-programming/array-chunk-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

