Jump to content

[SOLVED] function help


wrathican

Recommended Posts

i have never created a function before and i have read a few tutorials on them, but when i tried it, it didnt work.

 

when you tell a function what variables to accept can you put '$_POST['value']??

 

because i am getting this error:

Parse error: syntax error, unexpected '[', expecting ')' in F:\wamp\www\test\index.php on line 4

 

and here is my line 4:

function

function sendbooking($_POST['name'],$_POST['email'],$_POST['house'],$_POST['street'],$_POST['town'],$_POST['postcode'],$_POST['home'], $_POST['mobile'],$_POST['tour'],$_POST['price'],$_POST['day'],$_POST['month'],$_POST['year'],$_POST['experience'],$_POST['health'])

Link to comment
https://forums.phpfreaks.com/topic/61351-solved-function-help/
Share on other sites

I think you might be a little mistaken with what you are doing with the functions. The names of the variables that you put inside the function definition are not necessarily the names of the variables that you wish to pass into the function. They are simply the names of the variables that will be used in the function.

 

So, to pass variables from the POST array into a function, you would do something along the lines of:

 

<?php
function showname($name){
	echo $name;
}
showname($_POST['name']);
?>

Link to comment
https://forums.phpfreaks.com/topic/61351-solved-function-help/#findComment-305297
Share on other sites

no you cannot.

 

function sendbooking(name,email)

{

$name = $_POST['name'];

$email = $_POST['email'];

}

etc.

 

 

Much better of not using globals within functions, this would limit the function to only be able to work with $_POST.

 

Something like....

 

<?php

 function foo($a,$b) {
   return "You passed $a and $b to the function";
 }

 echo foo($_POST['name'],$_POST['email']);

?>

 

is alot more usefull.

Link to comment
https://forums.phpfreaks.com/topic/61351-solved-function-help/#findComment-305301
Share on other sites

Archived

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

×
×
  • 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.