Jump to content

Recommended Posts

Posted

This is probably an easy one! I'm trying to list pages with a given tag. But I need that tag to be a variable.

It works without the variable:

foreach ($page->find('template=placemarks, tag=1009') as $child){

...but this doesn't work:

$tag="1009";
foreach ($page->find('template=placemarks, tag=$tag') as $child){

What am I doing wrong?

Posted

Use double quotes:

$tag="1009"; // quotes are not required here by the way, using them just means that you'll get string "1009" instead of integer 1009.. doesn't really matter in this context though so use whichever you prefer 
foreach ($page->find("template=placemarks, tag=$tag") as $child){
Posted

This is probably an easy one! I'm trying to list pages with a given tag. But I need that tag to be a variable.

It works without the variable:

foreach ($page->find('template=placemarks, tag=1009') as $child){

...but this doesn't work:

$tag="1009";
foreach ($page->find('template=placemarks, tag=$tag') as $child){

What am I doing wrong?

Or this (I'm a fan of single quotes, because it's generally a little bit faster, because the PHP parser has less to do Edit: not true anymore, if it every was - but single quotes do look much better in my favorite IDE. Thanks to teppo for myth busting  :)

$page->find('template=placemarks, tag=' . $tag)
Posted

Or this (I'm a fan of single quotes, because it's generally a little bit faster, because the PHP parser has less to do):

Not that it matters much, but you might want to reconsider that statement: http://nikic.github.io/2012/01/09/Disproving-the-Single-Quotes-Performance-Myth.html.

Quotes don't matter, but concat method ("foo" . $var . "bar") on the other hand does tend to be slightly easier on eyes because many editors won't actually recognize a variable inside quotes (ie. string interpolation) as something that should / could be highlighted.

So, essentially, use whichever you prefer. Just wanted to point out that micro-optimizations like these are a) futile and b) often misunderstood -- and thus generally not worth even thinking about :)

  • Like 3
Posted

micro-optimizations like these are a) futile

Can't recall where I saw it, but earlier this week someone suggested that finding a way to remove 1 jpeg from a site design is the quickest and easiest optimisation possible.

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...