Jump to content

[SOLVED] php function,define


desithugg

Recommended Posts

//$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);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.