Page 1 of 1

What is the difference :/ [SOLVED]

Posted: Mon Nov 28, 2011 9:39 am
by Gunner
Hi!
Sorry for interrupting this community for many times, because I'm the real noob in this community :/

I want to ask, what is the difference between the first script and the second script, although both is running same and properly.

First script:

Code: Select all

$Love = "Love";
echo "I" . $Love . "You";
I often seen this on halls script, and I confuse what are they for

Second script:

Code: Select all

$Love = "Love";
echo "I $Love You";
I think this is the simplest way to echo. And this, is my way, the newbie style. The easiest way :D

So can anybody explain it to me??

Re: What is the difference :/

Posted: Mon Nov 28, 2011 10:01 am
by Chris
It generally depends on the programmer. Single quotation marks are used in languages to wrap around strings more often than double quotations. The thing about PHP is variables are started with a $, giving PHP the possibility to still make use of variables in certain situations. In other languages variables do not start with a $, and therefor for cannot be placed in the middle of a string and be expected to work. I also heard that using double quotes takes longer to process than if you are to use single quotes, this is down to how PHP processes strings when wrapped in double quotes.

Maybe this will make more sense:

Code: Select all

$str = 'Hello World';
echo $str; // Hello World
echo "$str"; // Hello World
echo '$str'; // $str

$str = 'World';
echo 'Hello ' . $str; // Hello World <-- I find best practice
echo "Hello " . Sstr; // Hello World
echo "Hello $str"; // Hello World
echo 'Hello $str'; // Hello $str
 
No if we were to try the same in JavaScript for example:

Code: Select all

var str = 'Hello World';
alert( str ); // Hello World
alert( "str" ); // str
alert( 'str' ); // str 

str = 'World';
alert( 'Hello ' + str ); // Hello World
alert( "Hello " + str ); // Hello World
alert( "Hello str" ); // Hello str
alert( 'Hello str' ); // Hello str

Re: What is the difference :/

Posted: Mon Nov 28, 2011 10:22 am
by Winawer
There's also the following

Code: Select all

$str = 'World';
echo 'Hello ', $str; 
Saves PHP from doing a string concatenation and is still very readable IMO.

Re: What is the difference :/

Posted: Mon Nov 28, 2011 11:10 am
by Gunner
So.. It seems both are okay and can be used properly in PHP isn't it? :)

Re: What is the difference :/

Posted: Mon Nov 28, 2011 11:57 am
by Winawer
Yes, it's mainly a matter of preference.

Re: What is the difference :/ [SOLVED]

Posted: Tue Nov 29, 2011 7:15 am
by Jackolantern
People that come from other languages to PHP will typically use the 2nd method of concatenating the strings together out of habit (I do), because most other languages do not allow for the 1st method. So yes, as others said, it is just a matter of preference, and PHP/MySQL has many other more serious bottlenecks to consider so performance will basically be a non-issue here. It usually will just depend on what your first language was (or basically just whether or not it was PHP) and what you are more comfortable with.

Re: What is the difference :/ [SOLVED]

Posted: Thu Dec 01, 2011 8:58 pm
by hallsofvallhalla
this IS NOT recommended

Code: Select all

$Love = "Love";
echo "I $Love You";

Re: What is the difference :/ [SOLVED]

Posted: Thu Dec 01, 2011 10:44 pm
by Jackolantern
It is slower, for sure, but with so many other bottlenecks, string parsing times are not likely a big concern. Is there any other reason that method is not recommended?

Re: What is the difference :/ [SOLVED]

Posted: Fri Dec 02, 2011 4:01 am
by hallsofvallhalla
very ugly, hard to search code, not standard. lazy, and to be honest and does not always work. Not sure if it is older versions of PHP but I have seen it actually print $variable. instead of whats in the variable.

Re: What is the difference :/ [SOLVED]

Posted: Fri Dec 02, 2011 7:21 pm
by Jackolantern
Very true. About the only time I have ever used it is when concatenating would have made it extremely complicated.