By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,278 Members | 1,357 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,278 IT Pros & Developers. It's quick & easy.

Way to do this in one line?

P: n/a
Hi,

I'm using PHP 5. I have an array of strings ($doc->getFilesArr()),
each of which is an absolute file name. I want to extract the "short
name" of the first element in the array. You can assume the array has
at least one element. The mess below

<?= pathinfo({$doc->getFilesArr()}[0], PATHINFO_FILENAME) ?>

causea a parse error. Any ways to correct?

Thanks ,-
Aug 21 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
..oO(la***********@zipmail.com)
>I'm using PHP 5. I have an array of strings ($doc->getFilesArr()),
each of which is an absolute file name. I want to extract the "short
name" of the first element in the array. You can assume the array has
at least one element. The mess below

<?= pathinfo({$doc->getFilesArr()}[0], PATHINFO_FILENAME) ?>

causea a parse error. Any ways to correct?
PHP doesn't support this systax. You could (if possible) extend the
class which $doc is created from, so that you can call a method which
directly returns the first element of that array.

Additionally you should avoid short open tags. They are unreliable and
will be turned OFF by default in PHP 6.

Micha
Aug 21 '08 #2

P: n/a
On Aug 21, 10:36*am, Michael Fesser <neti...@gmx.dewrote:
.oO(laredotorn...@zipmail.com)
I'm using PHP 5. *I have an array of strings ($doc->getFilesArr()),
each of which is an absolute file name. *I want to extract the "short
name" of the first element in the array. *You can assume the array has
at least one element. *The mess below
<?= pathinfo({$doc->getFilesArr()}[0], PATHINFO_FILENAME) ?>
causea a parse error. *Any ways to correct?

PHP doesn't support this systax. You could (if possible) extend the
class which $doc is created from, so that you can call a method which
directly returns the first element of that array.

Additionally you should avoid short open tags. They are unreliable and
will be turned OFF by default in PHP 6.

Micha
<?php
$files = $doc->getFilesArr();
pathinfo($files[0], PATHINFO_FILENAME)
?>

or, not sure if it will work, but if it does this would be even better
if the array uses a lot of memory:

<?php
$files &= $doc->getFilesArr();
pathinfo($files[0], PATHINFO_FILENAME)
?>

pretty basic ;)
Aug 21 '08 #3

P: n/a
..oO(Poppitypop)
>On Aug 21, 10:36*am, Michael Fesser <neti...@gmx.dewrote:
>.oO(laredotorn...@zipmail.com)
>I'm using PHP 5. *I have an array of strings ($doc->getFilesArr()),
each of which is an absolute file name. *I want to extract the "short
name" of the first element in the array. *You can assume the array has
at least one element. *The mess below
><?= pathinfo({$doc->getFilesArr()}[0], PATHINFO_FILENAME) ?>
>causea a parse error. *Any ways to correct?

PHP doesn't support this systax. You could (if possible) extend the
class which $doc is created from, so that you can call a method which
directly returns the first element of that array.

Additionally you should avoid short open tags. They are unreliable and
will be turned OFF by default in PHP 6.

Micha

<?php
$files = $doc->getFilesArr();
pathinfo($files[0], PATHINFO_FILENAME)
?>

or, not sure if it will work, but if it does this would be even better
if the array uses a lot of memory:

<?php
$files &= $doc->getFilesArr();
pathinfo($files[0], PATHINFO_FILENAME)
?>
Even if the array uses a lot of memory, such code will probably be more
inefficient(!) than the first version. PHP uses copy-on-write semantics,
which means that there isn't any data copied unless really necessary. It
takes more time to create/use the reference than a simple assignment.

Use references only when you really need them, not for "optimization".
The engine is smart enough to handle cases like above efficiently.

Micha
Aug 21 '08 #4

P: n/a
On Aug 21, 11:36*am, Michael Fesser <neti...@gmx.dewrote:
.oO(laredotorn...@zipmail.com)
I'm using PHP 5. *I have an array of strings ($doc->getFilesArr()),
each of which is an absolute file name. *I want to extract the "short
name" of the first element in the array. *You can assume the array has
at least one element. *The mess below
<?= pathinfo({$doc->getFilesArr()}[0], PATHINFO_FILENAME) ?>
causea a parse error. *Any ways to correct?

