OpaQue
Jul 8 2005, 06:17 AM
Today, I came up with this strange PHP behaviour. Just wanted to know if anyone has any suggestions! I make a common variable/function file called config.php. I put in my generally used functions in it. Suppose this is my file CODE <? // -----VARIABLES --- // $a=10,$b....
// -----FUCTIONS--- // function doit() { print "A value is " . $a; } ?>
Here, suppose we execute this file directly. Since A has a global scope, it does work perfectly. But if this same file is imported in another file say, mainfile.php CODE <? // -----VARIABLES --- // $c,$d....
include 'config.php'; // INCLUDED THE CONFIG FILE
// now we call our function doit(); ?>
Here, the $a variable in function doit() does not work. Instead it prints only CODE A value is
INSTEAD OF CODE A value is 10
Reply
Hercco
Jul 8 2005, 01:17 PM
You need to add CODE
global $a;
Inside the function scope. If you have several values you need inside the off-file functions it's handy to make an array so that you don't have to "global XX" everything.
Reply
mastercomputers
Jul 9 2005, 12:31 PM
There's a noticable problem with your script OpaQue, and that is $a is global to the main script execution, but functions each have their own scope and $a is definitely not part of that functions scope, the $a used inside that function is definitely going to be undefined, so I still don't see how this script functions correctly with the correct output for the first test. So all I could suggest is the scope of $a is correct and the programs output is correct, except I have no reason why it worked for the first test. If you did <?php $a = 10; function doit($a) { echo 'A is ' . $a; } doit($a); ?> Then it would work as intended as the global $a is sent to the function, however the function doit($a) is actually a different variable with a different scope, but the results are as intended. Cheers, MC
Reply
jvizueta
Sep 15 2005, 06:33 AM
Another solution: CODE <? // -----VARIABLES --- // $a=10,$b....
// -----FUCTIONS--- // function doit() { print "A value is " . $GLOBALS['a']; } ?>
or as I read in a post above CODE <? // -----VARIABLES --- // $a=10,$b....
// -----FUCTIONS--- // function doit() { global $a; print "A value is " . $a; } ?>
Reply
Umar Shah
Mar 25 2008, 10:08 PM
In PHP you can use the global variables from other files by declaring them as global in the file you want to use. this ensures that the variables are taken from the global scope. You may want to declare a similar variable in the file but some function func1 might still need to access the global copy. this can be achieved by declaring the variable as global in the function. Hope this was helpful.
Reply
Similar Topics
Keywords : php variables included functions variables included files- Magic Quotes And $_files
- (3)
- Random With Functions?
- (4)
Hey! Is it possible to use a random script on functions. Lets say I have created three
functions (Items that a user will win if he defeates a monster). One function is a function named
shield() and another function is named sword() and a third function is named helmet(). Now when a
player defeates a monster, he must be awarded. So.. How can I randomize what item he should get?
something like CODE rand(sheild(), sword(), helmet()); or?
Thanks //Feelay...
How Do I Create And Write To Files?
- creating, writing, deleting files (4)
Hi, Can someone please tell me how to create files and write to them in PHP. I just want to create
a simple file containing text, and then be able to read it or update it. Thanks Alfie...
How To Protect Included Files
- (10)
Hey! How can i make my included files 100% safe. Like if I include a file witht his code..
CODE include "bla.php"; How can i make it 100% safe? I know I must close the php tags
in the included files. but what more =?...
User Authentication Session Handling Problems
- Authorization server variables not staying across pages (14)
This is quite a bit of problem I am facing, and I cannot point exactly where I am going wrong. I
have been lurking around here at the Asta Host forums with regard to login and user authentication
scripts and I have got as far as this: - Starting a session - Registering a session variable -
Using the variable to check if the user is authenticated or not. - Authenticating the user through
MySQL database - Logging of the user, by setting the session variable to un-authenticated I have
been able to achive the following things too that I think is not related to this proble...
Reading Files And Folders
- (1)
I am in the process of writing a small content management system for my niche sites and need to be
able to accomplish the following two items. Any assistance would be appreciated: I have a
directory that has nothing but sub directories in it and I need to be able to identify each
directory name and return them in an option drop down selection menu which will be included in an
identifying URL. ALSO: I am in need of a script that could be included on a returned page
that will read the content of a table and identify various keywords within the content and pl...
Php Long Variables
- How do you make them? (5)
Don't ask why but I need to make a long variable that contains ' " ; and ) meaning I cannot
use: CODE $var=('Test'); It will create an error. Here is an example of
what I am talking about: CODE $var=("I want to use ' and " while allowing
html and not using htmlspecialchars"); Basicly is what I want is a CODE
echo<<<END This is some echo. I can use ' " ) and; without using
htmlspecialchars END; Is there a way to make a simalur code that works with variables? Do you ...
Php File Upload
- About uploading files through php (3)
Right i have done a check for a tutorial on this as well as a question about it but php is not
allowed in the search box. So i thought i'd just ask what i want to know. I have a form which
uploads a file, it refreshes the page, uploads the file and then alerts the user to if the file has
uploaded. To be honest im not sure why i keep getting the error. But here is the code: This is the
form that is used for the user to select the file &fid= " method="POST"> Choose a file to
upload: This is the upload code if ($op ==...
Help To Transfer Files Within Hosting Space Using Php
- (4)
I want to transfer files from one folder to another or an entire folder in my web space. I don't
want to download it and upload it, can I do this using a PHP script. Please help me. I'm just
learning the basics of PHP...
Quickly Create Form Variables
- simple form, variable creation, referer check, safe guard variables (5)
The reason I wanted to share this is I've seen so many people do this with their forms when
using PHP. CODE $username = $_POST['username']; $password =
sha1($_POST['password']); $another_var =
$_POST['another_var']; ... and so on, just imagine if you had a large
number of form inputs, do you really want to create each and every variable name? Why people do
this, is probably due to most of the examples I've seen on the web, that does not show an easier
and much quicker ...
How To Edit Php Files?
- (16)
Hi, You can use frontpage to preview HTML files, but how can I edit and preview PHP files offline.
I'm not well-known with PHP and I see more and more using this script to manage their site....
How To Delete Files When Session Ends
- (4)
Dear Friends I need solution to a problem. The problem is as under: I am creating certain files
(playlist) in server disk when user selects some songs. The files are created in ram format. What I
want to do is to delete these files created during a particular session. Is it possible to do so?
Now I am deleting these files using on Unload event fired by JavaScript. I am using PHP. ...
Deleting Files With PHP
- is this possible? (6)
It is posible for PHP to delete files on a server? If so, how is this possible? Just out of
interest......
PHP And Texts: Need Help With PHP String Functions
- (5)
Hey people! I'm pretty new with PHP and MySQL. I wanna have all the text on my site in
MySQL. But i'm having trouble with the inputing from HTML-forms, like . How to i do it? Can i
use PHP or is it better to use javascript and sorta add htmltags into the tag? I wanna make a
texteditor where i can use bold, italic and that kinda stuff. I'm thankful for any help...
/Jens...
Getting Files From Other Sites
- Using wget (3)
Hello. A while ago, a friend showed me this PHP code (wget) which allows you to get files from other
sites. I was a simple one liner type thing. Since my old host (Nytka) went down, I've lost it,
as has my friend. Do you know anything about wget, and could tell me it?...
Question On Depecrated Tracking Variables In Php
- (2)
It seems I can no longer use the line... it returns an error saying this: Warning: is no
longer supported - please use the track_vars INI directive instead in on line 1 So what do I
replace the track_vars command with? O_o Any help would be appreciated....
Looking for php, variables, included, work, functions, variables, included, files, work
|
|
Searching Video's for php, variables, included, work, functions, variables, included, files, work
|
advertisement
|
|