PDA

View Full Version : Functions and Variables



smithster
08-06-2008, 11:13 AM
I've been reading through about passing variables to functions and I can only find references to using globals but I heard that was a bad idea.

Here's an example script...


function add_to_database()
{
$query = "INSERT INTO table (name,age) VALUES ('".$name."', '".$age."')";
mysql_query($query);
}
function send_mail()
{
//using the same varibales to send an email
}
if (isset($_POST['submit']))
{
$name = $_POST['name'];
$age = $_POST['age'];
add_to_database();
send_mail();
}
else
{
//the form bit
}

The project I'm working on, I'd like to make use of a lot of functions. Most will require the same variables but all variables are based on user input.

I did read about using the define constant function but my variables are not constant so I don't think it would be any good to me.

Any ideas?

Thanks in advance.

Smithster.

Jesdisciple
08-06-2008, 03:19 PM
The PHP Manual is at http://www.php.net/manual/en/index.php; please refer to it for your next question. I would strongly prefer that you read through it, but some people might not take such a suggestion seriously.

The specific page you're needing is http://www.php.net/manual/en/functions.arguments.php.

smithster
08-06-2008, 06:30 PM
Well I'd never read through that part of the php manual before, but I did read up on function scopes yesterday at php.net and it talked about using globals which does get this working. But as I said, I've heard it's a bad idea and so want to learn to do it the right way. The problem is that what I have just read I don't understand! I even read through all the user comments to see if there was something there that might give me a clue.

I know a lot of members come across a problem and they post it asking for a solution. Yes I am asking for a solution to my problem but I really have researched it first!! I've spent hours pulling my hair out with this one, so tempted to just take the risk and use globals without knowing what the risks are!!! I have read through dynamic drive forums as well as other forums and also gone back to basics at w3schools!! I've also searched in the search engines but can't find anything that explains this in a way that I can understand!

Please if anyone has an understanding of a safe way to pass variables to a function, show some compassion and help a poor soul out!!!

Thanks in advance

Smithster.

techietim
08-06-2008, 06:32 PM
There's a time to use functions, and a time to not...


function add_to_database($name, $age)
{
$query = "INSERT INTO table (name,age) VALUES ('".$name."', '".$age."')";
mysql_query($query);
}
function send_mail($name, $age)
{
//using the same varibales to send an email
}
if (isset($_POST['submit']))
{
$name = $_POST['name'];
$age = $_POST['age'];
add_to_database($name, $age);
send_mail($name, $age);
}
else
{
//the form bit
}

smithster
08-06-2008, 06:39 PM
lol thanks for that Tim. Much appreciated.


There's a time to use functions, and a time to not...

The time to not use functions, would that be while I have no clue how to properly use them!!!

I was advised to learn to use them because I tend to repeat parts in my scripts a few times and I heard that I could create a function and have it called on whenever I needed that part of the script. The above coding is not part of the script I am working on, just a quick example I put together to keep things easy and simple, especially for others who may need help in this area.

Jesdisciple
08-06-2008, 06:43 PM
Tim beat me to the Submit button, but here's my reply.


function foo($bar){
echo 'Hello, ' . $bar . '!';
}
foo('world');That assigns $bar = 'world'; for the duration of foo, and ultimately prints 'Hello, world!'.

By the way, global variables aren't absolutely evil. But if you really want to avoid them, you can do something like this:
function main($name, $age){
addtodatabase($name, $age);
send_mail($name, $age);
}
if($_POST['submit']){
main($_POST['name'], $_POST['age']);
}

Twey
08-06-2008, 09:52 PM
Programming style 101:Do not use global variables in your functions. Keep functions pure if possible (that is, written in such a way that calling them twice with the same parameters will always return the same result). Some functions must be impure, but they should be very-top-level wrappers over the pure functions handling only the impure parts of the process. Generalise generalise generalise! PHP isn't a very nice language for more general programming, but a large function can almost always be broken down into several very general functions and then built up with minor amounts of glue. See, for example, array_map (http://www.php.net/array-map)() and friends. Don't be afraid to create functions. Function-calling overhead is minimal, and you might need it later — especially if it's particularly general.

See also: A Simple PHP Tutorial (http://www.php.net/tut.php).

tech_support
08-22-2008, 12:47 PM
There's a time to use functions, and a time to not...


function add_to_database($name, $age)
{
$query = "INSERT INTO table (name,age) VALUES ('".$name."', '".$age."')";
mysql_query($query);
}
function send_mail($name, $age)
{
//using the same varibales to send an email
}
if (isset($_POST['submit']))
{
$name = $_POST['name'];
$age = $_POST['age'];
add_to_database($name, $age);
send_mail($name, $age);
}
else
{
//the form bit
}


Oooh, I see a MySQL injection attack (http://www.google.com/search?q=mysql+injection) happening in a matter of time! Please for the love of whoever, escape your strings for safe MySQL usage! This prevents nasty hackers adding DELETE DATABASE whatever; in $_POST['name']; $_POST['age'] or whatever value you're passing on.

Read more about the escape function here. (http://php.net/mysql_real_escape_string)