Okay! I admit, the title may seem misleading to some but you’ll soon see my point of view. You see, Einstein once said, “God doesn’t play dice.”. Although, he said this to express his views on quantum mechanics, but what he meant was that it doesn’t matter how unpredictable or random anything seems to us, but for God, it’s always foreseeable. This means that you can know of all the possible outcomes of a future event, but only God knows before hand what actual outcome will occur. That outcome might be his own will, who knows. (Yeah right!, God). Now keeping this in mind, I’ve created a simple page, where you may ask any question you like and you’ll receive a random “YES” or “NO” answer. Check it out:
There a few things that you need to know when checking this out:
- It’s a page created using only HTML and PHP, i.e. there’s no MySQL/Database involved.
- Since there’s no database involved, whatever you ask is completely safe and personal since it doesn’t get stored anywhere.
- It’s a completely ad free page, thanks to Hostinger.com. Hostinger.com is a website which provides free domains and free hosting for all. Which is great if you have a small website or if you just wish to learn PHP on a real server.
rand(), is a built in math function in PHP. It returns a random value between two integers, i.e. if the min and max values are defined. For example:
echo rand(5, 15);
will generate a random number between 5 and 15. For our askgod website we’ll use this function, but first we need to create a form where the user can type in his/her question and the be redirected to the main PHP page from there. You can play with the form and design whatever you like. But for me, I just kept it simple and used the below code:
Notice that there is no “get” or “post” attribute. That’s because I’m not collecting any information. If however you wish save all the questions being asked by your users, then you may add the superglobals to collect the form-data. The above code will just redirect the user to the page ans.php, where he/she may get the answer to his question from “GOD”.
Now, on the ans.php, since I wasn’t going to use MySQL, I created a list of variables having fixed yes/no values within the code itself. If you’ll go to this page and check the source code, by right clicking, and selecting “view page source”, you’ll be able to see all the values. they’ll look like this:
The list goes till 70. Although you may just use 2 variable, since that’ll still give you a 50-50 fair chance result, but I wanted it to look more difficult. It’s same like when in casinos, dealers at the table use more than one deck of cards, so to make the calculations difficult for card counters. To get even more fair result, I’ve assigned “yes” “no” alternatively till 50 and then in pairs of 10 till 70.
To have a bit of more fun, I’ve also added variables containing quotes from Buddha, to appear in the result along with the answer. This gives a feeling of some hidden message when the user gets his answer. There are 60 such quotes. The quotes also appear randomly and in the source code look like:
The real fun starts now. We create two variables $num1 and $num2, to have values containing any random number between 1 and 70, and 1 and 60. Next we create a variable $ans which will contain the value obtained by randomly selecting from yes-no strings, and also a variable $qut which will contain a value obtained by randomly selecting from the quotes. Check the below code for better understanding:
Random function code
The value that will be assigned to the $ans variable will be $str.(random number between 1 and 70), say $str21. Similarly $qut may get a value of $qt25.
Now all that is left is to echo these variables in a nicely formatted HTML page at asuitable place and you are done. Be sure to check out the askgod.url.ph website and it’s source code for better understanding.
With the rand() function, you can not only create such fun pages but also do a lot more. Practically, the rand() function is used to display random banners on a web page or banner rotation.
The reason I’m not explaining here how banner rotation is done is because you can easily learn it from this about.com tutorial. Everything is explained perfectly. Don’t forget to check it out.
If you have any questions or feedback, feel free to comment below.
We are a Website Development, WebApp Development, and Digital Marketing Company, providing services to enterprises of all shapes and sizes, across the world. In our blog, we talk about the latest in Tech, Technical Tutorials, and our general opinions, among other things 🙂 Please feel free to reach us through the comment box or via the contact us page if you’d like to know more about our services.