PHP 8 Intermediate Basics · 1 min read

Local vs global scope

Start the PHP 8 learning path »

❮ PHP 8 Home All Tutorials ❯

Why can’t my function see an outside variable?

global keyword / pass args.

Example

PHP 8 Example
<?php
$site = 'CodePrimer';
function title() {
  global $site;
  return $site;
}
echo title();

Line-by-Line Walkthrough

Prefer passing parameters over global when possible.

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share

Reactions

Loading reactions…