Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I used a functions into my page I created for WordPress, it's working, but the functions 're not working, doesn't showing any result. For example:

<?php
function tester()
{

global $pol;

$pol="ok all fine and working";
}

tester();

echo $pol;
?>

This function works perfectly if I use it outside WordPress, but problem is when I tried using it into the file I created, it won't work.

I don´t know what the difference I need to assure to work it in WordPress other than any other PHP file.

share|improve this question
add comment

3 Answers

WordPress template files are included via functions, which places the scope of those included files within those functions. That's why you have to declare it global twice. Here is an example you can try with 3 simple php files to illustrate this without using WordPress:

main.php

function include_file_1(){
    include '1.php';
}
function include_file_2(){
    include '2.php';
}
include_file_1();
include_file_2();

1.php

function tester(){
    global $pol;
    $pol="ok all fine and working";
}

2.php

tester();
echo $pol;

If you test this out, you will see you get nothing from echo $pol, it's not within scope. Now edit 2.php to declare global $pol first:

global $pol
tester();
echo $pol;

And now you get the expected results.

share|improve this answer
add comment

It's a PHP question. That's because you do not use correct way to retrieve your global. Use the magic var_dump instead:

global $pols;
var_dump($pol);

You could also use :

$GLOBALS['$pol'] 

And then :

echo tester();
share|improve this answer
    
No work me i try these two methods and no works , and i don´t understand , because this simple function works fine when i put in single file of pfp and run , perfect works but if use into wordpress into template code no works and it´s the same php and the same server –  Fran Jul 14 '13 at 23:03
add comment

To use a global you need to declare it global before defining it and then pull it into scope with global $var_name before using it. You did not do that. You only did half of it.

function tester()
{
    global $pol;

    $pol="ok all fine and working";
}
tester();

global $pol;
echo $pol;

But why are you using a global at all? Why not just return the value? Your code will be shorter and neater.

function tester()
{
    return "ok all fine and working";
}
echo tester();
share|improve this answer
    
No use return because some functions give string value and i need get this value out functions for example , number files in folder , function generate $number_files="" and i need this value from function but out the function and it´s only this –  Fran Jul 14 '13 at 23:06
    
The value you get from this function-- the value of $pol-- will be the same whether you set a global or return a value. –  s_ha_dum Jul 14 '13 at 23:15
    
But why i need repit in wordpress the variable global $poll 2 time into function and outside function no sense , in php and in all my script only put one time into function and works perfect , why no works in wordpress this –  Fran Jul 15 '13 at 0:00
    
There is nothing WordPress about this. It is all basic PHP. it is hard to understand you but it is clear that you misunderstand variable scope. –  s_ha_dum Jul 15 '13 at 0:08
    
I use this functions in many website and no use wordpress and in all websites never need declarate 2 times the same global variable inside of function and outside , in the test function with put inside global $pol; no need put outside in my website but wordpress when i use no return never the value , i know pure php and nothing more , by this i don´t understand why no works inside wordpress using as normal php function but yes works out of worspress , sorry if you don´t understand well , i try explain the best –  Fran Jul 15 '13 at 0:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.