Re: [PHP] how a function 'return' statement works

2002-02-14 Thread Erik Price

Hm... I tried quite a few variations on this.  I can't seem to get any 
displayable value out of this function.

function get_current_page_name()
{
$current_page_name = explode(/, $_SERVER['PHP_SELF']);
$current_page_name = $current_page_name[-1];
return $current_page_name ;
}

$errorcode = get_current_page_name();
echo $errorcode;

Nothing.

The only thing that works is $errorcode = 
basename($_SERVER['PHP_SELF']), but that's not the point -- I still 
don't understand why the function above doesn't return anything.  In 
this case, I'm positive that I've specified the last element of the 
array.  (Haven't I?)


Erik




On Wednesday, February 13, 2002, at 05:03  PM, Darren Gamble wrote:

 I think what you're trying to do is return one particular element out 
 of the
 array.  If that's the case, just use something like:

 return $current_page_name[-1]

 to return the last element in the array.

 



Erik Price
Web Developer Temp
Media Lab, H.H. Brown
[EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] how a function 'return' statement works

2002-02-14 Thread Alastair Battrick

Do you not have to make $_SERVER a global variable in the function ?

Alastair
Lightwood Consultancy Ltd

 Hm... I tried quite a few variations on this.  I can't seem to get any 
 displayable value out of this function.
 
 function get_current_page_name()
 {
 $current_page_name = explode(/, $_SERVER['PHP_SELF']);
 $current_page_name = $current_page_name[-1];
 return $current_page_name ;
 }
 
 $errorcode = get_current_page_name();
 echo $errorcode;
 
 Nothing.
 
 The only thing that works is $errorcode = 
 basename($_SERVER['PHP_SELF']), but that's not the point -- I still 
 don't understand why the function above doesn't return anything.  In 
 this case, I'm positive that I've specified the last element of the 
 array.  (Haven't I?)
 
 
 Erik
 
 
 
 
 On Wednesday, February 13, 2002, at 05:03  PM, Darren Gamble wrote:
 
  I think what you're trying to do is return one particular element out 
  of the
  array.  If that's the case, just use something like:
 
  return $current_page_name[-1]
 
  to return the last element in the array.
 
  
 
 
 
 Erik Price
 Web Developer Temp
 Media Lab, H.H. Brown
 [EMAIL PROTECTED]
 
 
 -- 
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] how a function 'return' statement works

2002-02-14 Thread Aaron Gould

$_SERVER (as well as all the $_* functions) in PHP 4.1 are automatically
global.

--
Aaron Gould
[EMAIL PROTECTED]
Web Developer


- Original Message -
From: Alastair Battrick [EMAIL PROTECTED]
To: Erik Price [EMAIL PROTECTED]; Darren Gamble
[EMAIL PROTECTED]
Cc: PHP [EMAIL PROTECTED]
Sent: Thursday, February 14, 2002 10:46 AM
Subject: RE: [PHP] how a function 'return' statement works


 Do you not have to make $_SERVER a global variable in the function ?

 Alastair
 Lightwood Consultancy Ltd

  Hm... I tried quite a few variations on this.  I can't seem to get any
  displayable value out of this function.
 
  function get_current_page_name()
  {
  $current_page_name = explode(/, $_SERVER['PHP_SELF']);
  $current_page_name = $current_page_name[-1];
  return $current_page_name ;
  }
 
  $errorcode = get_current_page_name();
  echo $errorcode;
 
  Nothing.
 
  The only thing that works is $errorcode =
  basename($_SERVER['PHP_SELF']), but that's not the point -- I still
  don't understand why the function above doesn't return anything.  In
  this case, I'm positive that I've specified the last element of the
  array.  (Haven't I?)
 
 
  Erik
 
 
 
 
  On Wednesday, February 13, 2002, at 05:03  PM, Darren Gamble wrote:
 
   I think what you're trying to do is return one particular element out
   of the
   array.  If that's the case, just use something like:
  
   return $current_page_name[-1]
  
   to return the last element in the array.
  
   
 
  
 
  Erik Price
  Web Developer Temp
  Media Lab, H.H. Brown
  [EMAIL PROTECTED]
 
 
  --
  PHP General Mailing List (http://www.php.net/)
  To unsubscribe, visit: http://www.php.net/unsub.php

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] how a function 'return' statement works

2002-02-14 Thread DL Neil

Erik,

 function get_current_page_name()
 {
 $current_page_name = explode(/, $_SERVER['PHP_SELF']);
 $current_page_name = $current_page_name[-1];
 return $current_page_name ;
 }
 
 $errorcode = get_current_page_name();
 echo $errorcode;
 
 The only thing that works is $errorcode = 
 basename($_SERVER['PHP_SELF']), but that's not the point -- I still 
 don't understand why the function above doesn't return anything.  In 
 this case, I'm positive that I've specified the last element of the 
 array.  (Haven't I?)


Some extra debug echo statements will (dis)prove this...
=dn



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] how a function 'return' statement works

2002-02-14 Thread Matt Schroebel

How about:
return $current_page_name[count($current_page_name)-1];

-Original Message-
From: Erik Price [mailto:[EMAIL PROTECTED]] 


Can anyone give my puny mind an explanation as to why the following 
function only returns the value Array ?

# ===
# get_current_page_name()
# ---
# Returns the current document
#  Arguments
# ---
# no arguments
# ===

function get_current_page_name()
{
$current_page_name = explode(/, $_SERVER['PHP_SELF']);
$current_page_name = array_slice($current_page_name, -1);
return $current_page_name;
}



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] how a function 'return' statement works

2002-02-14 Thread Lars Torben Wilson

On Thu, 2002-02-14 at 07:32, Erik Price wrote:
 Hm... I tried quite a few variations on this.  I can't seem to get any 
 displayable value out of this function.
 
 function get_current_page_name()
 {
 $current_page_name = explode(/, $_SERVER['PHP_SELF']);
 $current_page_name = $current_page_name[-1];
 return $current_page_name ;
 }
 
 $errorcode = get_current_page_name();
 echo $errorcode;
 
 Nothing.
 
 The only thing that works is $errorcode = 
 basename($_SERVER['PHP_SELF']), but that's not the point -- I still 
 don't understand why the function above doesn't return anything.  In 
 this case, I'm positive that I've specified the last element of the 
 array.  (Haven't I?)

Hi Erik. :)

No, you've asked for the element of $current_page_name which has the key
-1. To get the last element, probably the easiest thing to do is:

return $current_page_name[count($current_page_name) - 1];

However, a somewhat less expensive way to do it:

$path = $_SERVER['PHP_SELF'];
return substr($path, strrpos($path, '/') + 1);

(No array overhead.)


Cheers (man, it's sunny in Vancouver! Weird...),

Torben
 
 Erik
 
 On Wednesday, February 13, 2002, at 05:03  PM, Darren Gamble wrote:
 
  I think what you're trying to do is return one particular element out 
  of the
  array.  If that's the case, just use something like:
 
  return $current_page_name[-1]
 
  to return the last element in the array.

I do not know where this came from, but it just ain't so.


-- 
 Torben Wilson [EMAIL PROTECTED]
 http://www.thebuttlesschaps.com
 http://www.hybrid17.com
 http://www.inflatableeye.com
 +1.604.709.0506


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] how a function 'return' statement works

2002-02-14 Thread Erik Price


On Thursday, February 14, 2002, at 10:46  AM, Alastair Battrick wrote:

 Do you not have to make $_SERVER a global variable in the function ?

$_SERVER is global AFAIK, but good thinking.  All $_* variables are 
global (unlike the $HTTP_*_VARS arrays).




Erik




Erik Price
Web Developer Temp
Media Lab, H.H. Brown
[EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] how a function 'return' statement works

2002-02-14 Thread Erik Price


On Thursday, February 14, 2002, at 02:32  PM, Lars Torben Wilson wrote:

 I think what you're trying to do is return one particular element out
 of the
 array.  If that's the case, just use something like:

 return $current_page_name[-1]

 to return the last element in the array.

 I do not know where this came from, but it just ain't so.

Torben,

I just got this message now (where you explained a better way to do 
it).  I got a lot of feedback on this, and I feel even worse than before 
for not realizing that I was trying to display the value of an array 
element without specifying its key!  and then this gem up top here got 
me thinking I could shortcut the array_slice($current_page_name, -1, 
1) to get the last element.

The substr trick works too, but I'm glad i was able to do it my way too.

here is the final function:

# ===
# get_current_page_name()
# ---
# Returns the current document
#  Arguments
# ---
# no arguments
# ===

function get_current_page_name()
{
$current_page = $_SERVER['PHP_SELF'];
$current_page_name = explode(/, $current_page);
$pagename = array_slice($current_page_name, -1, 1);
return $pagename[0];
}





Erik Price
Web Developer Temp
Media Lab, H.H. Brown
[EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] how a function 'return' statement works

2002-02-13 Thread Rick Emery

If your call to the function is:  print get_current_page_name()
then you are getting the expected results, a description of the returned
value.

Change your call to: $myarrray = get_current_page_name();

Then iterate through $myarray to print each value;

-Original Message-
From: Erik Price [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, February 13, 2002 3:56 PM
To: PHP
Subject: [PHP] how a function 'return' statement works



Can anyone give my puny mind an explanation as to why the following 
function only returns the value Array ?

# ===
# get_current_page_name()
# ---
# Returns the current document
#  Arguments
# ---
# no arguments
# ===

function get_current_page_name()
{
$current_page_name = explode(/, $_SERVER['PHP_SELF']);
$current_page_name = array_slice($current_page_name, -1);
return $current_page_name;
}

(I know this does the same thing as 'basename($_SERVER['PHP_SELF'])', I 
just want to understand something a bit more fundamental to the way 
functions work)

Thanks,



Erik






Erik Price
Web Developer Temp
Media Lab, H.H. Brown
[EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] how a function 'return' statement works

2002-02-13 Thread Darren Gamble

Good day,

You're returning $current_page_name.

$current_page_name is set to the return value for array_slice.

And array_slice returns an array.

So, $current_page_name will be an array.

So, you'll always return an array.


I think what you're trying to do is return one particular element out of the
array.  If that's the case, just use something like:

return $current_page_name[-1]

to return the last element in the array.


Darren Gamble
Planner, Regional Services
Shaw Cablesystems GP
630 - 3rd Avenue SW
Calgary, Alberta, Canada
T2P 4L4
(403) 781-4948


-Original Message-
From: Erik Price [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, February 13, 2002 2:56 PM
To: PHP
Subject: [PHP] how a function 'return' statement works



Can anyone give my puny mind an explanation as to why the following 
function only returns the value Array ?

# ===
# get_current_page_name()
# ---
# Returns the current document
#  Arguments
# ---
# no arguments
# ===

function get_current_page_name()
{
$current_page_name = explode(/, $_SERVER['PHP_SELF']);
$current_page_name = array_slice($current_page_name, -1);
return $current_page_name;
}

(I know this does the same thing as 'basename($_SERVER['PHP_SELF'])', I 
just want to understand something a bit more fundamental to the way 
functions work)

Thanks,



Erik






Erik Price
Web Developer Temp
Media Lab, H.H. Brown
[EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] how a function 'return' statement works

2002-02-13 Thread Gerhard Hoogterp

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On Wednesday 13 February 2002 22:55, Erik Price wrote:
 Can anyone give my puny mind an explanation as to why the following
 function only returns the value Array ?

 # ===
 # get_current_page_name()
 # ---
 # Returns the current document
 #  Arguments
 # ---
 # no arguments
 # ===

 function get_current_page_name()
 {
 $current_page_name = explode(/, $_SERVER['PHP_SELF']);
 $current_page_name = array_slice($current_page_name, -1);
 return $current_page_name;
 }


from the manual:

array array_slice ( array array, int offset [, int length])

array in, array out.. it wouldn't suprice me if when you changed the return 
into

return $current_page_name.'';

it would become a string again. 

Gerhard

 
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE8auY+Um/iHoGGwKMRAqaDAJ4v4zT+l4qvJN+Rei1zx7fdxrWV4wCgoly2
BoZqmifb7o6kaO6RzrZp0NU=
=NjIn
-END PGP SIGNATURE-

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php