Jump to content

Single/double quotation marks


NoUser2U

Recommended Posts

Hi,I've been stumbling upon this 'problem' for quite some times now. I'm currently just trying to learn php better then simply 'the basics', and am now using the filesystem functions (fopen, fread, fwrite etc.)Let's take fopen() for example.The php manual says the syntax is:

resource fopen ( string $filename , string $mode )

And i have this in my current script:

function write2file(){	$fileusers="C:/wamp/www/test/files/users.txt";	$fh=fopen($fileusers, "a");	fwrite($fh, "$user");	fclose($fh);}

The thing i don't know how to deal with, is the use of double and single quotation marks inside the parentheses of functions in general. For example, the php gives some examples of fopen() like so:example1: $handle = fopen("/home/rasmus/file.txt", "r");example2: $handle = fopen("http://www.example.com/", "r");In above 2 examples, both $filename and $mode are enclosed with double quotation marks.When i look for the fwrite() function (just for example) in the php manual, then the manual gives some examples regarding the use like so:example3: fwrite($fp, '1');example4: fwrite($fp, '23');Now my question is: When should i use single and double quotation marks INSIDE parentheses of functions? Why is the 'mode' in fopen() enclosed in double quotation marks and not in single quotation marks like in examples 3 and 4?? The use of single/double quotations marks regarding the use of echo() function is clear to me though.Much thanks in advance!

Link to comment
Share on other sites

I've been stumbling upon this 'problem' for quite some times now. I'm currently just trying to learn php better then simply 'the basics', and am now using the filesystem functions (fopen, fread, fwrite etc.)Now my question is: When should i use single and double quotation marks INSIDE parentheses of functions? Why is the 'mode' in fopen() enclosed in double quotation marks and not in single quotation marks like in examples 3 and 4?? The use of single/double quotations marks regarding the use of echo() function is clear to me though.
Single quotes and double quotes serve their own different purposes just the same in any context - not just in function parameters. When double quotes are used, variable interpolation takes place:
$var = 'hello';echo("$var"); //outputs 'hello'echo("{$var} world!"); //outputs 'hello world!'echo('$var world!'); //outputs '$var world!'echo($var.' world!'); //outputs 'hello world!'

Personally, I prefer using single quotes in most contexts, because in most contexts I don't actually need to use double quotes. It's been said that using single quotes is more efficient because PHP doesn't have to parse the string for potential variables, but the difference of execution time has been shown to be negligible.

Link to comment
Share on other sites

$var = 'hello';echo("$var"); //outputs 'hello'echo("{$var} world!"); //outputs 'hello world!'echo('$var world!'); //outputs '$var world!'echo($var.' world!'); //outputs 'hello world!'

Ah of course! that makes perfect sense! I didn't see that because until now i've always used the echo() function without the parenthesis, like so: <?php echo "Hello World"; ?> Thank you so much for this example! :)
Personally, I prefer using single quotes in most contexts, because in most contexts I don't actually need to use double quotes. It's been said that using single quotes is more efficient because PHP doesn't have to parse the string for potential variables, but the difference of execution time has been shown to be negligible.
That.... i didn't know. Thank you so much for your fast and great reply. My question has been fully answered now!(BTW this forum is wonderful!! up until now i've been only getting helpful, motivated and mature replies! not a single forum i'm a member of is like this one! god bless this forum! :))
Link to comment
Share on other sites

Dude, (as I'm sure you know) stuff like FWIW and IMO existed long before texting, long before the web, even.Anyway, where it's available, I generally use file_put_contents. You can add arguments to it to specify appending and such. It makes code a little shorter, especially if you're a good programmer and test all your return values before moving on to the next procedure.

Link to comment
Share on other sites

FWIW = For What Its WorthTake it ur not a txting wiz? :)
Haha didn't know that. Maybe because english isn't my mother language hehe.
Dude, (as I'm sure you know) stuff like FWIW and IMO existed long before texting, long before the web, even.Anyway, where it's available, I generally use file_put_contents. You can add arguments to it to specify appending and such. It makes code a little shorter, especially if you're a good programmer and test all your return values before moving on to the next procedure.
Thnx about the file_put_contents tip. Didn't know that existed. I think i'll stick with the regular way to opening, writing to/reading from and closing a file, just to get more familiair with those functions.Thnx anyway! :)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...