Jump to content

Newbie syntax question


Omirion

Recommended Posts

Sup,

Currently reading a book for PHP 5 and i can't understand why for example.

 

echo "$var1 - $var2<br />';

 

Why is the starting quote a double and the closing one a single?

 

and is there a difference?

 

well if you have a something that begins with a "  you have to have the same ending for that statement.  just like a ' has to have an ending.

 

this is really very useful for something like a SQL query

 

$query = "UPDATE table_name SET colum1 = '10' WHERE colum2 = 'pie' ";

 

as you can see at the beginning of the query I have double quotes  then I have another double quotes at the end.  any values in the query have single.  quotes.  Basically this allows you to have quotes inside of quotes.

 

I hope this makes sense...

Link to comment
Share on other sites

Yes, there is a different:

$string1 = "Hi";
echo "The string1 var is $string1"; // output: the string1 var is Hi
// Its not recommended to so that, the best way is to put a dot to seperate strings and vars
echo 'The string1 var is $string1'; // output: the string1 var is $string1

// Good way:
echo "The string1 var is " . $string1; // output: the string1 var is Hi
echo 'The string1 var is ' . $string1; // output: the string1 var is Hi

 

Another good thing to have this two is this:

 

echo "<span style="color:red">spanInnerHTML</span>"; // ERROR: double quotes within double quotes...

// Solution:
echo "<span style=\"color:red\">spanInnerHTML</span>"; // output: <span style="color:red">spanInnerHTML</span>
echo "<span style='color:red'>spanInnerHTML</span>"; // output: <span style='color:red'>spanInnerHTML</span>

Link to comment
Share on other sites

Another way of thinking of it is that it really isn't a difference. You can either use single or double quotes. But personally just to make it look cleaner for my own navigation of my code, and totally only my own preference, I tend to stick with double quotes and do something like this instead of how they use it... not withstanding certain cases of course that call for using single quotes... and you'll run into those as you play with PHP more trust me.  Anyways it doesn't really matter (most of the time that is, but windows servers tend to not like you mixing it... see Iscripts examples for soemthing I'm pretty sure windows servers puke on with php ) but I like to do that specific code they gave you in this manner instead:

echo $var1 - $var2."<br />";

or like this

echo $var1 - $var2.'<br />';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.