DamienRoche Posted September 23, 2008 Share Posted September 23, 2008 So I thought I'd get to using php in a more OO way. Created my first function and read up on variable scope. What I couldn't find the answer to was how to use a variable if it was created within a function..or even if this is possible. So here's a basic function to illustrate this: function addthis(){ $num1 = 2; $num2 = 3; $total = $num1 + $num2; return $total; } addthis(); echo "$total"; This doesn't output anything and no doubt has something to do with variable scope. But I can't get my head around it. How do I define a variable in a function and then use it outside that function? I've tried the global thing in a few places, but still nothing. Hopefully someone can shed some light on this. Thanks. Link to comment https://forums.phpfreaks.com/topic/125495-creating-variable-within-function-using-it-outside-of-function/ Share on other sites More sharing options...
discomatt Posted September 23, 2008 Share Posted September 23, 2008 This isn't OO(P). Functions != Objects. Read up here: http://php.net/manual/en/language.oop5.basic.php But here's your solution function addthis(){ $num1 = 2; $num2 = 3; $total = $num1 + $num2; return $total; } $total = addthis(); echo "$total"; Variables inside of functions are completely separate from variables outside, and vice versa. You can change this by using the global keyword, but it's not considered good coding practise Link to comment https://forums.phpfreaks.com/topic/125495-creating-variable-within-function-using-it-outside-of-function/#findComment-648775 Share on other sites More sharing options...
DamienRoche Posted September 23, 2008 Author Share Posted September 23, 2008 Thanks for clearing up my OO misconception. Onto to the problem. I know I can turn that functions output into a variable. but what if I have several variables I want to use within that function. Am I trying to do something I should be classes or objects? Basically, I have some code at the beginning of a few pages, very specific to that page..they simply define a few variables that are used throughout the page. What I want to know now is how can put these snippets of code into a *master file* and simply include the file in each page and refer to a snippet of code. I'm trying to enclose these snippets with something. I was trying to use functions...but obviously that won't work. Can I use classes to do this? Thanks again for the help. Link to comment https://forums.phpfreaks.com/topic/125495-creating-variable-within-function-using-it-outside-of-function/#findComment-648782 Share on other sites More sharing options...
discomatt Posted September 23, 2008 Share Posted September 23, 2008 Couple ways to do this... pass by reference, and using a class (OOP) <pre><?php function byReference( $int, & $total ) { $total = $int + 5; } $globalTotal = 12; echo "\$globalTotal = $globalTotal before byReference()\n"; byReference( 10, $globalTotal ); echo "\$globalTotal = $globalTotal after byReference()\n"; class someClass { public $total = 12; public function add5 ( $int ) { $this->total = $int + 5; } } $obj = new someClass; echo "\$obj->total = {$obj->total} before add5()\n"; $obj->add5( 10 ); echo "\$obj->total = {$obj->total} after add5()\n"; ?></pre> Link to comment https://forums.phpfreaks.com/topic/125495-creating-variable-within-function-using-it-outside-of-function/#findComment-648834 Share on other sites More sharing options...
jonsjava Posted September 23, 2008 Share Posted September 23, 2008 rather than using a class. Here's a *slight* mod on your script: <?php function addthis(){ $num1 = 2; $num2 = 3; $total = $num1 + $num2; return $total; } $total = addthis(); echo $total; You'll never see me OOP. I'm a procedural guy. I can think in procedural. Never could OOP think. Link to comment https://forums.phpfreaks.com/topic/125495-creating-variable-within-function-using-it-outside-of-function/#findComment-648836 Share on other sites More sharing options...
jonsjava Posted September 23, 2008 Share Posted September 23, 2008 oh, and to handle an array output from a function, here's another version of the same thing: <?php function addthis(){ $count = 5; while ($count > 1){ $number1 = rand(1,100); $number2 = rand(1000,2000); $finals[] = $number1 + $number2; $count--; } return $finals; } $total = addthis(); print_r($total); Link to comment https://forums.phpfreaks.com/topic/125495-creating-variable-within-function-using-it-outside-of-function/#findComment-648849 Share on other sites More sharing options...
discomatt Posted September 23, 2008 Share Posted September 23, 2008 But objects are the way of the awesome.... And yes, returning an array would be 'better' than passing by reference ( in a scope sense anyways )... Link to comment https://forums.phpfreaks.com/topic/125495-creating-variable-within-function-using-it-outside-of-function/#findComment-648857 Share on other sites More sharing options...
jonsjava Posted September 23, 2008 Share Posted September 23, 2008 a well-written object will run much faster than a function that does the same thing. With that said, you gotta decide if you need the speed. If it's a simple function to do something like I wrote, then you prolly don't need the time difference. I know how to OOP, but I hate doing it. It doesn't come to me as rapidly as procedural code. Link to comment https://forums.phpfreaks.com/topic/125495-creating-variable-within-function-using-it-outside-of-function/#findComment-648862 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.