What is the difference :/ [SOLVED]

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Gunner
Posts: 276
Joined: Sun Aug 14, 2011 12:07 am

What is the difference :/ [SOLVED]

Post 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??
Last edited by Gunner on Mon Nov 28, 2011 12:10 pm, edited 1 time in total.
Skype: mrvictordiaz
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: What is the difference :/

Post 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
Fighting for peace is declaring war on war. If you want peace be peaceful.
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: What is the difference :/

Post 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.
User avatar
Gunner
Posts: 276
Joined: Sun Aug 14, 2011 12:07 am

Re: What is the difference :/

Post by Gunner »

So.. It seems both are okay and can be used properly in PHP isn't it? :)
Skype: mrvictordiaz
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: What is the difference :/

Post by Winawer »

Yes, it's mainly a matter of preference.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: What is the difference :/ [SOLVED]

Post 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.
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: What is the difference :/ [SOLVED]

Post by hallsofvallhalla »

this IS NOT recommended

Code: Select all

$Love = "Love";
echo "I $Love You";
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: What is the difference :/ [SOLVED]

Post 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?
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: What is the difference :/ [SOLVED]

Post 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.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: What is the difference :/ [SOLVED]

Post by Jackolantern »

Very true. About the only time I have ever used it is when concatenating would have made it extremely complicated.
The indelible lord of tl;dr
Post Reply

Return to “Beginner Help and Support”