0

I am trying to work out how to write php functions, so that I don't have to keep writing a block of code over and over again, I am having a play around and tried writing this, but it doesnt work for some reason, can anyone tell me where I am going wrong ?.

<?php
$greeting 'Hello';

function frank ()
{
$name = 'frank';
$2nd = 'robson';
echo $greeting;
echo $name;
echo $2nd;
}


echo frank()
?>
Iain Simpson
  • 441
  • 4
  • 13
  • 29
  • Either [return the value you want to echo from the function](http://uk3.php.net/manual/en/functions.returning-values.php), or just call `frank()` (no `echo` beforehand). Also, you're missing an `=` from the first line and a `;` from the last. – cmbuckley Jan 09 '12 at 09:46
  • Iain, see my answer. I explained. – tekknolagi Jan 09 '12 at 09:51
  • also, can't have a variable name start with a number ($2nd is invalid) – sqram Jan 09 '12 at 10:33

6 Answers6

3

Instead of echoing inside your function you should consider returning a string with it. Also prefer passing a parameter instead of using global scope :

$greeting = 'Hello';

function frank ($funcGreetings)
{
    $name   = 'frank';
    $second = 'robson';
    //Concat variable together an return them
    return $funcGreetings.' '.$name.' '.$second;
}


echo frank($greeting);

Finally a variable name start with a letter or an underscore so $2nd is not a valid name.
See this this link for more info on naming variable

grunk
  • 14,718
  • 15
  • 67
  • 108
2

where I am going wrong ?.

Several places.

but it doesnt work for some reason

That's not an error message - PHP will give you meaningful messages about stuff it doesn't understand if you configure it properly.

If you are getting an error message then you should have included it in your question.

<?php
$greeting 'Hello';

Here's the first error - which will cause a parse failure. There should be an assignment operator in there e.g.

$greeting = 'Hello';

The next error is here:

$2nd = 'robson';

Variables names must begin with an underscore or letter - not a number

echo frank()

2 errors here.

The statement is not terminated with a ; - it may still parse OK but is very messy.

Also the frank function doesn't return a value to echo.

symcbean
  • 47,736
  • 6
  • 59
  • 94
1

Firstly, you need to pass in the $greeting variable for it to be in the functions scope - see the PHP manual for variable scope. It is best avoid using globals as they are harder to debug and test.

Secondly, you have missed out the assignment for the $greeting variable.

Thirdly, you have forgotten to return the value from the function so it can be echod out. See the PHP manual for functions.

Fourth, $2nd is not a valid variable name. Variables must start with a letter. See the relevant manual page.

$greeting = 'Hello';

function frank ($greeting) {
    $return = '';
    $name = 'frank';
    $second = 'robson';
    $return .= $greeting;
    $return .= $name;
    $return .= $second;
    return $return;
}

echo frank($greeting);
Treffynnon
  • 21,365
  • 6
  • 65
  • 98
  • Explain the downvote please. This is a perfectly good answer to the question. – Treffynnon Jan 09 '12 at 09:49
  • Hi, is that passing the $greeting into the function ?, one thing im confused with too is how to pass something defined outside a function into a function, so if inside the function I have $a + $b , and outside the function $a = 2 and $b = 2 , how do I pass these to use inside the variable ?. – Iain Simpson Jan 09 '12 at 10:13
  • This is described on the following manual pages: http://www.php.net/manual/en/functions.user-defined.php and http://www.php.net/manual/en/functions.arguments.php – Treffynnon Jan 09 '12 at 10:48
1

I see, you that you want to function which will return something string. You can pass variable as an argument. Things after return statement will be what you get by calling frank()

$greeting = 'Hello';
function frank ( $greeting) {
    $name = 'frank';
    $second = 'robson';
    return  $greeting." ".$name." ".$second;
}

echo frank ( $greeting );  

Or if you want to use global variable in the function you must declared this by a global statement

$greeting = 'Hello';
function frank ( $greeting) {
        global $greeting;
        $name = 'frank';
        $second = 'robson';
        return  $greeting." ".$name." ".$second; // 
    }
echo frank ( $greeting );  
abuduba
  • 4,986
  • 7
  • 26
  • 43
1

You have to return the string as you are using your function with echo

So, do something like this

//this is inside frank
$ret = $greeting.$name.$2nd;
return $ret;

Second, $greeting is a variable declared outside the function definition. So, you should not use it inside the function definition.

You function will now look like this

function frank ()
{
$greeting = 'hello';
$name = 'frank';
$2nd = 'robson';
$ret = $greeting.$name.$2nd;
return $ret;
}

Also, there is an error in the first line. $greeting 'Hello' is incorrect. It should be $greeting = 'Hello' (You are missing the = sign).

Ankit
  • 6,772
  • 11
  • 48
  • 84
0
<?php
$greeting = 'Hello';

function frank ()
{
  global $greeting;
  $name = 'frank';
  $2nd = 'robson';
  echo $greeting;
  echo $name;
  echo $2nd;
}


frank();
?>

You need to SET the variable and you need SEMICOLONS.

tekknolagi
  • 10,663
  • 24
  • 75
  • 119