473,383 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Way to do this in one line?

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
7 1098
..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
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
..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
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
..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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Peter A. Schott | last post by:
Per subject - I realize I can copy/paste a line at a time into an interactive session when I'm trying to debug, but was wondering if there is any tool out there that allows me to copy sections of...
65
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second...
22
by: DraguVaso | last post by:
Hi, For my application I need the following behavior: When I press F4 the cursor has to move to the next line in my multiline textbox which begins with "0". Finding lines starting with 0 isn't...
3
by: Double Echo | last post by:
Hi all, I'm using PHP 4.4.2, and use PHP on both the command-line and the web. I am running PHP on SuSE 10 Linux , in a VMware 5.5 workstation, using Apache 2.0.55 , on my Dell laptop. ...
9
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still...
6
by: magix | last post by:
Hi, when I read entries in file i.e text file, how can I determine the first line and the last line ? I know the first line of entry can be filtered using counter, but how about the last line...
6
by: Jacob Rael | last post by:
Hello, I have a simple script to parse a text file (a visual basic program) and convert key parts to tcl. Since I am only working on specific sections and I need it quick, I decided not to...
14
by: WStoreyII | last post by:
the following code is supposed to read a whole line upto a new line char from a file. however it does not work. it is producing weird results. please help. I had error checking in there for...
11
by: xdevel | last post by:
Hi, I don't understand option. if I write: #line 100 "file" I change file numeration to start to line 100 but what "file" ? any example?
19
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
This is an example of the data; 2007/07/27 11:00:03 ARES_INDICATION 010.050.016.002 404.2.01 (6511) RX 74 bytes 2007/07/27 11:00:03 65 11 26 02 BC 6C AA 20 76 93 51 53 50 76 13 48...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.