Globals

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Globals

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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);
}
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

It has to be done. I need that data outside of that function.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

So return it like Jcart's version does.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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...
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

I'm getting "Undefined variable" when trying to access $data outside of that function.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Code: Select all

function tag_contents($parser, $data) {
    global $new;
    settype($data, "string");
    $new = str_split($data);
}
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Post Reply