Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0
This is the forum to discuss the Wrox book Beginning PHP5, Apache, and MySQL Web Development by Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K. Glass; ISBN: 9780764579660
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 13th, 2005, 11:02 AM
Registered User
 
Join Date: Jul 2005
Location: Cleveland, Ohio, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Please explain single and double quotes

I'm just picking up php, and I'm finding this book very helpful. I'm afraid I need a four-year-old explanation of quotation marks in code. In some cases, single- and double-quotes seem interchangeable, and in others not.

In Lasso, which I've been using for a couple of years, you use single-quotes always, unless you're creating HTML code.

How does a php coder decide which quotes to use?

Thanks,
Stephen

Reply With Quote
  #2 (permalink)  
Old July 15th, 2005, 04:51 AM
Authorized User
 
Join Date: Jun 2005
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

double quotes are used to tell php that the code nested inbetween has to be stored without any execution.

e.g. if you would store $var1 = $var2 it would place the content of $var2 into $var1.

if you did $var1 = "$var2" it would store the TEXT $var2 into the variable without yet replacing it by the value.

now let's say the text you are using needs to have quotation marks as well. This happens a lot when defining things in HTML. A correct form initiation would look like this in HTML: <form action="1.html" method="post">

now if you enter this into a variable you get some problems.

as echo $htmlcode; ($htmlcode = "<formaction="1.html" method ="post">") tells php NOT to execute:"

<form action=
method =
>

but it DOES tell php to execute:
1.html
post

this, of course, is not going to work. So they made two levels of quotation marks.

The double quotes are the main quotes. Level 1 if you will.
If it occurs that you need to enter quotes within the level 1 quotation period, without ending the quotation period. You can use the Level 2 quotes: The single quotes.
This is also why they are not interchangeable. The double quotes are prioritised.

There still is one more situation (known to me thusfar) where the single quotes are used:
to get certain information from arrays.
For example the superglobal: $_POST[]
if you want to get information out of $_POST[](or any other array/global) you can do it 2 diffrent ways.

statically:
$_POST['whatever'].

this will get back the value for whatever from the $_POST array.

dynamically:
$_POST[$whatever]
this will enter the value for whatever $whatever is holding at that moment.


the logic is probably obvious by now (i hope)

But just as a recap:
Normally use double quotes (the standard).
To use quotes within the quotes: singles
and for arrays: singles

good luck.

Reply With Quote
  #3 (permalink)  
Old July 15th, 2005, 08:19 AM
Registered User
 
Join Date: Jul 2005
Location: Cleveland, Ohio, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Great--thanks. So, why does it seem to be standard in the book to use single quotes when setting variables?

Page 124:
$font-color='red';

Page 131:
$movie_rating = '';

It almost looks like the single quotes are used normally, unless one is actually inserting a string literal:

Page 130:
$review_query = "SELECT ...(etc)";

On a related topic, there seems to be many ways of representing the SQL instructions.

For instance:

Page 94 in the printed book:
//create "movie" table
$movie = "CREATE TABLE movie (
  movie_id int(11) NOT NULL auto_increment,
  movie_name varchar(255) NOT NULL,
  movie_type tinyint(2) NOT NULL default 0,
  movie_year int(4) NOT NULL default 0,
  movie_leadactor int(11) NOT NULL default 0,
  movie_director int(11) NOT NULL default 0,
  PRIMARY KEY (movie_id),
  KEY movie_type (movie_type,movie_year)
)";

Yet on page 95:
$insert = "INSERT INTO movie (movie_id, movie_name, movie_type, " .
          "movie_year, movie_leadactor, movie_director) " .
          "VALUES (1, 'Bruce Almighty', 5, 2003, 1, 2), " .
          "(2, 'Office Space', 5, 1999, 5, 6), " .
          "(3, 'Grand Canyon', 2, 1991, 4, 3)";

I'd rather avoid typing a billion dots and quotes if I can. What exactly are the rules?

Thanks,
Stephen


Reply With Quote
  #4 (permalink)  
Old July 17th, 2005, 07:30 PM
Authorized User
 
Join Date: Jun 2005
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It does, probably, not matter which type of quotations you use when defining a single string.
It will only matter (as i explained above) when you start using quotes within quotes.

