Iain Simpson
Iain Simpson

Reputation: 469

Writing a function in php

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()
?>

Upvotes: 0

Views: 1688

Answers (6)

abuduba
abuduba

Reputation: 5042

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 );  

Upvotes: 1

symcbean
symcbean

Reputation: 48357

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.

Upvotes: 2

Treffynnon
Treffynnon

Reputation: 21553

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);

Upvotes: 1

grunk
grunk

Reputation: 14938

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

Upvotes: 3

Ankit
Ankit

Reputation: 6980

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).

Upvotes: 1

tekknolagi
tekknolagi

Reputation: 11012

<?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.

Upvotes: 0

Related Questions