-
' and this "
I can't figure out how can to use this ' and " within one long string?
This is the part of the string that's causing the problem.
Code:
<li><a href="../rewind/">Where We've Been</a></li>
Actually & #39 ; worked (spaces are added to show it). Don't know there's another way though?
-
Try:
Code:
<li><a href="../rewind/">Where We\'ve Been</a></li>
If your using single quotes, and:
Code:
<li><a href=\"../rewind/\">Where We've Been</a></li>
If your using double.
-
-
Glad to help.
When your using single quotes, slash(' \ ') before single quotes.
" same same " double quotes, "same same ...." double quotes.
-
OO now I really get it hah. So I don't need to use the single quote (') when the text is containing the (") I can just stop its reading of it (escaping?) with the /" and keep it all with in a double quote. So now I have a new question when should I use the single quotes?
-
You can use single quotes anytime you like.
I would not use single quotes when including variables(unless needed), for example:
Code:
echo "Hello ".$var."!";
Is more characters then:
Code:
echo "Hello {$var}";
({ and } not needed...)
-
If you need to do this for HTML, chances are you should be breaking out of PHP parsing mode instead.
-
Or you could do something like this Twey:
PHP Code:
echo <<< HTML
<p>"Hello, it's a wonderful day!" says Nile...</p>
HTML;
-
What do you mean by breaking out of PHP parsing? I'm altering html with the php then echoing the final result.
-
Heredocs are also an option, but they screw up the formatting of the code (since you can't have whitespace before the end token), as well as being very slow. An output buffer would actually be more efficient.
bluewalrus: instead of:
Code:
<?php
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><title>';
echo get_title_text();
echo '</title></head><body><p>'
echo get_body_text();
echo '</p></body></html>';
?>
... or something silly like that, write:
Code:
<?php
$title = get_title_text();
$body = get_body_text();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>
<?php echo $title; ?>
</title>
</head>
<body>
<p>
<?php echo $body; ?>
</p>
</body>
</html>
It has four added benefits:- readability, since you can format the code nicely for output rather than worrying about what the containing PHP looks like;
- efficiency, since the whole thing doesn't have to be parsed as PHP;
- simplicity, as it doesn't require you to follow escaping rules for strings; and
- clear distinction between code and output, which provides three benefits of its own:
- allows you to store the output template in a different place (e.g. in a different file);
- allows you to completely change what's to be output at any point in the code; and
- allows you to start a session without worrying about whether the flow of code has caused output to happen at some point previously.
See also Smarty, which provides a mechanism and assorted tools for further separation of code and content.
-
Bluewalrus:
Twey means something like:
Code:
<?php
$var = "Hello";
if($var == "Hello"){
?>
You said Hello!
<?php
}
?>
-
Would that work for this? say $name was from a form on the previous page and was declared here.
PHP Code:
<?php
$var = "Hello";
if($var == "Hello"){
?>
You said Hello $name !
<?php
}
?>
----------------------------------------------------------------------
Code:
<?php
$var = "Hello";
if($var == "Hello"){
echo "You said Hello {$name} !";
}
?>
-
No - that would not work...
@Twey, it's .net not .org:
http://www.smarty.net/
-
Kind of, but think of it in terms of the HTML and format accordingly, and you need to re-enter PHP parsing mode to output a variable. PHP also includes an alternative syntax for blocks to make this more readable (you get an ADA-like 'comb'):
Code:
<?php
$var = 'Hello';
$name = 'John';
?>
And the output:
Code:
<?php if ($var === 'Hello'): ?>
You said hello, <?php echo $name; ?>!
<?php endif; ?>
Ah, you're right, Nile. Thanks! :)
Edit: When formatting, I like to indent PHP blocks one block above the current element, since they tend to semantically associate with the outer element:
Code:
<div>
<p>
<?php if ($var === 'Hello'): ?>
You said hello!
<?php else: ?>
You didn't say hello.
<?php endif; ?>
</p>
</div>
-
-
Only the $name bit didn't work; the idea bluewalrus was asking after (wrapping a piece of HTML in a PHP block) does. Sorry for the confusion; I've edited to make it clearer. :)
-
Ok.. sorry. :D
Also, your link has a .org, instead of .net(smarty).
-