Jump to content

Archived

This topic is now archived and is closed to further replies.

AV1611

learning exercise...

Recommended Posts

I don't understand the result this gives:
[code]
<?php
function now()
{
$row=gettimeofday();
echo $row['usec'];
}
$start=now();
echo "<br/><br/>";
$stop=now();
echo "<br/><br/>";
$total=$stop-$start;
echo "<br/><br/>";
echo "total: ".$total;
?>
[/code]

Here is the result:
[code]
428225
428275

total: 0
[/code]
Shouldn't total be like 50 ???

Share this post


Link to post
Share on other sites
You're not returning a value from the function, you're just echoing it.

Try:
[code]<?php
function now()
{
    $row=gettimeofday();
    return($row['usec']);
}
?>[/code]

Ken

Share this post


Link to post
Share on other sites
I have figured it out I think..I am a noobie and decided to try and answer this one from a "lack of knowledge" perspective

the function:-

function now()
{
$row=gettimeofday();
echo $row['usec'];
}

does not actually return a value that can be assigned to $start and $stop, all it is asked to do is to write out the value of 'usec'

function now()
{
$row=gettimeofday();
echo $row['usec'];
return $row['usec'];
}

makes it work fine

laugh at me all you like, I am new and am trying my best.......

Paul

Share this post


Link to post
Share on other sites
Hmmm... I think I just found something new to learn about...

I've never used "return"

Self taught people miss stuff like that sometimes...

Thanks, all...

Share this post


Link to post
Share on other sites
Surely you would of read something up about return when you learnt how to create functions?

Share this post


Link to post
Share on other sites
as I stated earlier wildteen, this was my very first lesson, but I couldn't get any traction from what php.net had on their site... I had never used "return" before, so didnt' know of it's existance...

// I learn out of necessity - I have written many full blown applications that work very good... But, I'm sure if someone with a better PHP education were to look at my code, they would call it very bulky, repetitious and bloated... But, it does work, and it works very well...

I am now trying to figure out how to be more EFFICIENT... and someone pointed out to me that I need to learn is how to do functions, as NONE of my programs have them, which would explain why my code is bulky, repetitous, and bloated...

I added this to give you context as to what seemed like a dumb question on the outside, but really wasn't dumb at all...

Share this post


Link to post
Share on other sites
Which area in php.net did you look at. The [a href=\"http://www.php.net/manual/en/language.functions.php\" target=\"_blank\"]section on Functions[/a] has a very clearly section on [a href=\"http://www.php.net/manual/en/functions.returning-values.php\" target=\"_blank\"]returning values[/a].

Also, if you have programmed in any other language, most have some sort of mechanism for returning values from a called routine back to the calling routine.

Ken

Share this post


Link to post
Share on other sites
A. Thanks, I guess I missed it...

B. I have no other programming language experience.


[!--quoteo(post=385976:date=Jun 20 2006, 07:54 AM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Jun 20 2006, 07:54 AM) [snapback]385976[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Which area in php.net did you look at. The [a href=\"http://www.php.net/manual/en/language.functions.php\" target=\"_blank\"]section on Functions[/a] has a very clearly section on [a href=\"http://www.php.net/manual/en/functions.returning-values.php\" target=\"_blank\"]returning values[/a].

Also, if you have programmed in any other language, most have some sort of mechanism for returning values from a called routine back to the calling routine.

Ken
[/quote]

Share this post


Link to post
Share on other sites

×

Important Information

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.