p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: NICK: using the require function


Message #1 by "jorge" <jorge@d...> on Tue, 8 Oct 2002 18:26:49 -0500
Nick :
If he is concatenating he need to escape the ""
other wise "wont work"
whats the point in writing more code and more confusing
is a lot easier
just staight or $DOCUMENT_ROOT/$mypath
an he can change that var as he likes keeping his code cleaner

and Nick thanks for the insult

Regards

Jorge Cordero



----- Original Message -----
From: "Nikolai Devereaux" <yomama@u...>
To: "beginning php" <beginning_php@p...>
Sent: Tuesday, October 08, 2002 4:57 PM
Subject: [beginning_php] Re: using the require function


>
> > hi john here is an example of how to use requiere and $ DOCUMENT_ROOT
> >
> > require("$DOCUMENT_ROOT/canitec../libjc../conectar.php");
> >
> > in your script i can see a dot "." just after DOCUMENT_ROOT pluss some "
"
>
>
> Jorge,
>
> The . in John's code is the concatenation operator for strings.  Are you
not
> familiar with it?  It's one of the most basic operations that you have
> available in PHP.
>
> "hello" . ", " . "world"
>
> results in the string "hello, world".
>
> There is no difference between the result of a concatenation and variable
> insertion.
>
> in other words,
>
> ("$foo bar") == ($foo . " bar") == ("{$foo} bar")
>
>
> nik
>
>


Message #2 by Jacob Cohen <cohen@r...> on Tue, 8 Oct 2002 16:14:49 -0700
On 10/8/2002,  <jorge@d...> wrote:
> Nick :
> If he is concatenating he need to escape the ""
> other wise "wont work"
> whats the point in writing more code and more confusing
> is a lot easier
> just staight or $DOCUMENT_ROOT/$mypath
> an he can change that var as he likes keeping his code cleaner

> and Nick thanks for the insult

> Regards

> Jorge Cordero

A) No, he doesn't need to escape any quotes. His problem is that
$DOCUMENT_ROOT is not set.

B) This will cause him to have the same problem if he uses the code
you suggested.

C) Nick wasn't insulting you, he was explaining what the . operator
does, since from your post it appeared you didn't know. You were
doubting the validity of code that used it in a perfectly legitimate
fashion.


Original poster: your $DOCUMENT_ROOT variable is probably not set..
you might want to try $_SERVER['DOCUMENT_ROOT'] instead, if you are
using PHP 4.1 or greater, since register_globals is now off.


-- 
Regards,
Jake                            mailto:cohen@r...

Message #3 by "Nikolai Devereaux" <yomama@u...> on Tue, 8 Oct 2002 16:35:48 -0700
> If he is concatenating he need to escape the ""
> other wise "wont work"
> whats the point in writing more code and more confusing
> is a lot easier
> just staight or $DOCUMENT_ROOT/$mypath
> an he can change that var as he likes keeping his code cleaner
>
> and Nick thanks for the insult


I wasn't insulting you, Jorge -- no need to take things so personally.  The
fact remains that you're mistaken.  It's really no big deal -- I make mistakes
all the time.


John's original code snippet was, and I quote:

 require($DOCUMENT_ROOT . "/members/inc/copyright.inc");

If you look more closely at this, you'll notice that there are two strings
being concatenated.

The first string is contained in the variable $DOCUMENT_ROOT.

The second string is simply "/members/inc/copyright.inc".


You don't need to escape any quotation marks because those are the delimiters
of the string, "/members/inc/copyright.inc".

If you were to escape the double quotes, you'd end up with this:


 require($DOCUMENT_ROOT . \"/members/inc/copyright.inc\");

which causes a parse error, since the first \ is unexpected.  The concatenation
operator expects a string.  This string can come from a function return value,
a variable, or a static string.


You should also notice that in my previous reply (the one you found insulting),
I stated quite simply that your solution was equivalent to John's original
post.


Check it out:

You>  ("$DOCUMENT_ROOT/canitec.../libjc.../conectar.php")

John> ($DOCUMENT_ROOT . "/members/inc/copyright.inc")


Me>   ("$foo bar") == ($foo . " bar")


$foo, in my example, is $DOCUMENT_ROOT.

' $bar' is '/members/inc/copyright.inc'

Note:  single-quotes used to stress the leading space in the string, and
single-quoted strings are identical to double-quoted strings, they just don't
perform any special variable and escape-character substitution.



I am sorry if I've offended you, but it wasn't intentional.



Also, could you spell my name correctly in the future?  I find it disrespectful
when people misspell my name, especially since I sign 99% of my posts with it.


Thanks!

Nik


  Return to Index