Jump to content

Globals


johnnyk

Recommended Posts

You can either return it as corbin has shown above or make your var2 var global like so:
[code]function whatever2()
{
    global $var2;

    $var2 = "hi";
}
// call our function, first
whatever2();

// now we will be able to use our variable from the function
echo $var2;[/code]

By I'd recommend you to use return rather than making your variables global. If you are going to be returning 2 or more variables. I'd return it as an array, and store the array in a variable, like so:
[code]<?php

function getVars()
{
    $var = 'hello';
    $var2 = 'hey';
    $var3 = 'bye';

    return array($var, $var2, $var3);
}

$vars = getVars();

echo '<pre>' . print_r($vars, true) . '</pre>';
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/15847-globals/#findComment-65088
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.