understanding the use of Brackets in this function ()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

understanding the use of Brackets in this function ()

Post 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 .
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post by Celauran »

Because that's how the output is meant to be formatted?

[text]Book: Moby Dick (635 pages)[/text]
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post by pickle »

Yeah - the parentheses do absolutely nothing in PHP-land. The parentheses are entirely meant simply to be output as parentheses.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

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

Post by gautamz07 »

Thanks Guys ! Perfect !!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post 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.
(#10850)
Post Reply