php-general Digest 15 Dec 2011 00:16:15 -0000 Issue 7610
Topics (messages 316007 through 316018):
Preferred Syntax
316007 by: Rick Dwyer
316008 by: Tedd Sperling
316009 by: Marc Guay
316010 by: Al
316011 by: Peter Ford
316012 by: David Harkness
316013 by: Tamara Temple
316014 by: Robert Cummings
316015 by: admin.buskirkgraphics.com
316016 by: Adam Richardson
316017 by: admin.buskirkgraphics.com
Re: Unique items in an array
316018 by: Jim Lucas
Administrivia:
To subscribe to the digest, e-mail:
php-general-digest-subscr...@lists.php.net
To unsubscribe from the digest, e-mail:
php-general-digest-unsubscr...@lists.php.net
To post to the list, e-mail:
php-gene...@lists.php.net
----------------------------------------------------------------------
--- Begin Message ---
Hello all.
Can someone tell me which of the following is preferred and why?
echo "<a style='text-align:left;size:14;font-weight:bold' href='/
mypage.php/$page_id'>$page_name</a><br>";
echo "<a style='text-align:left;size:14;font-weight:bold' href='/
mypage.php/".$page_id."'>".$page_name."</a><br>";
When I come across the above code in line 1, I have been changing it
to what you see in line 2 for no other reason than it delineates out
better in BBEdit. Is this just a preference choice or is one method
better than the other?
--Rick
--- End Message ---
--- Begin Message ---
On Dec 14, 2011, at 7:59 AM, Rick Dwyer wrote:
> Hello all.
>
> Can someone tell me which of the following is preferred and why?
>
> echo "<a style='text-align:left;size:14;font-weight:bold'
> href='/mypage.php/$page_id'>$page_name</a><br>";
>
> echo "<a style='text-align:left;size:14;font-weight:bold'
> href='/mypage.php/".$page_id."'>".$page_name."</a><br>";
>
> When I come across the above code in line 1, I have been changing it to what
> you see in line 2 for no other reason than it delineates out better in
> BBEdit. Is this just a preference choice or is one method better than the
> other?
>
> --Rick
Neither.
My advice, take all the style elements out of the anchor tag.
echo("<a href='/mypage.php/$page_id'>$page_name</a>");
Even the '<br>' can be (perhaps should be) handled by css.
Cheers,
tedd
_____________________
t...@sperling.com
http://sperling.com
--- End Message ---
--- Begin Message ---
> BBEdit. Is this just a preference choice or is one method better than the
> other?
As far as I know it's just preference. Your choice of editor could
influence your decision; one form might be given nicer highlighting.
Marc
--- End Message ---
--- Begin Message ---
On 12/14/2011 7:59 AM, Rick Dwyer wrote:
Hello all.
Can someone tell me which of the following is preferred and why?
echo "<a style='text-align:left;size:14;font-weight:bold'
href='/mypage.php/$page_id'>$page_name</a><br>";
echo "<a style='text-align:left;size:14;font-weight:bold'
href='/mypage.php/".$page_id."'>".$page_name."</a><br>";
When I come across the above code in line 1, I have been changing it to what you
see in line 2 for no other reason than it delineates out better in BBEdit. Is
this just a preference choice or is one method better than the other?
--Rick
This not a PHP subject and should not be here.
However, styles should be in the style block or better in the styles CSS file.
Spend some time learning about CSS3 and modern techniques.
--- End Message ---
--- Begin Message ---
On 14/12/11 16:48, Al wrote:
On 12/14/2011 7:59 AM, Rick Dwyer wrote:
Hello all.
Can someone tell me which of the following is preferred and why?
echo "<a style='text-align:left;size:14;font-weight:bold'
href='/mypage.php/$page_id'>$page_name</a><br>";
echo "<a style='text-align:left;size:14;font-weight:bold'
href='/mypage.php/".$page_id."'>".$page_name."</a><br>";
When I come across the above code in line 1, I have been changing it
to what you
see in line 2 for no other reason than it delineates out better in
BBEdit. Is
this just a preference choice or is one method better than the other?
--Rick
This not a PHP subject and should not be here.
However, styles should be in the style block or better in the styles CSS
file.
Spend some time learning about CSS3 and modern techniques.
With respect to tedd and Al, you've misread the question: the important
PHP-related bit is about whether to embed variables in double-quoted strings or
to concatenate them. These are only two of the options, and each has it's pros
and cons. There has been (some time ago) plenty of discussion on this list about
this sort of thing, including (ISTR) a timed test of the performance
implications of various forms - that only really matters in big loops, of course...
Horses for courses. I use whatever I feel like at the time, and mix various
styles, as long as it's readable!
Cheers
Pete
--
Peter Ford, Developer phone: 01580 893333 fax: 01580 893399
Justcroft International Ltd. www.justcroft.com
Justcroft House, High Street, Staplehurst, Kent TN12 0AH United Kingdom
Registered in England and Wales: 2297906
Registered office: Stag Gates House, 63/64 The Avenue, Southampton SO17 1XS
--- End Message ---
--- Begin Message ---
On Wed, Dec 14, 2011 at 4:59 AM, Rick Dwyer <rpdw...@earthlink.net> wrote:
> Can someone tell me which of the following is preferred and why?
>
> echo "<a style='text-align:left;size:**14;font-weight:bold'
> href='/mypage.php/$page_id'>$**page_name</a><br>";
>
> echo "<a style='text-align:left;size:**14;font-weight:bold'
> href='/mypage.php/".$page_id."**'>".$page_name."</a><br>";
>
On Wed, Dec 14, 2011 at 9:09 AM, Peter Ford <p...@justcroft.com> wrote:
> Horses for courses. I use whatever I feel like at the time, and mix
> various styles, as long as it's readable!
I agree with Peter here. I would bet that the string with embedded
variables is parsed once when the file is loaded and turned into the same
bytecode as the second form.
If you are going to use the second style above, I would at least switch the
quotes around so you can use the double-quotes in the HTML as that to me
reads nicer and avoids the minor cost of scanning the string for embedded
code. I also put spaces around the dots to make the variable concatenation
easier to spot when skimming it.
echo '<a style="text-align:left;size:**14;font-weight:bold"
href="/mypage.php/' . $page_id . '">' . $page_name . '</a><br>';
David
--- End Message ---
--- Begin Message ---
Tedd Sperling <tedd.sperl...@gmail.com> wrote:
> On Dec 14, 2011, at 7:59 AM, Rick Dwyer wrote:
>
> > Hello all.
> >
> > Can someone tell me which of the following is preferred and why?
> >
> > echo "<a style='text-align:left;size:14;font-weight:bold'
> > href='/mypage.php/$page_id'>$page_name</a><br>";
> >
> > echo "<a style='text-align:left;size:14;font-weight:bold'
> > href='/mypage.php/".$page_id."'>".$page_name."</a><br>";
> >
> > When I come across the above code in line 1, I have been changing it to
> > what you see in line 2 for no other reason than it delineates out better in
> > BBEdit. Is this just a preference choice or is one method better than the
> > other?
> >
> > --Rick
>
> Neither.
>
> My advice, take all the style elements out of the anchor tag.
>
> echo("<a href='/mypage.php/$page_id'>$page_name</a>");
>
> Even the '<br>' can be (perhaps should be) handled by css.
I think you may have missed the question. The OP was asking whether:
"text $var text"
or
"text ".$var." text"
was preferrable.
For me, I tend to use the first unless the second makes things clearer
where they need to be. Syntax hilighing can easily be one of those
times.
--- End Message ---
--- Begin Message ---
On 11-12-14 01:10 PM, David Harkness wrote:
On Wed, Dec 14, 2011 at 4:59 AM, Rick Dwyer<rpdw...@earthlink.net> wrote:
Can someone tell me which of the following is preferred and why?
echo "<a style='text-align:left;size:**14;font-weight:bold'
href='/mypage.php/$page_id'>$**page_name</a><br>";
echo "<a style='text-align:left;size:**14;font-weight:bold'
href='/mypage.php/".$page_id."**'>".$page_name."</a><br>";
On Wed, Dec 14, 2011 at 9:09 AM, Peter Ford<p...@justcroft.com> wrote:
Horses for courses. I use whatever I feel like at the time, and mix
various styles, as long as it's readable!
I agree with Peter here. I would bet that the string with embedded
variables is parsed once when the file is loaded and turned into the same
bytecode as the second form.
If you are going to use the second style above, I would at least switch the
quotes around so you can use the double-quotes in the HTML as that to me
reads nicer and avoids the minor cost of scanning the string for embedded
code. I also put spaces around the dots to make the variable concatenation
easier to spot when skimming it.
echo '<a style="text-align:left;size:**14;font-weight:bold"
href="/mypage.php/' . $page_id . '">' . $page_name .'</a><br>';
+1 on the use of single quotes instead of double quotes. With a bytecode
cache it's not really going to make a lick of difference, but using
double quotes for HTML seems far more palatable to me. I too like to
pull the variable out into code space. I also like to format my
attributes for easy reading and commenting:
echo '<a'
.' style="text-align:left;size:**14;font-weight:bold"'
.' href="/mypage.php/'.$page_id.'"'
.'>'
.$page_name
.'</a>'
.'<br />';
Although, I usually only do the above for tags that have lots of
attributes. Otherwise the following is more likely:
echo '<div class="some-class">'
.'Something something'
.'</div>';
As I said before though, a bytecode cache with any degree of
optimization "should" make any particular style have zero impact on
runtime (beyond original parse) by optimizing the strings into a minimal
set of operations. For instance 'foo'.'fee' would be coverted to
'foofee' by the bytecode engine.
Cheers,
Rob.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.
--- End Message ---
--- Begin Message ---
> -----Original Message-----
> From: Tamara Temple [mailto:tamouse.li...@tamaratemple.com]
> Sent: Wednesday, December 14, 2011 1:40 PM
> To: Tedd Sperling
> Cc: Rick Dwyer; PHP-General
> Subject: Re: [PHP] Preferred Syntax
>
> Tedd Sperling <tedd.sperl...@gmail.com> wrote:
> > On Dec 14, 2011, at 7:59 AM, Rick Dwyer wrote:
> >
> > > Hello all.
> > >
> > > Can someone tell me which of the following is preferred and why?
> > >
> > > echo "<a style='text-align:left;size:14;font-weight:bold'
> href='/mypage.php/$page_id'>$page_name</a><br>";
> > >
> > > echo "<a style='text-align:left;size:14;font-weight:bold'
> href='/mypage.php/".$page_id."'>".$page_name."</a><br>";
> > >
> > > When I come across the above code in line 1, I have been changing
> it to what you see in line 2 for no other reason than it delineates out
> better in BBEdit. Is this just a preference choice or is one method
> better than the other?
> > >
> > > --Rick
> >
> > Neither.
> >
> > My advice, take all the style elements out of the anchor tag.
> >
> > echo("<a href='/mypage.php/$page_id'>$page_name</a>");
> >
> > Even the '<br>' can be (perhaps should be) handled by css.
>
> I think you may have missed the question. The OP was asking whether:
>
> "text $var text"
>
> or
>
> "text ".$var." text"
>
> was preferrable.
>
> For me, I tend to use the first unless the second makes things clearer
> where they need to be. Syntax hilighing can easily be one of those
> times.
>
>
> --
The key thing to remember here is that this is a preference and not a
performance thing.
--- End Message ---
--- Begin Message ---
On Wed, Dec 14, 2011 at 7:59 AM, Rick Dwyer <rpdw...@earthlink.net> wrote:
> Hello all.
>
> Can someone tell me which of the following is preferred and why?
>
> echo "<a style='text-align:left;size:**14;font-weight:bold'
> href='/mypage.php/$page_id'>$**page_name</a><br>";
>
> echo "<a style='text-align:left;size:**14;font-weight:bold'
> href='/mypage.php/".$page_id."**'>".$page_name."</a><br>";
>
> When I come across the above code in line 1, I have been changing it to
> what you see in line 2 for no other reason than it delineates out better in
> BBEdit. Is this just a preference choice or is one method better than the
> other?
>
I prefer sending arguments to the echo language construct (note, if you
send more than one argument, you can't use parentheses.) I perceive this
usage to be a clean presentation of the code's intent, easy to use in most
IDE's, and it's very fast relative to the other options:
echo "<a style='text-align:left;size:**14;font-weight:bold'
href='/mypage.php/$page_id'>$**page_name</a><br>";
echo "<a style='text-align:left;size:**14;font-weight:bold'
href='/mypage.php/".$page_id."**'>".$page_name."</a><br>";
echo "<a style='text-align:left;size:**14;font-weight:bold'
href='/mypage.php/", $page_id, "'>", $**page_name, "</a><br>";
And, for longer lines, I'll often break it up into separate lines by
argument like below:
echo
"<a style='text-align:left;size:**14;font-weight:bold'
href='/mypage.php/",
$page_id,
"'>",
$**page_name,
"</a><br>";
That all said, I don't change code that uses another convention, as I think
it's most beneficial to stay with the established conventions in any
codebase (unless you're establishing a new convention and refactoring the
entire code base.) This is just my general preference, and I don't believe
there is consensus as to the most appropriate.
Adam
--
Nephtali: A simple, flexible, fast, and security-focused PHP framework
http://nephtaliproject.com
--- End Message ---
--- Begin Message ---
> -----Original Message-----
> From: Adam Richardson [mailto:simples...@gmail.com]
> Sent: Wednesday, December 14, 2011 2:19 PM
> To: Rick Dwyer
> Cc: PHP-General
> Subject: Re: [PHP] Preferred Syntax
>
> On Wed, Dec 14, 2011 at 7:59 AM, Rick Dwyer <rpdw...@earthlink.net>
> wrote:
>
> > Hello all.
> >
> > Can someone tell me which of the following is preferred and why?
> >
> > echo "<a style='text-align:left;size:**14;font-weight:bold'
> > href='/mypage.php/$page_id'>$**page_name</a><br>";
> >
> > echo "<a style='text-align:left;size:**14;font-weight:bold'
> > href='/mypage.php/".$page_id."**'>".$page_name."</a><br>";
> >
> > When I come across the above code in line 1, I have been changing it
> to
> > what you see in line 2 for no other reason than it delineates out
> better in
> > BBEdit. Is this just a preference choice or is one method better
> than the
> > other?
> >
>
> I prefer sending arguments to the echo language construct (note, if you
> send more than one argument, you can't use parentheses.) I perceive
> this
> usage to be a clean presentation of the code's intent, easy to use in
> most
> IDE's, and it's very fast relative to the other options:
>
> echo "<a style='text-align:left;size:**14;font-weight:bold'
> href='/mypage.php/$page_id'>$**page_name</a><br>";
>
> echo "<a style='text-align:left;size:**14;font-weight:bold'
> href='/mypage.php/".$page_id."**'>".$page_name."</a><br>";
>
> echo "<a style='text-align:left;size:**14;font-weight:bold'
> href='/mypage.php/", $page_id, "'>", $**page_name, "</a><br>";
>
> And, for longer lines, I'll often break it up into separate lines by
> argument like below:
>
> echo
> "<a style='text-align:left;size:**14;font-weight:bold'
> href='/mypage.php/",
> $page_id,
> "'>",
> $**page_name,
> "</a><br>";
>
> That all said, I don't change code that uses another convention, as I
> think
> it's most beneficial to stay with the established conventions in any
> codebase (unless you're establishing a new convention and refactoring
> the
> entire code base.) This is just my general preference, and I don't
> believe
> there is consensus as to the most appropriate.
>
> Adam
>
> --
> Nephtali: A simple, flexible, fast, and security-focused PHP framework
> http://nephtaliproject.com
Adam,
You are very correct, the last discussions and testing of theories
caused my head to hurt!!!
Neither side gained ground in testing or discussion.
Having said that to me it is a preference only and not a performance
enhancing or degrading factor in syntax.
--- End Message ---
--- Begin Message ---
On 12/13/2011 1:15 PM, Marc Guay wrote:
> Hi folks,
>
> Let's say that I have the following array:
>
> [0]=>
> array(35) {
> ["contact_id"]=>
> string(3) "356"
> ["contact_first_name"]=>
> string(4) "Marc"
> }
> [1]=>
> array(35) {
> ["contact_id"]=>
> string(3) "247"
> ["contact_first_name"]=>
> string(4) "Marc"
> }
> [2]=>
> array(35) {
> ["contact_id"]=>
> string(3) "356"
> ["contact_first_name"]=>
> string(4) "Marc"
> }
>
> And I would like to filter out exact duplicates, such as key 0 and key
> 2 in this example, leaving me with an array containing only unique
> entries. How would you go about it?
>
> Thanks for any help,
> Marc
>
Assuming you want to make things unique based on the "contact_first_name" field,
how would you decide which record to keep? The first one you run in to, the
last one you come across, or some other criteria?
--
Jim Lucas
http://www.cmsws.com/
http://www.cmsws.com/examples/
http://www.bendsource.com/
--- End Message ---