March 28, 2012

What does '" actually mean in PHP Syntax?

Question by Christus Cleetus

I have a piece of code and i keep getting syntax errors for codes like thess :

$query ="SELECT * from `jos_menu` where `id` = ".'".$parent.'";

Now when i reformat it as :

$query ="SELECT * from `jos_menu` where `id` = ".$parent;

That is when i remove : ‘”
it works fine. So i am just wondering, what does (‘”) actually do ???

Answer by Burhan Khalid

The only problem with

$query ="SELECT * from `jos_menu` where `id` = ".'".$parent.'";

Is that you missed a few ':

$query ="SELECT * from `jos_menu` where `id` = ".'"'.$parent.'"';

In PHP, a string can either be:

$var = 'This is a string';

Or

$var = "This is a string";

If you want to put " inside a string that you already started with ", you need tell PHP that you don’t want your second " to end the string but use the character " as part of the string itself. This is what " does. It tells PHP that Don’t give the " character any special meaning; since normally if you started the string with ", the next " would end the string.

means remove any “special” meaning to the next character

This only works if the character after the would have had special meaning. Some examples:

Suppose we want to print Hello "World". I am a string!:

$var = "Hello "World". I am a string!";

In this example we will have errors. Since we started the string with ", the next " will close the string. So what PHP thinks:

  1. " Start of string
  2. Hello part of string variable.
  3. " Hey, since I saw that the string was started with ", this must mean the end of it!
  4. World" <– Error

Stop processing and throw errors.

However, if we write:

$var = "Hello "World". I am a string!";

Now, PHP thinks:

  1. " Start of string
  2. Hello part of string variable
  3. Ah, okay, the next character I should remove any special meaning
  4. " Okay, this is immediately after , so I just use it normally, as a ".
  5. World part of string
  6. Okay, the next character I will remove any special meaning
  7. " This is now a normal "
  8. . I am a string! – part of string variable.
  9. " Ah! Since the string was started with ", this must be the ending.
  10. ; ends statement.

Hopefully this clarifies things for you.

Answer by Starx

It denotes escaped characters. The next character that appear after it, will be taken as its current form.

Your Query is incorrectly escaped

$query ="SELECT * from `jos_menu` where `id` = ".'".$parent.'";
                                               //^ You mismatched the quotes from here

A correctly escaped query should be

$query ="SELECT * from `jos_menu` where `id` = "$parent"";
                                           //  ^ Note here " will printed as it is within the query

For example,
If $parent was 2, then the query would be

 SELECT * from `jos_menu` where `id` = "2"

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!