473,503 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Variables do not keep values after leaving function

i set a variable while im in a function, but it does not keep its value
when returning to the place the function was called. is this something to
do with register_globals being off? is there anyway i can make the
variable keep its value when returning to the code where it branched from?
TIA (again)

function textsearch($the_items_description){

$pos = strpos($the_items_description, $search_for);
if ($the_items_description == "") {
$does_contain_text = true;
} else {
if ($pos === false) {
$does_contain_text = false;
} else {
$does_contain_text = true;
}
}
echo ("before leaving <BR>");
echo ("$does_contain_text");
echo ("<br>");
}
Jul 17 '05 #1
6 2750
its the $does_contain_text variable that i want to keep the val of

Jul 17 '05 #2
In article <pa***************************@hotmail.com>,
ma*************@hotmail.com says...
i set a variable while im in a function, but it does not keep its value
when returning to the place the function was called. is this something to
do with register_globals being off? is there anyway i can make the
variable keep its value when returning to the code where it branched from?
TIA (again)


global $varname;
--
**************************************
The Eldritch Dark:
Dedicated to Clark Ashton Smith
http://www.eldritchdark.com/
Jul 17 '05 #3
i fixed it, i just put $does_contain_text = true; at the start of the
file. thanks anyway .
Jul 17 '05 #4
i realised that what i tried wasn't working because it was always
evaluating to true, for some reason after global $does_contain_text; or
whatever my variable was called, it say's there's a parse error on the
first line that tried to assign the variable - $does_contain_text = true;
Jul 17 '05 #5
Matthew Robinson <ma*************@hotmail.com> wrote:
i set a variable while im in a function, but it does not keep its value
when returning to the place the function was called. is this something to
do with register_globals being off? is there anyway i can make the
variable keep its value when returning to the code where it branched from?
TIA (again)

function textsearch($the_items_description){

$pos = strpos($the_items_description, $search_for);
if ($the_items_description == "") {
$does_contain_text = true;
} else {
if ($pos === false) {
$does_contain_text = false;
} else {
$does_contain_text = true;
}
}
echo ("before leaving <BR>");
echo ("$does_contain_text");
echo ("<br>");
}


why not return the variable you need? something like:

function myfunc($var1,$var2) {
// lines of code
$var_of_intrest = ...
return $var_of_intrest;
}

$what_i_want = myfunc('var1value','var2value');

note that, in your code, you are not passing $search_for to your
function but you do use it. if you do not pass it, you need to put
global $search_for
inside your function.

also, your if/else construction is strange...

maybe it is a good idea to read the beginning of the manual at least?

steven.
Jul 17 '05 #6
In the function, it considers that it's a local variable unless told
otherwise, so you have to declare it as a global one: either by
putting in the function "global $does_contain_text;" or using the
$_GLOBALS['does_contain_text'] variable. Read
http://www.php.net/manual/en/languag...bles.scope.php for more
informations about it.
Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
1849
by: Jeff Grills | last post by:
I am an experienced C++ programmer with over 12 years of development, and I think I know C++ quite well. I'm changing jobs at the moment, and I have about a month between leaving my last job and...
17
2432
by: Davíð Þórisson | last post by:
now in my web I have some global variables to be used in many different subpages, in the old ASP I simply loaded a variables.asp file into memory using the eval() function. Now I'd like to use XML...
42
1543
by: Dooglo | last post by:
I'm new VB and programming all together, but I'm getting he hang of it. My question is; I'm writting a program to figure square feet and yards when the user inputs "Length in feet and inch (...
27
2778
by: Madhav | last post by:
Hi all, I did not understand why do the global vars are initialized to NULL where as the block level variables have random values? I know that the C standard requires this as was mentioned in a...
10
7214
by: EOZyo | last post by:
Hi, i'm trying to set pagination for a search i run on my website, i'll try to explain the easiest i can: When i click the search button on search.php, data is received and stored in variables...
6
3205
by: Clausfor | last post by:
Hello, I have a problem with restoring variables in the setjmp/longjmp functions: K&R2 for longjmp says: "Accessible objects have the same value they had when longjmp was called, except for...
9
8611
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
10
1808
by: pod | last post by:
Hello Using ANSI C... The following code is causing a bit of bother.... UINT16 x, y, z = 0 Would people expect x and y and z to be set to 0 ?
1
29305
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
0
7074
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7273
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6982
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5572
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4667
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1501
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.