The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 21, 2019, 12:10:55 PM

Login with username, password and session length


Pages: [1] 2
  Print  
Author Topic: Function - return 2 values?  (Read 7049 times)
stma
Rookie
**
Offline Offline

Posts: 10


View Profile
« on: December 31, 2009, 10:11:20 AM »

I always thought it wasn't possible to return more than one value from a function with out an array.

However - this is working:

Code:
function load_xml_feed($location)
{

$feedXml = simplexml_load_file($location);

$i= ‘1′;
foreach ($feedXml->channel->item as $article)
{
$title[$i] = (string)$article->title;
$link[$i] = (string)$article->link;
$description[$i] = (string)$article->description;

global $title;
global $description;
global $link;

$i++;

}
$randnumber = rand(2, $i);
$link = trim($link[$randnumber]);
$title = trim($title[$randnumber]);
$description = trim($description[$randnumber]);
$title = iconv(“UTF-8″, “ISO-8859-1″, $title);
$description = iconv(“UTF-8″, “ISO-8859-1″, $description);

return $title;
return $link;
return $description;

}
//call the function with a valid rss feed.
load_xml_feed($rss);

//echo out the 3 things the function is returning -- why IS this working?
echo $title;
echo $link;
echo $description;
Any idea why?  I thought global values only worked inside the function or to pull data into a function.
Logged

No links in signatures please
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #1 on: December 31, 2009, 12:14:00 PM »

errr wtf?

my head just did all sorts of scope nightmare summersalts... That is terrifying if that is right.

Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
stma
Rookie
**
Offline Offline

Posts: 10


View Profile
« Reply #2 on: December 31, 2009, 12:25:09 PM »

Basically it DOES work.


No damn clue why - I was combining two of my functions together and ran it -- poof - it worked. 
Logged

No links in signatures please
stma
Rookie
**
Offline Offline

Posts: 10


View Profile
« Reply #3 on: December 31, 2009, 12:30:03 PM »

Little update:

I've tested it on 3 different servers and it's returning all three variables from the function.

Should only be able to do that with an array.
Logged

No links in signatures please
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #4 on: December 31, 2009, 12:51:08 PM »

exactly. Are you sure you dont have those vars declared somewhere outside the scope of the function as globals?

if not, that is terrifying.
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
stma
Rookie
**
Offline Offline

Posts: 10


View Profile
« Reply #5 on: December 31, 2009, 01:22:03 PM »

That snippet runs as is.

Just define $rss as a rss feed and run it.

Obviously your server has to support iconv - if it doesn't just strip those lines out.

I be baffled - and a few of you are edubecated enough to tell my why I hope.... or at least tell my why it runs on the servers I've tested it on.


Logged

No links in signatures please
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #6 on: December 31, 2009, 01:34:23 PM »

oh wait. LOL

I AM RETARDED.

you have them declared as globals inside the function. (fucking cough drugs are making me stupid it seems)

Basically, when you declare a var global, inside a function, you are pushing it up to the root scope.

so when you do "global $title" inside the function, it actually creates a var without any scope, or actually global scope. Meaning, its accessible from ANYWHERE.

so.... your code will work even without the return lines because the global var is already set to a value inside the function.
(99% sure on that but havent tried)

Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
stma
Rookie
**
Offline Offline

Posts: 10


View Profile
« Reply #7 on: December 31, 2009, 01:37:58 PM »

O.K. -- I THOUGHT that made it only available in other functions.  I'm tuckered too - so neither of us has a good enough excuse.
Logged

No links in signatures please
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #8 on: December 31, 2009, 02:55:51 PM »

lol. im tempted to delete this thread...

Actually i cant remember how globals actually work. I think to get them into a function, you must declare them inside it. like you did.
So even if you wanted those vars inside a different function you would need to declare them in that new function as well.

I guess "declare" is the wrong word since that implies "initializing".

More like "bring into this scope".

but yea, global is bringing a variable from the root scope, down into the function scope. It can be declared for the first time inside a function and it will automatically bubble up into the root (but not down into other functions, i think).

