[php] Global variabele bug?

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

  • Scorpion1984
  • Registratie: Juni 2002
  • Laatst online: 07-07 10:04
Dit script geeft "users" als uitput , zoals de bedoeling
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
$userTable = 'users';

function blaat()
{
    global $userTable;
    print_r($GlOBALS);
    $table = $userTable; // Best zinloos, maar goed :)
    echo $table;
}

// en dan aanroepen met
blaat();
?>


Nu heb ik het volgende script(deel van een script) dit wordt geinclude in een door een ander script en vervolgens op het scherm gedumpt. als je die op bepaalde manier aanvraagd word de case "test" uitgevoerd. Dit werkt, alleen dumpt deze niet "userTable: users" zoals zou moeten op het scherm, maar "userTable: ".

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php

//Make required vars global
global $request;
global $tpl;

########################################
# Settings
$mode = $request[3]; //mode selection

$userTable = "users";

// Mode
switch ($mode) {
    case "test":
        //Temporary test-entry
        sendvalidationmail(1);
        break;
    case ...
}

function sendvalidationmail($userid) {
    //Make required var global
    global $userTable;
    
    //Temporary: print out required table
    echo "userTable: ".$userTable;
    die();

    // rest of function
}
?>


Weet iemand waar dit aan kan liggen?

Acties:
  • 0 Henk 'm!

Verwijderd

Zou je het bestand die deze file include ook nog kunnen posten? Dit werkt namelijk gewoon...

Acties:
  • 0 Henk 'm!

  • mocean
  • Registratie: November 2000
  • Laatst online: 04-09 10:34
na een kleine aanpassinge (de ... en de mode) krijg ik de output!
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php 

//Make required vars global 
global $request; 
global $tpl; 

######################################## 
# Settings 
$mode = 'test'; //$request[3]; //mode selection 

$userTable = "users"; 

// Mode 
switch ($mode) { 
    case "test": 
        //Temporary test-entry 
        sendvalidationmail(1); 
        break; 
    //case ... 
} 

function sendvalidationmail($userid) { 
    //Make required var global 
    global $userTable; 
     
    //Temporary: print out required table 
    echo "userTable: ".$userTable; 
    die(); 

    // rest of function 
} 
?> 


Output
code:
1
userTable: users


Dus ik denk dat je elders in het script wat verkeerd doet.

Koop of verkoop je webshop: ecquisition.com


Acties:
  • 0 Henk 'm!

Verwijderd

Scorpion1984 schreef op 24 mei 2004 @ 18:44:
Weet iemand waar dit aan kan liggen?
Volgens mij heeft 'global' alleen betekenis binnen functies, om globale variabelen in een functie namespace te importeren.

Acties:
  • 0 Henk 'm!

  • Scorpion1984
  • Registratie: Juni 2002
  • Laatst online: 07-07 10:04
Verwijderd schreef op 24 mei 2004 @ 18:49:
Zou je het bestand die deze file include ook nog kunnen posten? Dit werkt namelijk gewoon...
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
    function scriptIntoTemplate($script, $tovar, $intemplate)
    {
        
        // This function will run a script defined in $script. 
        // First it will check if the script exists. Else it should return an error
        // If script exists run the script then add the output to $output which will be
        // assigned in the $intemplate to $tovar
        
        // Check if source template exists. Destination template doesn't have to be checked
        // cause the replace function will handle that.
        if (file_exists($script))
        {
            
            // Script exists. So start the script
            ob_start();
            
            if(!@include_once($script)) 
            {
                trigger_error("Script <strong>" . $script . "</strong> could not be runned!", E_USER_WARNING);
                die();
            }
        
            $output = ob_get_contents();
        
            ob_end_clean();
            
            // Replace the var if template exists
            if($this->replace($intemplate, $tovar, $output))
            {
                return TRUE;
            }

        }
        // Script doesn't exists so return an error!
        else
        {
            trigger_error("Script <strong>" . $script . "</strong> doesn't exists. Check the name and try again", E_USER_WARNING);
            die();
        }


    // EndOfFunction        
    }
?>


Die wordt aangeroepen met het volgende stukje text.
code:
1
$tpl->scriptIntoTemplate("modules/" . $script . ".php", "content", "default"); // Put the requested script into the default template

Acties:
  • 0 Henk 'm!

  • Scorpion1984
  • Registratie: Juni 2002
  • Laatst online: 07-07 10:04
Het rare is ook dat de de variabele $userTable nergens voorkomt in de array $GLOBALS, maar wel kan worden aangesproken in de rest van het script.

[ Voor 27% gewijzigd door Scorpion1984 op 24-05-2004 19:52 ]


Acties:
  • 0 Henk 'm!

  • Bosmonster
  • Registratie: Juni 2001
  • Laatst online: 18-09 16:28

Bosmonster

*zucht*

Verwijderd schreef op 24 mei 2004 @ 18:51:
[...]


Volgens mij heeft 'global' alleen betekenis binnen functies, om globale variabelen in een functie namespace te importeren.
Dat weet ik wel zeker :P

Het ietwat verwarrende 'global $var' is alleen om een globale variabele binnen een functie te importeren, aangezien dit niet standaard gebeurt.

[edit]
Spuit 11.. ik herhaal je gewoon :P

[ Voor 31% gewijzigd door Bosmonster op 24-05-2004 20:15 ]

Pagina: 1