Jump to content

Creating Variable Within Function. Using it Outside of Function.


DamienRoche

Recommended Posts

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.

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

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.

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>

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.

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);

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.