Solved

Retriving a value inside a function

Posted on 2009-05-11
10
194 Views
Last Modified: 2012-05-06
Im trying to retrive a value inside a function, which is set outside, but it doesnt seems to work.

Just for a test, Ive written the code below, any ideas why it doesnt work?

Thanks in advance!!!!
<?php

$testValue = "banannas";
 

function testFunc($x1)

	{

	echo "1-" . $x1 . $testValue . "\n";

	if ($x1 < 10)

		{

		testFunc($x1+1);

		}

	echo "2-" . $x1 . $testValue . "\n";

	}
 
 

testFunc(0);

?>

Open in new window

0
Comment
Question by:tonelm54
  • 5
  • 3
  • 2
10 Comments
 
LVL 12

Expert Comment

by:alien109
ID: 24356991
you'll need to put $testValue into the global scope.
0
 
LVL 12

Expert Comment

by:alien109
ID: 24357001
function testFunc($x1)
{
    global $testValue;
    ... the rest of your function

}
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24357390
As an alternative to declaring the variable as global, you can make it a parameter to the function:
<?php

$testValue = "banannas";

 

function testFunc($x1,$testValue)

        {

        echo "1-" . $x1 . $testValue . "\n";

        if ($x1 < 10)

                {

                testFunc($x1+1);

                }

        echo "2-" . $x1 . $testValue . "\n";

        }

 

 

testFunc(0,$testValue);

?>

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24357413
Sorry, I did not notice the recursive call. It should be like this:
<?php

$testValue = "banannas";

 

function testFunc($x1,$testValue)

        {

        echo "1-" . $x1 . $testValue . "\n";

        if ($x1 < 10)

                {

                testFunc($x1+1,$testValue);

                }

        echo "2-" . $x1 . $testValue . "\n";

        }

 

 

testFunc(0,$testValue);

?>

Open in new window

0
 
LVL 12

Expert Comment

by:alien109
ID: 24357468
go with cxr's approach, if you can. it's just better to limit globally defined variables and using them within functions (IMO).
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:tonelm54
ID: 24358636
Sorry, my example dosnt really show what Im trying to do.

Ill modify my code a bit better...

$x5 should always be incremented by 1, including in every increment, and when it comes back to run after the recursive call, it should +1 to that too.

At the moment the code runs and gives:-
      1-0=1
      1-1=1
      1-2=1
      1-3=1
      1-4=1
      1-5=1
      1-6=1
      1-7=1
      1-8=1
      1-9=1
      1-10=1
      2-10=1
      2-9=1
      2-8=1
      2-7=1
      2-6=1
      2-5=1
      2-4=1
      2-3=1
      2-2=1
      2-1=1
      2-0=1

Where it should be giving:-
      1-0=1
      1-1=2
      1-2=3
      1-3=4
      1-4=5
      1-5=6
      1-6=7
      1-7=8
      1-8=9
      1-9=10
      1-10=11
      2-10=12
      2-9=13
      2-8=14
      2-7=15
      2-6=16
      2-5=17
      2-4=18
      2-3=19
      2-2=20
      2-1=21
      2-0=22
<?php

$testValue = "banannas";

$x5 = 20;
 

function testFunc($x1)

	{

	$x5++;

	echo "1-" . $x1 . $testValue . "=" . $x5 . "\n";

	if ($x1 < 10)

		{

		testFunc($x1+1);

		}

	$x5++;

	echo "2-" . $x1 . $testValue . "=" . $x5 . "\n";

	}
 

$x5 = 0;

testFunc(0);

?>

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24359133
I am confused. What is your question? How to get the $testvalue variable into the function, or how to get the output you describe? What is the purpouse of the "banannas" string, it is not part of your wanted output?

How to transfer variables into the function is described above.

This code will create the output you require:
$count = 1;

for($i=1;$i<3;$i++)

  for($j=0;$j<=10;$j++)

    echo $i.'-'.(($i==2)?10-$j:$j).'='.($count++)."\n";

Open in new window

0
 

Author Comment

by:tonelm54
ID: 24359272
Sorry.

I need to be able to set a variable outside the function, and then call on that variable inside the function, and inside that function use it as a recursive loop.

Any ideas?
0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 24359433
I can think of three options:

1) The suggestion from alien109, define the variable as global within the function.

2) My first suggestion, send the variable into the function as a parameter.

3) Use a class, and define the parameter as a property.

Since we have allready showed you the two first options, I will demonstrate the third. The code below shows a php4 compatible class, it works with both php4 and php5.
class foo {

  function foo($x5,$testValue) {

    $this->x5 = $x5;

    $this->testValue = $testValue;

  }

  function testFunc($x1) {

    echo "1-" . $x1 . $this->testValue . "=" . ($this->x5++) . "\n";

    if ($x1 < 10)

      $this->testFunc($x1+1);

    $this->x5++;

    echo "2-" . $x1 . $this->testValue . "=" . $this->x5 . "\n";

    

  }

}

$testValue = 'banannas';

$f = new foo(1,$testValue);

$f->testFunc(0);

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24359491
It does not count right... remove line 10, and change line 11 into this:

    echo "2-" . $x1 . $this->testValue . "=" . ($this->x5++) . "\n";
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now