A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearPHP syntax for dereferencing function result - page 1
User InfoPosts
PHP syntax for dereferencing function result#1
Background

In every other programming language I use on a regular basis, it is simple to operate on the return value of a function without declaring a new variable to hold the function result.

In PHP, however, this does not appear to be so simple:

example1 (function result is an array)

<?php 
function foobar(){
return preg_split((/\s+/(, (zero one two three four five();
}

// can php say "zero"?

/// print( foobar()[0] ); /// <-- nope
/// print( &foobar()[0] ); /// <-- nope
/// print( &foobar()->[0] ); /// <-- nope
/// print( "${foobar()}[0]" ); /// <-- nope
?>


example2 (function result is an object)

<?php    
function zoobar(){
// NOTE: casting (object) Array() has other problems in PHP
// see e.g., http://www.momige.com/1869812
$vout = (object) Array((0(=>(zero(,(fname(=>(homer(,(lname(=>(simpson(,);
return $vout;
}

// can php say "zero"?
// print zoobar()->0; // <- nope (parse error)
// print zoobar()->{0}; // <- nope
// print zoobar()->{(0(}; // <- nope
// $vtemp = zoobar(); // does using a variable help?
// print $vtemp->{0}; // <- nope


Can anyone suggest how to do this in PHP?

posted date: 2009-04-12 17:12:00


Re: PHP syntax for dereferencing function result#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-12 17:12:01


Re: PHP syntax for dereferencing function result#3
PHP can not access array results from a function. Some people call this an issue, some just accept this as how the language is designed. So PHP makes you create unessential variables just to extract the data you need.So you need to do.$var = foobar();print($var[0]);

posted date: 2009-04-12 17:14:00


Re: PHP syntax for dereferencing function result#4
I realise that I'm still incredibly new to this, but why is this a problem? It...makes sense to me that you'd need to create a variable to hold a value/result; though admittedly: very new

posted date: 2009-04-12 17:16:00


Re: PHP syntax for dereferencing function result#5
It's an issue because it becomes very easy to lose track of where you are if you have a function that returns a structured variable or object. For example, what if you have $data['tvshow']['episodes'][1]['description'] as a valid address in your variable?

posted date: 2009-04-12 17:26:00


Re: PHP syntax for dereferencing function result#6
Well, you could use any of the following solutions, depending on the situation:function foo() { return array("foo","bar","foobar","barfoo","tofu");}echo(array_shift(foo())); // prints "foo"echo(array_pop(foo())); // prints "tofu"Or you can grab specific values from the returned array using list():list($foo, $bar) = foo();echo($foo); // prints "foo"echo($bar); // print "bar"Edit: the example code for each() I gave earlier was incorrect. each() returns a key-value pair. So it might be easier to use foreach():foreach(foo() as $key=>$val) { echo($val);}

posted date: 2009-04-12 18:36:00


Re: PHP syntax for dereferencing function result#7
Write a wrapper function that will accomplish the same. Because of PHP(s easy type-casting this can be pretty open-ended:function array_value ($array, $key) {return $array[$key];}

posted date: 2009-06-07 02:23:00


Re: PHP syntax for dereferencing function result#8
Does this work? return ($foo->getBarArray())[0];Otherwise, can you post the getBarArray() function? I don(t see why that wouldn(t work from what you posted so far.

posted date: 2009-07-25 08:35:00


Re: PHP syntax for dereferencing function result#9
There isn(t a way to do that unfortunately, although it is in most other programming languages.If you really wanted to do a one liner, you could make a function called a() and do something like$test = a(func(), 1); // second parameter is the key.But other than that, func()[1] is not supported in PHP.

posted date: 2009-07-25 08:36:00


Re: PHP syntax for dereferencing function result#10
No that doesn't work either. Regardless of the function, it throws an "unexpected [" error.

posted date: 2009-07-25 08:39:00


Re: PHP syntax for dereferencing function result#11
Oh wow, I didn't know that. Do you know why that doesn't work? Shouldn't func() be essentially an array type with the return value, so [1] acts on an array? Or does PHP parse it poorly?

posted date: 2009-07-25 08:43:00


Re: PHP syntax for dereferencing function result#12
PHP does not parse it like other languages do, so you have to define it as a variable first.

posted date: 2009-07-25 08:46:00


select page: « 1 2...»
Copyright ©2008-2017 www.momige.com, all rights reserved.