PHP doesn't support this systax. You could (if possible) extend the
class which $doc is created from, so that you can call a method which
directly returns the first element of that array.

Additionally you should avoid short open tags. They are unreliable and
will be turned OFF by default in PHP 6.

Micha
When you say "avoid short open tags", what do you mean? - Dave
Aug 21 '08 #5

P: n/a
..oO(la***********@zipmail.com)
>On Aug 21, 11:36*am, Michael Fesser <neti...@gmx.dewrote:
>.oO(laredotorn...@zipmail.com)
>I'm using PHP 5. *I have an array of strings ($doc->getFilesArr()),
each of which is an absolute file name. *I want to extract the "short
name" of the first element in the array. *You can assume the array has
at least one element. *The mess below
><?= pathinfo({$doc->getFilesArr()}[0], PATHINFO_FILENAME) ?>
>causea a parse error. *Any ways to correct?

PHP doesn't support this systax. You could (if possible) extend the
class which $doc is created from, so that you can call a method which
directly returns the first element of that array.

Additionally you should avoid short open tags. They are unreliable and
will be turned OFF by default in PHP 6.

Micha

When you say "avoid short open tags", what do you mean? - Dave
These are short open tags:

<?
<?=

The recommended, more portable and more reliable way are these longer
variants:

<?php
<?php echo

They will work always, while short open tags can be disabled on the
server and may cause problems if you want to use PHP to print out XML
for example.

Micha
Aug 21 '08 #6

P: n/a
On 21 Aug, 18:17, "laredotorn...@zipmail.com"
<laredotorn...@zipmail.comwrote:
Hi,

I'm using PHP 5. I have an array of strings ($doc->getFilesArr()),
each of which is an absolute file name. I want to extract the "short
name" of the first element in the array. You can assume the array has
at least one element. The mess below

<?= pathinfo({$doc->getFilesArr()}[0], PATHINFO_FILENAME) ?>

causea a parse error. Any ways to correct?

Thanks ,-
There is no "short name". If you read the manual page, you'd see the
links at the end to various functions including basename() which I
suspect is what you're looking for.

Not sure if it would work but I guess you could call current() on the
return set. Even if it works, it would be sloppy practice -
programming is not a competition to right the most terse
implementation possible. Write your own wrapper function/method.

C.
Aug 22 '08 #7

P: n/a
On Aug 21, 12:16*pm, Michael Fesser <neti...@gmx.dewrote:
.oO(Poppitypop)
On Aug 21, 10:36*am, Michael Fesser <neti...@gmx.dewrote:
.oO(laredotorn...@zipmail.com)
I'm using PHP 5. *I have an array of strings ($doc->getFilesArr()),
each of which is an absolute file name. *I want to extract the "short
name" of the first element in the array. *You can assume the array has
at least one element. *The mess below
<?= pathinfo({$doc->getFilesArr()}[0], PATHINFO_FILENAME) ?>
causea a parse error. *Any ways to correct?
PHP doesn't support this systax. You could (if possible) extend the
class which $doc is created from, so that you can call a method which
directly returns the first element of that array.
Additionally you should avoid short open tags. They are unreliable and
will be turned OFF by default in PHP 6.
Micha
<?php
$files = $doc->getFilesArr();
pathinfo($files[0], PATHINFO_FILENAME)
?>
or, not sure if it will work, but if it does this would be even better
if the array uses a lot of memory:
<?php
$files &= $doc->getFilesArr();
pathinfo($files[0], PATHINFO_FILENAME)
?>

Even if the array uses a lot of memory, such code will probably be more
inefficient(!) than the first version. PHP uses copy-on-write semantics,
which means that there isn't any data copied unless really necessary. It
takes more time to create/use the reference than a simple assignment.

Use references only when you really need them, not for "optimization".
The engine is smart enough to handle cases like above efficiently.

Micha
Hmm. I learn something new every day. Thanks for telling me that, lol.
Aug 23 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.