Page 1 of 1

Globals

Posted: Fri Oct 13, 2006 12:48 pm
by impulse()
I have this code an I'm getting this error:
PHP Parse error: syntax error, unexpected '=', expecting ',' or ';'

Code: Select all

function tag_contents($parser, $data) {
            settype($data, "string");
            global $new = str_split($data);   // This is the line causing the error

          }
Any ideas?

Posted: Fri Oct 13, 2006 1:02 pm
by John Cartwright
you cannot assign a global at the same time as assigning it's value. Better yet, avoid globals all together and get rid of the code smell.

Code: Select all

function tag_contents($parser, $data) {
   settype($data, "string");
   return str_split($data);
}

Posted: Fri Oct 13, 2006 1:03 pm
by impulse()
It has to be done. I need that data outside of that function.

Posted: Fri Oct 13, 2006 1:04 pm
by feyd
So return it like Jcart's version does.

Posted: Fri Oct 13, 2006 1:10 pm
by Luke
I have never been forced into a global variable. If you do what Jcart says, you can just grab the return value and assign it to a variable in the particular scope you need it in...

Posted: Fri Oct 13, 2006 1:13 pm
by impulse()
I'm getting "Undefined variable" when trying to access $data outside of that function.

Posted: Fri Oct 13, 2006 1:28 pm
by volka

Code: Select all

<?php
function tag_contents($parser, $data) {
   settype($data, "string");
   return str_split($data);
}

$xyz = tag_contents($p, $d);
echo $xyz;
?>
But I guess you want to use tag_contents() as part of a SAX parser (via xml_set_..._handler).
What do you want to achieve with "globalizing" some of the xml data?
Can you show us more code?

Posted: Fri Oct 13, 2006 6:27 pm
by Ollie Saunders

Code: Select all

function tag_contents($parser, $data) {
    global $new;
    settype($data, "string");
    $new = str_split($data);
}

Posted: Fri Oct 13, 2006 7:06 pm
by RobertGonzalez
impulse() wrote:I'm getting "Undefined variable" when trying to access $data outside of that function.
How are you accessing it outside that function? Post your function code and sample call to it.