[thelist] Multiple PHP Varialbes and Single Quote Syntax Question
Rick den Haan
rick.denhaan at gmail.com
Sun Dec 3 09:15:09 CST 2006
Jono,
> --------------------
> QUESTIONS
> --------------------
>
> 1. I would like to add a few more variables to this code but am
> wondering if there is a more efficient way to write this?
>
> Would this work?:
>
> <? if ($filename=='cool-site.php')
> {$parent = 'cool-site.php'; && $thisPage = 'Cool Site'; &&
> $PageHeader = 'Cool Site Header Text Here'; }
> ?>
>
If you drop the &&, yes. If the IF-statement passes, everything between
the curly brackets {} will be executed. So, yes, you could do that.
Personally, I'd recommend using a switch[1] in this case, though. You
can read more about it on the PHP.net website, but here's the basics in
your situation:
<?php
switch($filename)
{
case 'cool-site.php':
$parent = 'cool-site.php';
$thisPage = 'Cool Site';
$PageHeader = 'Cool Site Header Text Here';
break;
}
?>
Repeat everything from case to break for each page. Just keep it within
the curly brackets.
> 2. I need a little help with escaping a single quote used for
> possession, in my Pseudo PHP database. How would I correctly write
> "Site's" in PHP?
>
> Is this correct?:
>
> if ($filename =='cool-site.php')
> { $PageHeader = "Cool Site\'s Header Text Here'; }
>
Close. If you open a string with a double quote, you have to close it
with a double quote as well. The same with single quotes. As for
escaping, that would depend on your choice for opening and closing the
string. If you open and close with a double quote, you'll need to escape
double quotes inside the string. If you open and close with a single
quote, you'll need to escape single quotes. Example:
CORRECT:
$PageHeader = "Cool Site's Header Text Here";
$PageHeader = 'Cool Site\'s Header Text Here';
INCORRECT:
$PageHeader = "Cool Site\'s Header Text Here";
$PageHeader = 'Cool Site's Header Text Here';
The first incorrect option will simply output the \ as well. The second
one will throw an error, because, as far as PHP is concerned, the string
is "Cool Site", and where there's "s Header Text Here" it will expect a
semi-colon.
> Any tips or help are appreciated. I am still learning my around basic PHP.
>
> Thanks,
No problem. I would recommend staying clear of the short open tags,
though. Try to use "<?php" instead of just "<?". For one thing, PHP 6
will no longer support the short form (can't find the source at the
moment). For another, not all webservers running PHP 4 or 5 have the
option enabled. It can also be confusing with XML-declarations, as they
have the form of "<?xml".
Good luck, and feel free to ask away, on or off list :)
Rick.
More information about the thelist
mailing list