Jump to content

What does this mean?


davidthygreat

Recommended Posts

 

I have this code within this exercise im doing trying to teach myself php,

 

i don't understand why there are slashes used in this echo statement could someone please tell me why you would need to use slashes in this statement this is reall

confusing.

 

echo "<input type=\"hidden\" name=\"travel[]\" value=\"$t\" />\n";

 

thanks

Link to comment
Share on other sites

There are a few different ways to make strings in php.  I'm not going to write a book on it right now, but the main ones are:

 

$string = "Hello there";
$string2 = 'Hello there too';

 

So you can see that either single or double quotes delimits the start and end of the string.  So the obvious question becomes... how would i have a string like:

 

Hello there "friend"

 

With single quotes it would not be a problem.

 

$string = 'Hello there "friend"';

 

But if you tried to use double quotes, the parser would not know where you meant the string to end, and would generate a parsing error.

 

$string = "Hello there "friend""; // doesn't work

 

So when you want to embed characters in a string, you can use the escape character which in php is the backslash(\) to tell php that the character you're escaping (the following character) is a special character that should be treated as part of the string.

 

$string = "Hello there \"friend\"";

 

As you can see there are other escape characters like the "\n" which have special meaning in an interpolated php string (one created using enclosing "...")

 

 

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.