Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Using a variable that are inside a function

Tags:

php

I have:

include ('functions.php');
check_blocked();
echo $blocked;

in functions.php, check_blocked(); exists. Inside check_blocked I got:

global $blocked;
$blocked = '1234';

I want to echo $blocked variable, that are inside check_blocked().

It doesnt work, no output..

This is an example of my original problem, so please dont say that I could just have the echo inside the function, as I cannot have in my original code.

like image 714
Karem Avatar asked Feb 26 '23 09:02

Karem


1 Answers

Your code should look like

$blocked = 0;
include('functions.php');
check_blocked();
echo $blocked;

With functions.php looking like

function check_blocked(){
     global $blocked;
     $blocked = 1234;
}

You must define blocked outside of the scope of the function before you global blocked into the function.

like image 169
Geoffrey Wagner Avatar answered Mar 08 '23 02:03

Geoffrey Wagner