LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-06-2004, 04:34 AM   #1
gaddargarson
LQ Newbie
 
Registered: Sep 2004
Location: izmir
Distribution: mandrake,GBL,fedora core,slackware
Posts: 26

Rep: Reputation: 15
functions in PHP


how can i get the variable which is the result of a function?
for example;

<?php
$number1=10;
$number2=20;
$number3=50;
function calc ($number1,$number2) {result1=$number1 * $number2;return $result1;}
function finalcalc ($result1,$number3) {result2=$result1 + $number3;return $result2;}
print calc( $number1,$number2);
print "<br>";
print finalcalc($result1,$number3);
?>



Why i can't see result2,although i see result1?
How can i get $result1 from the function?
------------------------------------------------------------------------------------------------
I have another question that how can i change the variables from browser(one by one or alltogether)?(especially in this example)
 
Old 10-06-2004, 05:04 AM   #2
masand
LQ Guru
 
Registered: May 2003
Location: INDIA
Distribution: Ubuntu, Solaris,CentOS
Posts: 5,522

Rep: Reputation: 69
hi there


try using

echo $result1

in ur function

regards
 
Old 10-06-2004, 05:19 AM   #3
gamehack
Member
 
Registered: Jun 2003
Location: Sevenoaks, UK
Distribution: Ubuntu
Posts: 183

Rep: Reputation: 30
PHP Code:
<?php
$number1
=10;
$number2=20;
$number3=50;
function 
calc ($number1,$number2) {result1=$number1 $number2;return $result1;}
function 
finalcalc ($result1,$number3) {result2=$result1 $number3;return $result2;}
$result1 calc$number1,$number2);
echo 
$result1;
echo 
"<br>";
echo 
finalcalc($result1,$number3);
?>
In the function calc, $result1 is not exported because it's a member variable of the functions, and the scope is only in the function body. And after the functions finished, it's automatically unset. So that code should do the thing.

Cheers,
gamehack
 
Old 10-07-2004, 02:02 PM   #4
gaddargarson
LQ Newbie
 
Registered: Sep 2004
Location: izmir
Distribution: mandrake,GBL,fedora core,slackware
Posts: 26

Original Poster
Rep: Reputation: 15
thank you gamehack for your explanation

i understood the mistake.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
php and gd (image functions) devfreak Linux - Software 1 05-11-2005 05:17 PM
PHP about functions djgerbavore Programming 1 11-25-2004 07:14 PM
php: array with functions ldp Programming 7 09-22-2004 04:55 PM
PHP -- How to execute a shell script from PHP using FTP functions?? zoonalex Programming 3 07-29-2004 11:51 AM
PHP date functions cmfarley19 Programming 1 10-17-2003 08:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration