Log in

View Full Version : ' and this "



bluewalrus
01-11-2009, 07:08 PM
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.


<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?

Nile
01-11-2009, 07:10 PM
Try:


<li><a href="../rewind/">Where We\'ve Been</a></li>

If your using single quotes, and:


<li><a href=\"../rewind/\">Where We've Been</a></li>

If your using double.

bluewalrus
01-11-2009, 07:15 PM
OO okay thanks

Nile
01-11-2009, 07:20 PM
Glad to help.
When your using single quotes, slash(' \ ') before single quotes.
" same same " double quotes, "same same ...." double quotes.

bluewalrus
01-11-2009, 07:53 PM
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?

Nile
01-11-2009, 08:03 PM
You can use single quotes anytime you like.
I would not use single quotes when including variables(unless needed), for example:


echo "Hello ".$var."!";

Is more characters then:


echo "Hello {$var}";

({ and } not needed...)

Twey
01-11-2009, 08:05 PM
If you need to do this for HTML, chances are you should be breaking out of PHP parsing mode instead.

Nile
01-11-2009, 08:17 PM
Or you could do something like this Twey:


echo <<< HTML
<p>"Hello, it's a wonderful day!" says Nile...</p>
HTML;

bluewalrus
01-11-2009, 09:03 PM
What do you mean by breaking out of PHP parsing? I'm altering html with the php then echoing the final result.

Twey
01-11-2009, 09:04 PM
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:
<?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:
<?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 (http://www.smarty.net), which provides a mechanism and assorted tools for further separation of code and content.

Nile
01-11-2009, 09:09 PM
Bluewalrus:
Twey means something like:


<?php
$var = "Hello";
if($var == "Hello"){
?>
You said Hello!
<?php
}
?>

bluewalrus
01-11-2009, 09:50 PM
Would that work for this? say $name was from a form on the previous page and was declared here.


<?php
$var = "Hello";
if($var == "Hello"){
?>
You said Hello $name !
<?php
}
?>
----------------------------------------------------------------------

<?php
$var = "Hello";
if($var == "Hello"){
echo "You said Hello {$name} !";
}
?>

Nile
01-11-2009, 10:09 PM
No - that would not work...
@Twey, it's .net not .org:
http://www.smarty.net/

Twey
01-11-2009, 10:15 PM
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'):
<?php
$var = 'Hello';
$name = 'John';
?>And the output:
<?php if ($var === 'Hello'): ?>
You said hello, <?php echo $name; ?>!
<?php endif; ?>

Ah, you're right, Nile. Thanks! :)

When formatting, I like to indent PHP blocks one block above the current element, since they tend to semantically associate with the outer element:
<div>
<p>
<?php if ($var === 'Hello'): ?>
You said hello!
<?php else: ?>
You didn't say hello.
<?php endif; ?>
</p>
</div>

Nile
01-11-2009, 10:17 PM
Um.. it doesn't work.

Twey
01-11-2009, 10:21 PM
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. :)

Nile
01-11-2009, 10:23 PM
Ok.. sorry. :D
Also, your link has a .org, instead of .net(smarty).

Twey
01-11-2009, 11:06 PM
Thanks, I fixed it :)