adrumsolo4u Posted February 23, 2007 Share Posted February 23, 2007 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 More sharing options...
artacus Posted February 23, 2007 Share Posted February 23, 2007 escape your ' like so \' Link to comment https://forums.phpfreaks.com/topic/39812-php-echo/#findComment-192286 Share on other sites More sharing options...
adrumsolo4u Posted February 23, 2007 Author Share Posted February 23, 2007 how would i implement that in the string, without changing the values? is echo the only way to print? Link to comment https://forums.phpfreaks.com/topic/39812-php-echo/#findComment-192290 Share on other sites More sharing options...
JBS103 Posted February 23, 2007 Share Posted February 23, 2007 You could use print, but I don't think it's gonna do much for you in this situation. What artacus posted is usually how its handled. Link to comment https://forums.phpfreaks.com/topic/39812-php-echo/#findComment-192309 Share on other sites More sharing options...
shank888 Posted February 23, 2007 Share Posted February 23, 2007 try this: echo "<a href='file.php?menu=openclose'><h1>Home</h1></a>"; Link to comment https://forums.phpfreaks.com/topic/39812-php-echo/#findComment-192319 Share on other sites More sharing options...
soycharliente Posted February 23, 2007 Share Posted February 23, 2007 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 More sharing options...
Yesideez Posted February 23, 2007 Share Posted February 23, 2007 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.