tags:

views:

305

answers:

7

How do I simply call a function by using PHP?

Say I have created:

function sam(){
 echo "Hello World!;
 exit();
}

How would I initiate the function?

+13  A: 

Like this -

sam();
Jez
very well put. straight to the point! +1
Peter Perháč
A: 

Well, i suppose:

sam();

Kuroki Kaze
+8  A: 

Read the PHP manual about functions. And note that exit will not just exit the function but the whole script. So nothing else will be executed after this call.

Gumbo
+15  A: 

Is this a trick question?

First thing I notice is you are missing a quote:

function sam(){ echo "Hello World!; exit(); }

.. should be ..

function sam(){ echo "Hello World!"; exit(); }

(I see you fixed your post after I mentioned the above by adding the quote).

Next, in your case, the function echo's the result, and then exits the entire script (it doesn't just exit the function as you may be assuming), so not much you can do with that other than this:

<?php

function sam(){ 
  echo "Hello World!"; 
  exit(); 
}

sam();
// anything here on down will not execute or render!

?>

However, if your function was more real-world, like this:

function sam() {
  return "Hello World!";
}

.. then you could do any of these (and more) ..

$s = sam();
echo $s;
$s = "Say it: " . sam();
echo $s;
OneNerd
A: 

`< ?php

sam();

?>`

help yourself to php.net

Gos
This is a lame answer, but it was honestly what first came to my mind when I read the question. People need to go to a little effort to find answers in their language's official documentation before they give up and decide to take it to Stack Overflow.
Phantom Watson
A: 

I've just started using OOP techniques in PHP, and one simple bit of information that I found hard to find online was that if you want to call your function as part of the object you need to use $this->functionName();

i.e.

class newInstance {
    function helloWorld(){
      echo 'Hello world';
    }
}

$object = new newInstance();
$object->helloWorld;
Matt
A: 

In addition to the other answers here, I think your intention for using exit() is actually satisfied by the return construct. More info on returning values.

However, in your example there is no reason to have it since PHP functions return void automatically once the function ends and no other return value has been specified.

An example where you would want to use return to stop a function from continuing:

function foo($bar) {
  if ($bar < 10) {
    return;
  }

  return $bar * 10;
}
print foo(1); // Prints nothing
print foo(20); // Prints 200

I couldn't tell you what real-world application this particular example has but it shows a useful situation where you may want to cut a function short.

Another example showing that return is not needed in simple functions:

function foo($bar) {
  print $bar * 10;
}

foo(20); // Prints 200
print foo(20); // Also prints 200, but since the function does not return anything, the print statement on this line does nothing.

You may also want to checkout the PHP.net manual section on Functions. Hope this helps.

Mike B