Jump to content

Home

PHP Question II: Attack of the Ignorant Programmers


Joetheeskimo

Recommended Posts

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*

Link to comment
Share on other sites

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. :\\

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...