Re: PHP calling PHP? - Mailing list pgsql-php

From Steve Werby
Subject Re: PHP calling PHP?
Date
Msg-id 015f01c0d969$8ec8ed60$6501a8c0@workstation7
Whole thread Raw
In response to Re: PHP calling PHP?  (Grant <grant@conprojan.com.au>)
List pgsql-php
"Chris Smith" <csmith@squiz.net> wrote:
> If you did echo '$PHP_SELF' (single quotes) it won't print the variable
> value, it will print the variable name.. (easy to get caught on!)..
>
> eg
> <?
>         echo "PHPSELF = $PHP_SELF<br>";
>         echo 'PHPSELF = $PHP_SELF<br>';
> ?>
>
> prints out..
>
> PHPSELF = test.php
> PHPSELF = $PHP_SELF

That's called interpolation.  Variables within double quotes are
interpolated (converted to their corresponding values), while variables
within single quotes are not.  This really isn't emphasized enough in the
PHP manual and presents a lot of problems for PHP newbies who aren't
familiar with this behavior and are struggling to find the best way to
handle quoting of attribute values in HTML tags.  Using single quotes to
enclose values in PHP has a marginal performance benefit over using double
quotes since single quotes don't have to be interpolated (parsed) by PHP.
When I'm dealing with HTML tags with attribute values, I tend to use the
following syntax:

echo 'My name is <a href="' . $url . '">' . $name . '</a>';

Otherwise, I tend to place everything within double quotes.

echo "My name is $name.";

YMMV.  FYI, a technique that is sometimes useful is to enclose variables
within single quotes and then interpolate them later using the eval()
function.  I sometimes do this within applications where I have dozens of
dynamic queries whose SQL statements change depending on user action (like
clicking a field heading to sort on).  I will store the SQL statements in a
separate file which in include()ed within my app, then use eval() to
interpolate them *after* the needed variables are set within the webpage.
For example, my include file will have code like:

$sql_103  = 'SELECT quantity, price';
$sql_103 .= 'FROM orders ';
$sql_103 .= 'WHERE user = "$user"';

And my main script will include the queries file and have code like:

// user variable comes from user input via form post.
// assume it was "Steve".
$user = $HTTP_POST_VARS['user'];

eval("\$sql_103 = \"$sql_103\";");

Then I can go about executing the query b/c $sql_103 now has a value of:

SELECT quantity, price FROM orders WHERE user = "Steve"

And the reason I store the SQL statements in a single file is b/c they're
easier to find, easier to change since many times the same SQL statements
are used in several scripts and I can avoid changing in multiple places,
with multiple users working on a project everyone knows where to find all
queries and whether one has already been created that they can use, it makes
documentation of the code and debugging a lot easier.

Hope this helps someone.

--
Steve Werby
President, Befriend Internet Services LLC
http://www.befriend.com/

--
Steve Werby
President, Befriend Internet Services LLC
http://www.befriend.com/


pgsql-php by date:

Previous
From: "Christian Marschalek"
Date:
Subject: RE: PHP calling PHP?
Next
From: "Christian Marschalek"
Date:
Subject: Only one session per user?