Quantcast
Channel: WP Woodshed
Viewing all articles
Browse latest Browse all 16

Troubleshooting Strings, Double Quotes, and the Simple Syntax of Variable Parsing

$
0
0

Though technically this could go in my previous post,  I opted to include it on its own, if only for my own mental clarity.

Here’s the nitty gritty, and then I’ll explain it after:

// THIS IS WRONG - no need to use double quotes like this
  function getInfo() {
      return "A" . $this->name . "is an" . $this->flavor "flavored fish. The world record weight is" . $this->record ".";
  }

  // DO THIS INSTEAD
  public function getInfo() {
      return "A $this->common_name is an $this->flavor flavored fish. The world record weight is $this->record_weight.";
  }

The first time around, I thought I had to split the string up into individual double-quoted sections. But that code didn’t work.

So, after some research, namely here as well as the Practical PHP book, I realized that I didn’t need to split the string up. In fact, that’s what was breaking it.

On the second try, I combined everything into one string, surrounded by quotes and including each variable (with the $ prefix, of course).

Problem solved. Lesson learned (at least for now).


Viewing all articles
Browse latest Browse all 16

Trending Articles