13 thoughts on “Using functions in PHP?

  1. user

    Honestly, Sam, you may want to pick up a PHP book and read up some before asking for help on sites like these. This is about as basic as it gets.

  2. user

    @Gumbo: I edited out the " you added to his code; if that's how he posted it and there's answers referring to the missing quote, then it takes everything out of context to fix the mistake in the question.

  3. user

    This site is for programming questions, not necessarily 'advanced' programming question. Sam's question was on topic and I don't think it should be downvoted because it's an easy answer.

  4. user

    It is a big problem. This question have their answer everywhere in the web, so you don't need another place to be. At the contrary, SO allows you to solve problem you have usually trouble to deal with or to discover things. If you let questions like this one to be asked, then you will add a lot of noise in SO and the interesting stuff will be lost in the mess (while the basic stuff won't be best answered as anywhere else). Finally SO would become as interesting as any fora in the Web.

  5. user

    I didn't downvote this and I'm all for beginner questions here – I just got done defending another one of his questions when someone said it was too simple – my advice was more towards his own personal journey towards being a better programmer. Coming here to ask questions like these displays a severe lack of understanding basic programming concepts. In the spirit of that, he's probably best served reading a book to get an idea of how languages work, instead of generating Yet More Bad PHP Code.

  6. user

    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:

    function sam(){ 
      echo "Hello World!"; 
    // 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;
  7. user

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


    class newInstance {
        function helloWorld(){
          echo 'Hello world';
    $object = new newInstance();
  8. user

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


Leave a Reply

Your email address will not be published.