TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-15-2008, 05:59 PM   #1 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Confused User defined function with 'return' statement -- not working

This example is from a book that I'm using to learn, but I can't get it to work properly! What am I doing wrong here? I may have copied something wrong, but I've spend a couple of hours trying to figure it out...?

php Code:
// Charge for an item.
   $nCharge = 100.00 ;
   
   // Define the function.
   function salestax($nCharge, $nShip = .10)
   {
      $nYourtotal = ($nCharge + ($nCharge * $nShip));
      return $nYourtotal ;
   }

   // Invoke the function...
   salestax($nCharge) ;

   // Print the results...
   print $nYourtotal ;

Thanks for any help...
Dave
Dave is offline  
Reply With Quote
Old 04-15-2008, 06:09 PM   #2 (permalink)
The Acquainted
 
wGEric's Avatar
 
Join Date: Nov 2007
Posts: 166
Thanks: 0
wGEric is on a distinguished road
Default

PHP Code:
<?php
   
// Charge for an item.
   
$nCharge 100.00 ;
   
   
// Define the function.
   
function salestax($nCharge$nShip .10)
   {
      
$nYourtotal = ($nCharge + ($nCharge $nShip));
      return 
$nYourtotal ;
   }

   
// Invoke the function...
   
$nYourtotal salestax($nCharge) ;

   
// Print the results...
   
print $nYourtotal ;

?>
You are returning the variable correctly. All you needed to do was assign the variable when you call the function. It was returning the value but it wasn't being stored or used in anyway.
__________________
Eric
wGEric is offline  
Reply With Quote
The Following User Says Thank You to wGEric For This Useful Post:
Dave (04-15-2008)
Old 04-15-2008, 06:12 PM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

The only segment that I have modified is assigned the value of the salestax function to the $nYourtotal variable. When you issue the return in your custom function, that simply returns data back, and so you can either assign (as in our case), print (as we do later on when we print), or do nothing with (as you were doing previously).

php Code:
// Charge for an item.
$nCharge = 100.00 ;

// Define the function.
function salestax($nCharge, $nShip = .10)
{
    $nYourtotal = ($nCharge + ($nCharge * $nShip));
    return $nYourtotal ;
}

// Invoke the function...
$nYourtotal = salestax($nCharge) ;

// Print the results...
print $nYourtotal ;
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
Dave (04-15-2008)
Old 04-15-2008, 07:15 PM   #4 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default

Thanks to you both, Eric and Wildhoney! I see now that I am blocking on a basic concept related to UDFs, which I will write about (ask about!) in a different post.

Dave
Dave is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 03:07 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design