Jump to content
xisto Community
Sign in to follow this  
iGuest

Php Interpolation When to use single/double quotes

Recommended Posts

I just thought I'd bring this to the PHP developers, learners, and anyone else who is interested.Most people are still not building PHP web applications for performance but rather building web applications to get it working, but you must consider all areas in programming, especially performance because you want speedy applications too.If you don't know the difference between:echo 'Hello, World!';andecho "Hello, World!"; // as pointed out by vujsathen you really need to pay attention to this, it's vital you understand interpolation.What is Interpolation:Interpolation is a way of inserting/modifying additional text. In programming terms it's easily explained like these BBCodes we have on this forum, anything between the BBCode tags are interpolated, meaning they are modified to be displayed however the BBCode tags have been defined to do. Other interpolated objects are HTML tags like strong, em, h1-h6, etc, anything like formatting tags are ways of interpolating text.This meaning is the same when used with double and single quoted strings, objects, etc in PHP. However, HTML tags used within any quotes will be considered HTML by the browser which will perform it's own interpolation on the objects.The difference between the single quoted text of Hello, World is that it's displayed as is, the PHP interpreter does not need to convert anything in this text, so it just displays the string by outputting each character it comes across.The double quoted text of Hello, World makes the interpretter work harder, because while it reads each character it comes across, it is also looking for anything that can be interpolated. Since there's nothing to be interpolated, why do some developers use double quotes?What I am wanting you to learn out of this, is when to use single quotes and double quotes.So single quotes does no interpolation, double quotes does.Here's an example that should hopefully show you what I mean.<?php$string1 = 'This string will not be interpolated\n<br />';$string2 = 'Part of this string will be interpolated ' . "\n" . '<br />';$string3 = "$string2";$string4 = '$string1<br />';$string5 = "This will be interpolated\n<br />";echo $string1 . $string2 . $string3 . $string4 . $string5;?>You should do this example and display it, so you can see for yourself what's happening, also view the source code, so you can see whether newlines were created or not.Now what happens in $string1 is very important. You're using the escaped character \n which means newline, however, newlines need to be interpolated and since single quotes do not provide interpolation, you will get the exact string as it is except for the <br /> since the web browser handles HTML tags it will not show on the page, but it will show in the source code.So the output of string one will look like this in your browser:This string will not be interpolated\n$string2 uses a mixed method, single and double quoted strings that get concatenated (joined) together. This is quite acceptable, that way only interpolation will work inside the double quoted text, which is the newline character.$string3 stores the exact same as $string2, we didn't need to use double quotes for this we could have just done $string3 = $string2 which is the preferred method but since this is about interpolation, I needed to use double quotes to show you. The double quotes interpolated $string2 the variable and came up with what $string2 stored.$string4 is not interpolated it will just display:$string1I also had to add the <br /> tag on it, because it would not have had this tag.$string5 uses double quotes for the whole string and requires interpolation only on a small part in this string, but still uses interpolation for the newline.Now you can choose what ways you want to do, but non interpolation should use single quotes and interpolation should use double quotes. I prefer $string2's method with concatenating the strings but sometimes this can get quite messy that using double quotes should be ok, but only if it has a lot of things to be interpolated.e.g.echo 'Hello, ' . "\n\t" . 'World!' . "\t" . 'This can get quite messy' . "\n\t" . 'especially if you need to interpolate quite a few things at once.' . "\n";echo "Hello, \n\tWolrd!\tThis is still kinda messy\n\tbut it's also a lot easier\n";We're using the newline and tab escaped characters in these strings, and after a while, it just becomes messy. Concatenating strings to this extent can also be a performance issue, so we must choose wisely.The main thing I wanted to point out is if it doesn't do interpolation, don't use double quotes for it, if it does, then it's ok. If you know when a string needs interpolation then this is better, e.g. you don't need interpolation in $_SERVER['host']; but you do if it's $_SERVER["$some_var"];So hopefully from this you will understand what interpolation is, and when to use it and when not to use it. I may write a benchmarking PHP application to show you how fast it was to process an interpolated string and a non interpolated string, this speed test is however an estimation, but to my understanding, any non interpolated string should be processed faster than an interpolated string.Cheers,MC

Share this post


Link to post
Share on other sites

echo 'Hello, World!';

 

and

 

echo "Hello, World!;

<{POST_SNAPBACK}>

"

