Jump to content

php echo


adrumsolo4u

Recommended Posts

my question is, is there any other function besides echo, which has the same effect?

my goal is to write this code:

echo ' <a href="?menu='openclose'"><H1>Home</H1></a>';

the idea is to have the menu able to equal another value that changes.

echo doesn't let me do this because when i use the ' again, it closes the echo field, and i get a nasty result.

Link to comment
https://forums.phpfreaks.com/topic/39812-php-echo/
Share on other sites

Like artacus said, use the symbol \ to escape the character that would cause the string to end.

 

By putting a \ before a character, it tells whatever is reading it to treat whatever character is after that as that literal character. So if you are using single quotes to denote your string and you put \' somewhere inside the string, when it tries to read the string and gets to the point where you have \' it will read a single quote and know that it is supposed appear in the string and not close the string.

 

Make sense?

 

For example:

echo "<a href=\"http://www.charlieholder.com\" title=\"charlie holder\">charlie holder</a>";

 

Notice the \" is the string? It will not close the string, but allow the double quotes to appear in the string.

 

So the answer to your question would thus be:

echo ' <a href="?menu=\'openclose\'"><H1>Home</H1></a>';

 

@shank888:

You should always use " when wrapping tag attributes in HTML. ;)

Link to comment
https://forums.phpfreaks.com/topic/39812-php-echo/#findComment-192320
Share on other sites

When I use echo I use ' to contain what I want to send to the browser. That way I can use " without having to use \.

 

If you use " to contain the content then you can mostly include variables directly and the variable content will be inserted.

<?php echo "Your age is $age years"; ?>

If you use ' instead then you can't use variables directly (above example using ' would show "Your age is $ years") like you can above but instead have two ways of showing them.

<?php echo 'Your age is {$age} years'; ?>

Or second way:

<?php echo 'Your age is '.$age.' years'; ?>

Which is how I prefer to do it. Also, when using special characters (eg. \t for tab) you'll need to surround any special characters using ".

<?php echo "This is\ttabbed"; ?>

Link to comment
https://forums.phpfreaks.com/topic/39812-php-echo/#findComment-192342
Share on other sites

Archived

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

×
×
  • 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.