I never use globals.
i use $GLOBALS[] when I need a global value. Its more obvious in code, less prone to what just happened in this thread... lol
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #9 on: December 31, 2009, 02:57:19 PM »

Wow ... just back from holiday to this thread. That must be some jackboot cough medicine Nuts. Whydoncha keep a little around for after the holidays and when you're healthy? Wink

STMA - don't use global unless there is absolutely no other way (hard to imagine).
Global essentially defeats every bit of scoping that keeps you in the pink.

[edit]
Nuts - global pushes variables into the global scope ... so the root level (which hangs at global) can automatically see them, which is why the above function works. Other functions will still need to declare them global if you want to pull a variable down into a lower scope (like a function).

@ $GLOBALS - excellent plan, that's what I do as well. Named vars on the GLOBALS array is the best way, because it is very clear what scope they live in.
[/edit]
« Last Edit: December 31, 2009, 03:00:34 PM by perkiset » Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #10 on: December 31, 2009, 05:20:38 PM »

Unfortunatly it don't get me stoned, just dumb. I am eating 3 at the same time... And it's barely denting the cough. As usual.
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
nop_90
Global Moderator
Lifer
*****
Offline Offline

Posts: 2203


View Profile
« Reply #11 on: December 31, 2009, 09:02:12 PM »

NEVER EVER USE GLOBALS.
Just in case u did not see that.
In the early days of PHP there was a very nasty security problem.
PHP used to automatically parse urls args into globals, both posts and gets.

http://www.perkiset.org/forum/index.php?action=post&topic=2357.0&num_replies=10
So action would be stuck into global $action etc.
This was very convienient.

Problem was that stupid programmers would stick their thier security marking bits into a global.
So lets say for this board, if you where an admin user, then it would be set by global $admin_user
so someone could go along like this and go
http://www.perkiset.org/forum/index.php?action=post&topic=2357.0&num_replies=10&admin_user=1
And then PHP would over write the global admin_user, and magically I get to be admin user.
Again it was not really a security problem, the documentation stated quite clearly that is what PHP did.
Then you end up with crippled languages like java and C# which do not allow the programmer to do anything.
Multiple inheritance is difficult to understand, therefore lets not have it in the language Smiley
Actually it is a good thing, big companies fall into that trap, so they go Java etc Smiley.
Much easier to compete against them when the programmer has one hand tied behind his back Smiley

Not counting that with too many globals it clutters up the name space. you can get all sorts of evil nasty errors from that.

In this part you are not returning 3 values just 1
return $title;
return $link;
return $description

it reaches $title then function returns.
Either return using an array. Or if you want pass by reference. If you go the reference way your code will be slightly quicker.
Or in this case (I am not 100% up to snuff in PHP, but i would do something like this).
title,link and description are all related. So make a throw away class and return info with that. Or maybe use a dictionary (i think in PHP dictionaries and arrays are the same).
Logged
jammaster82
Lifer
*****
Offline Offline

Posts: 666


Thats craigs list for ya


View Profile
« Reply #12 on: December 31, 2009, 09:55:19 PM »

So you would create a throw away class ,
pass the class by reference and store link
title and description in it from inside the function?
Logged

The watched pot, never boils... But if you walk away from it , the soup burns.  What gives?
stma
Rookie
**
Offline Offline

Posts: 10


View Profile
« Reply #13 on: December 31, 2009, 10:02:34 PM »

My finished code does return in an array - just noticed this working while combining a couple of functions into one and it startled me.  Learned a little bit about globals so thanks all.
Logged

No links in signatures please
nop_90
Global Moderator
Lifer
*****
Offline Offline

Posts: 2203


View Profile
« Reply #14 on: December 31, 2009, 10:18:43 PM »

 ROFLMAO @ jam
wait till perks gets back to see what he saids

Reference is probably the worst choice after globals, because u are modifying outside the functions scope.
So unless u are modifying a huge array etc, not a good idea.

I am partial to dictionaries. I use them as light weight classes.
Mainly because it is easier to understand
$feed["title"] compared to $feed[0]
So IMHO i think dictionary is way to go, unless with the object you are going to return you will be doing operations on.
Logged
Pages: [1] 2
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!