Jump to content
xisto Community
karlo

Confused... some php functions that i am confused

Recommended Posts

First of all, if you have answers to the following

Feel free to answer them all.

 

Now, in PHP i know there's microtime, srand, float, array_rand, arrays, foreach, or any other loop functions. i'm really confused. Please help me!

Edited by karlo (see edit history)

Share this post


Link to post
Share on other sites

First of all, if you have answers to the following

 

Feel free to answer them all.

 

Now, in PHP i know there's microtime, srand, float, array_rand, arrays, foreach, or any other loop functions. i'm really confused. Please help me!

54284[/snapback]


microtime() is a funtion that will return the time in seceonds as well with the microseconds.

 

srand() is a function that seeds a random value generator. if you are confused of what a seed and a number generator is.. then just stick to the rand() function.

 

a float is a data type. it is an integer with a decimal point like 1.5, 0.5 and 0.1.

 

array_rand() is a funtion that picks one or more keys. the value returned will be an array with the keys that were randomly picked.

 

An array in PHP is actually an ordered map. A map is a type that maps values to keys. This type is optimized in several ways, so you can use it as a real array, or a list (vector), hashtable (which is an implementation of a map), dictionary, collection, stack, queue and probably more. it's really hard to explain because there are many types of arrays.

 

foreach loop is used with an array. the arguments are used here as a reference to the keys and the values of the array. the loop start from where the pointer is. so if the pointer is at the end of the array, the loop will not work. you have to reset the array.

Share this post


Link to post
Share on other sites

microtime() is a funtion that will return the time in seceonds as well with the microseconds.

 

srand() is a function that seeds a random value generator. if you are confused of what a seed and a number generator is.. then just stick to the rand() function.

 

a float is a data type. it is an integer with a decimal point like 1.5, 0.5 and 0.1.

 

array_rand() is a funtion that picks one or more keys. the value returned will be an array with the keys that were randomly picked.

 

An array in PHP is actually an ordered map. A map is a type that maps values to keys. This type is optimized in several ways, so you can use it as a real array, or a list (vector), hashtable (which is an implementation of a map), dictionary, collection, stack, queue and probably more. it's really hard to explain because there are many types of arrays.

 

foreach loop is used with an array. the arguments are used here as a reference to the keys and the values of the array. the loop start from where the pointer is. so if the pointer is at the end of the array, the loop will not work. you have to reset the array.

54799[/snapback]


Can you give me an example of those array, loops, and randomizing functions? and microtime functions? and also please include a tutorial for those kind of things.

Share this post


Link to post
Share on other sites

Can you give me an example of those array, loops, and randomizing functions? and microtime functions? and also please include a tutorial for those kind of things.

55403[/snapback]


for an array:

$array = array("Key1" => "Value1, "Key2" => "Value2");

there are many types of loops. this is one of them.

 

The for loop. this loops works in a simple way. it sets an initial value for a variable then check it with the condition then if it is true, executes the commands inside the loop and executes a command to change the value of the variable and the loop is ready for another processing.

 

In this example the variable is $x and the condition is $x < 10 and the code we used to change the value of $x is $x++

for ($x = 1; $x < 10; $x++) {    print $x;}

PHP already has a function for generating a random number. the function is rand().

 

Here is a simple function to get the current microseconds. this function is also used to time your script's execution time.

 

function get_microtime() {    list($usec, $sec) = explode(" ", microtime());    return ((float)$usec + (float)$sec); }

Share this post


Link to post
Share on other sites
function get_microtime() {   list($usec, $sec) = explode(" ", microtime());   return ((float)$usec + (float)$sec); }

so what's the list() function? and return() function?

Share this post


Link to post
Share on other sites

list assigns variables as if they were an array. in the get_microtime() function an array was returned from the function explode(). list() then was used to assign variables for the elements of the array.

return() is used to return a certain value from function to a variable. example:

function test () {    $test = "test";        return($test);}//when the function is referenced by  a variable, the value returned will be//the new value of the variable.$string = test();//$string now has a value of "test"

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.