← Back to PHP Course | Chapter 5: Functions | Lesson 1 of 10

PHP Functions Introduction

A function is like a recipe card you write once and use whenever you like. You give it a name, and then you can run the same steps again and again without rewriting them.
Syntax
php
function function_name() {
    // body
}

function_name();   // call it

Defining and Calling Functions

A function packages a block of statements under a name so you can run that same logic repeatedly without retyping it.

PHP parses the definition when the file loads, but the code inside only actually runs at the moment something calls the function by name.

Example: Defining and Calling Functions

php
<?php
// Define the function `greet` with no parameters
function greet() {
    // Print "Hello!" to the output
    echo "Hello!";
}
// Call `greet()`
greet();
?>

Function Naming Rules

Function names must start with a letter or an underscore, and while PHP itself doesn't care about the casing you use, staying consistent — camelCase or snake_case throughout a project — makes a codebase far easier to scan and predict.

Example: Function Naming Rules

php
<?php
// Define the function `calculateTotal` with no parameters
function calculateTotal() {
    // Return `100` from this function
    return 100;
}
// Print `calculateTotal()` to the output
echo calculateTotal();
?>

Variable Scope inside Functions

Any variable created inside a function only exists for that function's own execution and disappears once it returns — code outside can't see it.

To read or modify a variable from the surrounding global scope inside a function, you have to explicitly declare it with the global keyword first.

Example: Variable Scope inside Functions

php
<?php
// Declare `$count`, set to `10`
$count = 10;

// Define the function `showCount` with no parameters
function showCount() {
    global $count;
    // Print `$count` to the output
    echo $count;
}
// Call `showCount()`
showCount();
?>

Functions with No Arguments

A function doesn't need parameters to be useful — plenty of functions just perform a fixed task or print a fixed message each time they're called, with nothing external needed to do their job.

Example: Functions with No Arguments

php
<?php
// Define the function `printWelcome` with no parameters
function printWelcome() {
    // Print "Welcome to the site!" to the output
    echo "Welcome to the site!";
}
// Call `printWelcome()`
printWelcome();
?>

Conditional Function Declarations

Defining a function inside an if block means the function only comes into existence if that block actually executes — PHP registers it conditionally, at runtime, rather than immediately when the file is parsed the way an unconditional function definition would be.

Example: Conditional Function Declarations

php
<?php
// Declare `$defineIt`, set to `true`
$defineIt = true;
// Check whether `$defineIt`
if ($defineIt) {
    // Define the function `sayHi` with no parameters
    function sayHi() {
        // Print "Hi!" to the output
        echo "Hi!";
    }
}
// Call `sayHi()`
sayHi();
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Calling a function before its definition is reached inside a conditional block, when only top-level functions are hoisted.
  2. Expecting a function to see a variable defined outside it, when variables inside a function are local unless passed in.
  3. Defining the same function name twice, which causes a fatal 'Cannot redeclare' error.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.