Introduction to Functions in PHP
Get started learning about functions in PHP so you can create your own reusable blocks of code.
StartKey Concepts
Review core concepts you need to learn to master this subject
PHP Variable Scope
return statement in PHP
Invoking a function in PHP
Define PHP Function
Camel Case Function
PHP Variable Scope
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.
Introduction to PHP Functions
Lesson 1 of 1
- 1We 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…
- 2Let’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 …
- 3In 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…
- 4As 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…
- 5The 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…
- 6The 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…
- 7What 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…
- 8Functions 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…
- 9We 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…
- 10Earlier 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…
- 11We 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…
- 12Passing arguments into a function and returning values is a clear way to define the interface between the function and the rest of the code. This is the preferred method of exchanging information w…
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory