PDA

View Full Version : PHP Question II: Attack of the Ignorant Programmers


Joetheeskimo
02-20-2005, 06:56 PM
More spoonfeeding for you, matt-- :D
...or whoever else has sufficient knowledge to answer the following question...

Let's say I'm defining a function. As I've already learned, if I want to access variables that have been defined outside of the function, I have to declare them global.
I've created a function to extract all the rows in my users table:

function user_fields() {
$email = $row['email'];
$location = $row['location'];

etc.
}

If I call this function, user_fields(), within my other function, will I be able to use the variables defined within the user_fields() function as if they were local now, instead of global? They are, in a way, defined in the function itself now, aren't they?

*Clenches his teeth and waits for the humiliating one-word answer*

ZBomber
02-20-2005, 06:58 PM
I have no clue about anything PHP, but I think it's global. :D

Joetheeskimo
02-20-2005, 07:08 PM
Originally posted by ZBomber
I have no clue about anything PHP, but I think it's global. :D

What is?

matt--
02-20-2005, 07:26 PM
No.

You can, however, have the function return a value though with the return() statement.

For example:return($row);

Joetheeskimo
02-20-2005, 07:30 PM
Originally posted by matt--
No.

You can have the function return a value though with the return() statement.

For example:return($row);

But the include() function can't?

That's weird...I was under the impression the only difference between require() and include() was how they handled errors. :\\

ZBomber
02-20-2005, 07:31 PM
Originally posted by joetheeskimo5
What is?


I dunno, I wasn't even aware there was more than one thing that could be global.... >_>

matt--
02-20-2005, 07:33 PM
Originally posted by joetheeskimo5
But the include() function can't?

That's weird...I was under the impression the only difference between require() and include() was how they handled errors. :\
What does your question have to do with include/require?

Using include/require produces essentially the same result as if you had pasted the contents of the included file into the first one. There is no value returned.

Joetheeskimo
02-20-2005, 07:35 PM
Originally posted by matt--
What does your question have to do with include/require?

Using include/require produces essentially the same result as if you had pasted the contents of the included file into the first one. There is no value returned.

Sorry, I got return() mixed up with require().
Pretend I didn't say that...

Anyway, do I then have to return every single variable defined in the user_fields() function?

return $email;
return $location;
return $avatar;

etc.


Before I can use them as normal?

matt--
02-20-2005, 07:39 PM
You can only return one value per function, unless you use an array, which your ironically trying to get rid of in this function.

Why not just use the $row array instead of having an extra function?
Originally posted by joetheeskimo5
Sorry, I got return() mixed up with require().
Pretend I didn't say that...

Anyway, do I then have to return every single variable defined in the user_fields() function?

return $email;
return $location;
return $avatar;

etc.


Before I can use them as normal?

ZBomber
02-20-2005, 07:41 PM
Kinda off-topic, should I learn PHP? I know HTML, and I want my site to be good, but what can PHP really do? And where should I learn it?

Joetheeskimo
02-20-2005, 07:47 PM
Originally posted by ZBomber
Kinda off-topic, should I learn PHP? I know HTML, and I want my site to be good, but what can PHP really do? And where should I learn it?

YES.

Nuff said.

Buy a book and teach yourself, twas how I did it :)

Anyway...

Originally posted by matt--
You can only return one value per function, unless you use an array, which your ironically trying to get rid of in this function.

Why not just use the $row array instead of having an extra function?

Ah, good point...:o However, once I return the array, I want to use each of the variables in seperate ways...I assume once I've returned the array, I can do all that

$location = $row['location'];
$avatar = $row['avatar'];

...

stuff?

ZBomber
02-20-2005, 07:51 PM
What would I use it for? I know I can use include to add menus, headers, footers, etc, which is very useful, but what else can I use it for?

Joetheeskimo
02-20-2005, 07:56 PM
Allowing members to join your site and be reg'ed instantly.
Creating a points system on your site.
Allowing user-submitted screenshots, files, and other submissions to be posted up immediately after they fill out a form.
Designing your own site guestbook instead of turning to the crappy free ones out there.
Allowing visitors to post comments on files and design your own smilie/BBCode system for the comments.

The list goes on and on. You can also use it for the simpler, more practical stuff, like displaying certain HTML or plugins depending on what browser the visitor is using or what plugins he has.

Once you get a book on PHP (price range $10-$25) you'll see how many more capabilites it has.

EDIT: Also, a lot of the things above depend on a database, but non-database PHP can do useful things like making forms work to e-mail you or something.

matt--
02-20-2005, 07:57 PM
Yes, you can use the values stored in an array as if they were separate variables.

Originally posted by ZBomber
What would I use it for? I know I can use include to add menus, headers, footers, etc, which is very useful, but what else can I use it for?
Mostly for coding web applications of some type.

Joetheeskimo
02-20-2005, 07:59 PM
Originally posted by matt--
Yes, you can use the values stored in an array as if they were separate variables.

Thanks a ton, man. I think that's just about all the questions I have. *Prints out copy of thread*

matt--
02-20-2005, 08:11 PM
Originally posted by joetheeskimo5
Thanks a ton, man. I think that's just about all the questions I have. *Prints out copy of thread*
Anytime.

If you need any more help, feel free to post, pm, or msg me on irc.