473,651 Members | 2,525 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_global s 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_descript ion){

$pos = strpos($the_ite ms_description, $search_for);
if ($the_items_des cription == "") {
$does_contain_t ext = true;
} else {
if ($pos === false) {
$does_contain_t ext = false;
} else {
$does_contain_t ext = true;
}
}
echo ("before leaving <BR>");
echo ("$does_contain _text");
echo ("<br>");
}
Jul 17 '05 #1
6 2757
its the $does_contain_t ext 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_global s 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_t ext = 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_t ext; 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_t ext = 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_global s 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_descript ion){

$pos = strpos($the_ite ms_description, $search_for);
if ($the_items_des cription == "") {
$does_contain_t ext = true;
} else {
if ($pos === false) {
$does_contain_t ext = false;
} else {
$does_contain_t ext = true;
}
}
echo ("before leaving <BR>");
echo ("$does_contain _text");
echo ("<br>");
}


why not return the variable you need? something like:

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

$what_i_want = myfunc('var1val ue','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_t ext;" or using the
$_GLOBALS['does_contain_t ext'] 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
1863
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 starting my new one. In that time, I have decided to learn C#. I picked up the book, "Programming C# (4th Edition)" recently and have read most of it. I've written about 1,500 lines of C# now, and I've run into the first really ugly thing I...
17
2445
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 but what method would I use to load the entries from the xml file into memory and make them quickly accessible globally in the web code? Just need to know what functions specifically I should start reading about! Thx
42
1579
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 ( seperate boxes), Width in feet and inches (seperate boxes), and thickness in feet and inches (all so in seperate boxes). I haven't quit figured out the split function yet to use a single text box for each. Anyways, when I enter data in each box;...
27
2801
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 recent thread. I want to know why this descrimination is in place. Can't all the variables be initialised to NULL automatically by the compiler? This would make programming a little easier.
10
7228
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 within results.php, MySQL structure seems to work as expected, although i get some problems: As soon as i click on the "Page 2" Link, i get nothing, neither results nor page numbers.
6
3219
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 automatic non volatile variables of the function invoking setjmp, these will be undefined if modified after the setjmp call"
9
8644
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 Su Gatlin, casual mention was made about using static variables as an alternative to using global variables. This caused me to think of the following: '-----Begin module code
10
1820
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
29342
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 called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
0
8275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8792
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8694
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8571
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7294
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5605
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4280
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2696
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 we have to send another system
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.