As both work in a WYSIWYG fashion the author(s) probably did not find it necessary to dedicate a chapter but rather use diffrent styles (to do the same thing) so you can understand what is happening logically.

On the other hand these could also be simply a result of the fact that they use different authors.

Every programmer has his own style.

while you may find the $var='red' simpler
another person will choose the $var="red" variant.
Truth remains, in fact, that they both do the same.

I would suggest following the book and try using diffrent methods of quotations in the excersises to see what the diffrences and/or limitations are of diffrent styles.

The neat thing about php is that it is a less strict language than most others.
Instead of having to define variables before giving it a value, you can do so on the fly.
Same goes for the syntaxis. To truly anderstand the logic of the quotations (which, in php, is rather trivial) You just need to try out all combinations to see if there's a diffrence (e.g. "''" to '""').
I advise to use a visual php code tool (PHPEdit www.waterproof.fr) where you can keep track of your quotations by use of color markings.

After all of this just pick the version you understand the most (or like best) and stick to it. I'll bet that it's different than my style ;).

hope this was helpful and clear as i'm totally spent today so if not everything is clear yet, the next answer surely will be.:D

Reply With Quote
  #5 (permalink)  
Old June 8th, 2006, 07:20 PM
Registered User
 
Join Date: Jun 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Actually, that's not entirely true.

When double quotes are used, they expand any variable placed with the double quotes to the value of that variable.

For example:

This:
Code:
$html = '<html>';  // It doesn't matter which one is used here.
                         // I'll explain why shortly.
echo "$html";
will output the following:
Code:
<html>
Single quotes, however, are used to contain literal strings. Everything inside single quotes is considered to be a literal, giving you a true WYSIWYG effect.

So, using the previous, example, if I do this:
Code:
$hmtl = '<html>';
echo '$html';
I won't get this:
Code:
<html>
but this instead
Code:
$html
which is not what I intended.

The reason it doesn't matter which string delimiter I use (either " or ') for the <html> tag is because the <html> tag is intended to be used as it is (i.e., a string literal). Since there are no metacharacters contained within the $html variable, I get the same string no matter which quote I use.

However... ;)

If I want to display a string that has a double quotation mark embedded in it, such as:
Code:
$html = "<input type="text" name="somevalue" value="default value" />";
I have problems, because PHP would consider the first embedded double quote mark as the end of the string.

I'd have to do this one of two ways:
    1. Use single quotes to "literalize" the string
    2. Use the \ (backslash) character to escape the embedded double quotes

Like so:
Code:
$html = '<input type="text" name="somevalue" value="default value" />';
or
Code:
$html = "<input type=\"text\" name=\"somevalue\" value=\"default value\" />";
Once you print either of those to STDOUT:
Code:
$html = '<input type="text" name="somevalue" value="default value" />';
echo $html;
or
Code:
$html = "<input type=\"text\" name=\"somevalue\" value=\"default value\" />";
echo $html;
you would get the same result.

For me, using double quotes is a little easier. That way, I don't have to worry about concatenating literal string values. In other words, instead of doing this:
Code:
$textvalue = 'PHP rocks!';
$html='<input type="text" name="somevalue" value="' . $textvalue . '" />';
echo $html;
I can do this instead:
Code:
$textvalue = 'PHP rocks!';
$html="<input type=\"text\" name=\"somevalue\" value=\"$textvalue\" />";
echo $html;
Of course, then you have to worry about escaping those pesky embedded double quote marks (or other metacharacters).

HTH
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help! i hate...double and single quotes... keita Classic ASP Basics 3 March 28th, 2007 11:12 AM
displaying single quotes and double quotes ren_123 Classic ASP Databases 2 February 22nd, 2006 02:17 PM
Double Quotes and Single Quotes Problem phungleon Classic ASP Basics 7 May 27th, 2004 01:44 PM
Storing single and double quotes in text roniestein Access 4 December 30th, 2003 05:09 PM
Getting single and double quotes into DB hoffmann Classic ASP Databases 9 December 4th, 2003 12:24 AM



All times are GMT -4. The time now is 02:46 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.