Key Concepts

Review core concepts you need to learn to master this subject

PHP Variable Scope

<?php $x = 6; function scope(){ $y = 7; echo $x; // prints 'undefined variable' global $x; echo $x; // prints 6 echo $y; // prints 7 } scope();

A variable with local scope can only be accessed within the function it is declared. A variable with global scope can be accessed from multiple functions in the PHP script.

return statement in PHP

<?php $x = 6; function scope(){ $y = 7; echo $x; // prints 'undefined variable' global $x; echo $x; // prints 6 echo $y; // prints 7 } scope();

The return statement is used to return the final output of a function. Once the first return statement is executed, the function is exited.

Invoking a function in PHP

<?php $x = 6; function scope(){ $y = 7; echo $x; // prints 'undefined variable' global $x; echo $x; // prints 6 echo $y; // prints 7 } scope();

Functions are invoked by using the function name followed by parentheses.

Define PHP Function

<?php $x = 6; function scope(){ $y = 7; echo $x; // prints 'undefined variable' global $x; echo $x; // prints 6 echo $y; // prints 7 } scope();

A function contains a set of instructions to be executed. It is defined by using the keyword function followed by the name of the function, the parentheses which contain the parameters, and finally the curly braces which contain the code block.

Introduction to PHP Functions
Lesson 1 of 1
  1. 1
    We can think of programs as series of instructions to be performed by the computer. So far in each of our PHP programs, each step has been explicitly laid out in the order we want it to happen. S…
  2. 2
    Let’s get right to it and create our first function: function greetLearner() { echo “Hello, Learner!\n”; echo “I hope you’re enjoying PHP!\n”; echo “Love, Codecademy”; } Let’s walk through …
  3. 3
    In our last exercise, we saw that when we define a function, the instructions within the code block aren’t executed. Defining a function only tells the computer to associate a name with a set of in…
  4. 4
    As we build more complicated functions, we’ll often be using them to process data. In order for the data to be useful, functions have the ability to return a value in addition to performing instr…
  5. 5
    The return keyword immediately stops a function. This means that any code after a return won’t run. Let’s compare two different functions: announceRunning() and announceRunning2(). The first of t…
  6. 6
    The value returned from a function is just that—a value. This means it can be used in any manner we would normally use a value of that type. This can take some getting used to. Take a look at the f…
  7. 7
    What about functions without return statements? Any function without a return returns a special value NULL. NULL is a special data type that stands for the absence of a value. function returnNothi…
  8. 8
    Functions that do exactly the same thing every time they run can save us from having to repeat code in our programs, but functions can do more. In the beginning of this lesson, we wrote a greetLe…
  9. 9
    We can also define functions with multiple parameters. function divide($num_one, $num_two) { return $num_one / $num_two; }; In the function above, we defined the divide() function. It takes in…
  10. 10
    Earlier we wrote a sayCustomHello() function which took in a $name and printed a custom greeting. If we tried to invoke this function without an argument, it would cause an error; the function is d…
  11. 11
    We can invoke functions with variables or with values directly. When we invoke a function with a variable as its argument, it’s as if we’re assigning the value held by that variable to the function…
  12. 12
    Great job! Let’s review what we covered in this lesson: + We can package a set of instructions within a named function to reuse whenever we’d like. + When we invoke a function, the computer wil…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo