Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Tuesday, 10 December 2013

Random Number generation in php

Random Number generation in php
Random Number generation in php programming language. At some point of programming in php you are going to need of generate a random numbers. Let see how to create a random number then how to give lower and upper limit of generatingrandom number. When we use rand function we can supply nonearguments otherwise we can supply two arguments.
Code for random number generation (Screenshot1):-
Random Number Generation
Random Number Generation (Screenshot1)
1
2
3
4
5
6
<?php
 
$rand = rand();
echo $rand();
 
?>
Output (Screenshot2, Screenshot3) :-
Random Number Generation
Random Number Generation (Screenshot2)
Random Number Generation
Random Number Generation (Screenshot3)
Every time you refresh we can see examples it will automaticallygenerates random number with its lower limit which is 0 or 1 to upper limit which is predefined. Now rand function is already inbuilt-ed function in php which we can use it. rand functiongenerates random number which is unique so every-time yourefresh it will automatically reset.
Code for random number generation (Screenshot4):-
Random Number Generation
Random Number Generation (Screenshot4)
1
2
3
4
5
6
7
<?php
 
$rand = rand();
$max = getrandmax();
echo $rand().'/'.$max;
 
?>
Output(Screenshot5):-
Random Number Generation
Random Number Generation (Screenshot5)
For rand function maximum upper limit till which you can generate number without passing argument is 32767. As you can find getrandmax function to find maximum generate value of the randfunction. As you can see in example max value of rand functionremains same will refreshing browser and it only generate random numbers.
As we can pass argument to rand function for this i had great idea to create one samll php project play game rolling dice in php script. Its fun use of rand function.
There are other reasons to use rand functions are like when we are uploading file we may want to specify a random array if you like of numbers , characters letters and thing like that. rand function is useful for creating more random value everytime you can also combine letters and characters of your choice

Timestamps in php

Timestamps in php
Timestamps in php programming language  Timestamps are widely used a standard used by php in order to keep track current time and current day in fact we had functions retrieve us current week and months the things like that. timestamps is numeric value or as integer value amount of second from the first of January 1970.
Code for time function (Screenshot1) :-
timestamps
timestamps (Screenshot1)
1
2
3
<?php
echo time();
?>
Output (Screenshot 2,Screenshot3) :-
timestamps
timestamps (Screenshot2)
timestamps
timestamps (Screenshot3)
In this example time is function which echo out seconds from 1st January 1970 to today’s time seconds reached. Functionreturns current unit time stamps. As you can see output every time you refresh it will increase by seconds.So, its very useful to keep track of amount of second or current time depending upon you like if you want to track when the comment is posted when you posted on you post. But its not user-friendly because we are echoing the seconds not in human reader format.
Code for date and time function (Screenshot4):-
timestamps
timestamps (Screenshot4)
1
2
3
4
5
6
7
8
<?php
$time = time();
$actual_time = date('H:i:s', $time);
echo 'The current time is '.$actual_time;
?>
Output (Screenshot5):-
timestamps
timestamps (Screenshot5)
In this example i had created variable time with timestamp and we create actual_time in that we will process date function it takes two argument first how you want to display and we had key for different things to use second arguments here it is timestamp most common way to process first argument to display is H:i:s hour Minute and second. As you will see output it will be same time where in your system time i mean computer time. Timestamps updated dynamically.
Code for date and time function (Screenshot6):-
timestamps
timestamps (Screenshot6)
1
2
3
4
5
6
7
8
<?php
$time = time();
$actual_time = date('D M Y', $time);
echo 'The current time is '.$actual_time;
?>
Output (Screenshot7) :-
timestamps
timestamps (Screenshot7)
In this example its a date month and year for current timestamps which is updating dynamically there are many other methods to getdifferent formats of timestamps this are few examples from it.
Code for date and time function (Screenshot8):-
timestamps
timestamps (Screenshot8)
1
2
3
4
5
6
7
8
<?php
$time = time();
$actual_time = date('D M Y @ H:i:s', $time);
echo 'The current date/time is '.$actual_time;
?>
Output (Screenshot9) :-
timestamps
timestamps (Screenshot9)
In this example its a current date month year and current time you have lot more its just few of them try your self.

Read More Extra Tutorials