The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > Web Programming > PHP


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 08-07-2007, 09:46 AM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 616
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
[SOLVED]get value from function

Hey,

I know theres some PHP geeks here

I have a simple question that i cant find an answer for.

example function:
PHP Code:
function test_func()
{
//Do some things here
$Value "Result in here"

I then want to echo the result to the page but thats where i can not get things to work, what do i have to do to get this value to echo?

An example would really help me then i can see what i need to do or if i am doing something wrong?

Thanks,
Lee.
Reply With Quote
  #2 (permalink)  
Old 08-07-2007, 09:54 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,111
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Sorry, I don't really understand what you're trying to do, but why don't you just do this:

PHP Code:
function test_func()
{
//Do some things here
$Value "Result in here"
echo $value;
}  
test_func(); 

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #3 (permalink)  
Old 08-07-2007, 10:04 AM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 616
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
Sorry for being unclear, i meant echo value to be an example the real use of this will be used within another functions like:
PHP Code:
func_two($value
Sorry for being unclear.
Reply With Quote
  #4 (permalink)  
Old 08-07-2007, 10:07 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,111
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Try this one:

PHP Code:
function test_func($value)
{
echo 
$value;
}  
$value "Hello World";
test_func($value); 
Hopefully that helps

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by HelloWorld : 08-07-2007 at 10:11 AM. Reason: Make it more similar
Reply With Quote
  #5 (permalink)  
Old 08-07-2007, 10:14 AM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 616
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
I will give more infomation, here is my current code:
PHP Code:
    function GetSubject()
    {
        
$reason $_POST['reason'];
        
$subject "Noobio Contact $reason";
    }

//When i run i call by using
GetSubject(); 
Once that code is run i want to be able to use $subject in the mail function:
mail()

Where as using echo will write it to the page.
Reply With Quote
  #6 (permalink)  
Old 08-07-2007, 12:48 PM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Staff*
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 428
iTrader: (0)
TeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enough
PHP functions are very much like functions in other languages with the exception that you don't have to declare the return type.

Let's say that I have a function called getSubject that does the processing to determine the subject of an email and I want to 1) use it in the subject line of an email and 2) return to the end user the results. This code could look like this:
PHP Code:
<?php
function getSubject() {
  
//process the subject and store it in a variable, $subject;
  
return $subject;
}

//Send the email.  notice that since $subject is returned by getSubject(), it's value is being passed directly to the mail function
mail ($to,getSubject(),$body);

//Confirm to user:

echo "Your email to $to has been sent.  The subject was <u>".getSubject()."</u> and the body was <textarea style=\"width:100%;height:8em;\">$body</textarea>";
?>
Now, that calls the function twice which may be undesirable, so you could also do this:
PHP Code:
<?php
function getSubject() {
  
//process the subject and store it in a variable, $subject;
  
return $subject;
}

$calculated_subject getSubject();

//Send the email.
mail ($to,$calculated_subject,$body);

//Confirm to user:

echo "Your email to $to has been sent.  The subject was <u>".$calculated_subject."</u> and the body was <textarea style=\"width:100%;height:8em;\">$body</textarea>";
?>
There are a few other ways of achieving the same task, but I think this addresses your question.

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #7 (permalink)  
Old 08-07-2007, 02:52 PM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 616
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
See i knew it was something simple but i just couldnt think of it, i am not experinced with php as you may have guessed.

The first one would have done but i am thinking when it comes to my full written script that i will use it 2 times so that 2nd one is just perfect, i cant believe i didnt think of that.

Thanks very much.
Lee.
Reply With Quote
Reply


Thread Tools
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 -7. The time now is 12:55 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50