Very nice tutorial. Explains why I couldn't get the newline (\n) character to work the other day.

"

Very interesting subject. You're like a walking PHP textbook. :rolleyes:

"

I'll start using these techniques from now on. Thanks for the info.

"

I would like for you to go into more detail about concatenating strings.

"

When do we use a * period and when do we use a (,) comma. I was thinking that commas are used when inserting a function call into a string and the period is to join strings to strings, variables, and interpolated strings. Please correct me if I am wrong.

 

Anyway, did you catch that hint ?" - If you didn't, are you missing a double quote?

 

vujsa

Share this post


Link to post
Share on other sites

very interesting...but is all that really necessary these days when you can use apps like dreamweaver to create all your web content? do you still need to enter all those codes yourself?

Share this post


Link to post
Share on other sites

It is always important to know any language that you use a program to create. Programs are very inefficient, and sometimes generate code that needs modification for security reasons or because it doesn't do exactly what you want.Question: If I am echoing some html as such: echo "<img src = '$string'>"; then is the $string interpolated or not, and would it be better to use \" instead of '?~Viz

Share this post


Link to post
Share on other sites

The string will be interpolated.e.g.$string = 'smile.gif';echo "<img src='$string'>"; then the results will be <img src='smile.gif'>I have the understanding that browsers accept any single pair/double pair of quotes, but in this case I would do echo "<img src=\"$string\">"; just to keep it alike with the rest of the HTML, or we could do echo '<img src="' . $string . '">'; whatever you think is better. Escaping the double quotes would be my choice though, as inside the HTML tags, it could get pretty messy.DreamWeaver goes for working methods rather than performance. To me it would use double quotes if it's got it's own methods of inserting PHP code.I prefer to know every bit of code indepth than rely on a programs method. Sometimes the programs can make a mess of things. In saying this, even C/C++ compilers have their flaws, now I haven't gone indepth into how the compiler works, but I look at their end result of the binary program, I disassemble it, and read the assembly opcodes, by this, I could rewrite parts of the code to make it smaller and faster, another advantage of knowing ASM.So to some degree, you rely on your program, but not fully. As I said, the main concern for the program is to actually get your code working and this may mean, doing the easy way for it.Then again, I don't use DreamWeaver and it could be that amazing that I should use it instead, since it will do PHP how I want it to... somehow I don't think so, because even I don't know how I'll use PHP effectively and end up rewriting the same code, but using different methods looking for the best approach.Cheers,MC

Share this post


Link to post
Share on other sites

nothing new to me, knew that and always try to do it that way, but practically the speed does not change out of this, just test it with the execution time, unless the server is very slow, you could feel a difference, but i encourage people to write in this way anyway..same as the manual say that mt_rand() is 4 times faster than rand() but i read and tested it with execution time, no difference. :) but i stil use mt_rand()

Share this post


Link to post
Share on other sites

Hey manuleka,If I understand you correctly, you mean comparing between a ' ' and "\t",Single quotes should be faster in any case with comparing from the alternative double quotes, as I stated, double quotes makes the interpretter work harder as it is also searching inside the quoted string for anything that can be interpolated, single quotes does not request this of the interpretter. The performance would be marginal for small things like this, but imagine large scale programs with over 100,000 lines of code, you'll really notice the difference now, so it's better to get in the habit in an early stage than to change your bad habits.If you mean whitespace as in should we use spaces instead of tabs for indentation?Now this has been talked about and the preferred method is spaces, but tabs would be faster for a compiler or interpretter as each space = 1 byte, a tab key is 1 byte, so if you used 4 spaces for indenting you're using 4 bytes that the compiler/interpretter must go through, if you just use a tab it only has 1 byte.Unfortunately, I am going to say we should use spaces for indenting, due to how tabs are interpretted by different editors, it can make the code appear out of alignment. The importance of your code is also it's readability, there are tonnes of guidelines on these issues of why we should do such and such a certain way.I try to conform to ANSI/ISO C/C++ standards for readability, I have looked at K&R techniques, GNU Programming, etc and even though GNU may have good reasons to write in such way, I still prefer ANSI/ISO as the standard, but if someone showed me source in GNU or K&R I can still read it no problems at all and won't argue why they should switch, although with K&R I may suggest ANSI/ISO, because when trying to figure out the start and end of compound statements the curly braces do not align.I hope I answered your question, if not try to be more specific about what you were intending and I'll try to cover it.Cheers,MC

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.