Page 1 of 1

understanding the use of Brackets in this function ()

Posted: Thu Dec 04, 2014 2:13 am
by gautamz07
check out this display function guys :

Code: Select all

public function display()
{
echo "<p>Book: $this->_title ($this->_pageCount pages)</p>";
}
now have a closer look at the echo statement :

echo "<p>Book: $this->_title ($this->_pageCount pages)</p>";


Why has the coder chosen to leave :

Code: Select all

$this->_title
outside the brackets while putting :

Code: Select all

$this->_pageCount pages
inside brackets .

Re: understanding the use of Brackets in this function ()

Posted: Thu Dec 04, 2014 6:38 am
by Celauran
Because that's how the output is meant to be formatted?

[text]Book: Moby Dick (635 pages)[/text]

Re: understanding the use of Brackets in this function ()

Posted: Fri Dec 05, 2014 10:59 am
by pickle
Yeah - the parentheses do absolutely nothing in PHP-land. The parentheses are entirely meant simply to be output as parentheses.

Re: understanding the use of Brackets in this function ()

Posted: Sun Dec 07, 2014 11:02 pm
by gautamz07
Thanks Guys ! Perfect !!

Re: understanding the use of Brackets in this function ()

Posted: Mon Dec 08, 2014 11:30 am
by Christopher
gautamz07 wrote:check out this display function guys :

Code: Select all

public function display()
{
echo "<p>Book: $this->_title ($this->_pageCount pages)</p>";
}
I would recommend always using braces for object properties and array elements within double-quoted strings:

Code: Select all

public function display()
{
echo "<p>Book: {$this->_title} ({$this->_pageCount} pages) also {$arr[0]}</p>";
}
Using braces eliminates any possible parsing ambiguities and would make it clear here what the programmer intended.