desithugg Posted March 24, 2007 Share Posted March 24, 2007 //$user_id = "1"; define("user", $user_id); function battle() { echo user; } battle(); I want to make it so the variable/constant user is defined for every function by default but the above didnt work can i do anything instead of having to do the fallowing each time function battle($id) { echo $id; } battle($user_id); Quote Link to comment Share on other sites More sharing options...
Koobi Posted March 24, 2007 Share Posted March 24, 2007 well it won't work because you've commented out $user_id. try uncommenting it. also, you don't have to surround the value for $user_id in quotes. - if you have to use quotes, use single quotes where possible. - PHP doesn't bother too much with the 'type' (integer, string, boolean, array, etc) of a value. when you surround a value in quotes, the reader would assume you're trying to convert it to a string. There's no need to convert a number to a string. In your case, PHP would handle the number 1 as a string anyway (correct me if i'm wrong, anyone) so there's really no need for the quotes. Quote Link to comment Share on other sites More sharing options...
per1os Posted March 24, 2007 Share Posted March 24, 2007 You can do this: $user_id = 1; function battle() { global $user_id; echo $user_id; } battle(); Quote Link to comment Share on other sites More sharing options...
desithugg Posted March 24, 2007 Author Share Posted March 24, 2007 Well actually the comment was just there to tell you guys what the variable $user_id contains and you're right there's no need for the quotes the real $user_id variable is fetched from the database, when i try to echo it outside of a function like echo $user_id; //it outputs 1 (my id in the database) but when i do this function something() { echo $user_id; or $query = mysql_query("SELECT * from users where id = '$user_id'") or die(mysql_error()); $row = mysql_fetch_array( $query ); $email = $row["email"]; //the query is just an example i have really long codes and it might be too much to read } but yeah it doesn't work withing functions and I didn't test this before so it will take me too long to change all the files and functions plus i have to do them by web ftp since my computer is being stupid and wont allow ftp clients like smart ftp or anything... so yeah I tried to define it as user too but that still didnt work...so any idea? Quote Link to comment Share on other sites More sharing options...
desithugg Posted March 24, 2007 Author Share Posted March 24, 2007 You can do this: $user_id = 1; function battle() { global $user_id; echo $id; } battle(); oooh thank god your a life saver lol i would have way too much stuff to edit if I didn't find out a way but yeah thanks alot Quote Link to comment Share on other sites More sharing options...
Koobi Posted March 25, 2007 Share Posted March 25, 2007 it's odd that it didn't work for you. i'm not sure why unless you made a typo because it is valid code and the following worked for me (i tried it out the other day before i made my first post in this thread): <?php $user_id = 1; define("user", $user_id); function battle() { echo user; } battle(); ?> if your file structure is such that your function is in one file and you define $user_id in another file, it's possible that you may not be including them properly. anyway, i'm glad to see you've got it working. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.