Jump to content


Photo

learning exercise...


  • Please log in to reply
7 replies to this topic

#1 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 19 June 2006 - 04:39 PM

I don't understand the result this gives:
<?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;
?>

Here is the result:
428225
428275

total: 0
Shouldn't total be like 50 ???

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 19 June 2006 - 04:58 PM

You're not returning a value from the function, you're just echoing it.

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

Ken

#3 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 19 June 2006 - 05:26 PM

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
you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

#4 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 19 June 2006 - 06:14 PM

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...

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 19 June 2006 - 07:33 PM

Surely you would of read something up about return when you learnt how to create functions?

#6 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 20 June 2006 - 11:43 AM

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...

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 20 June 2006 - 11:54 AM

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

#8 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 20 June 2006 - 01:02 PM

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) View Post[/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]





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users