php-general Digest 22 Aug 2004 03:56:34 -0000 Issue 2950

Topics (messages 194389 through 194408):

mysqli_escape_string vs. mysqli_prepare
        194389 by: aRZed

Re: Get reference count on a variable.
        194390 by: Hannes Magnusson
        194393 by: Curt Zirzow
        194394 by: Robert Cummings
        194395 by: Curt Zirzow
        194396 by: Robert Cummings
        194403 by: Daniel Schierbeck
        194405 by: Robert Cummings
        194406 by: Daniel Schierbeck

Replacing variables
        194391 by: Watty

Re: PHP, over javascript code simple \n
        194392 by: Louie Miranda

for loops performance
        194397 by: Xongoo!com: Central unit
        194398 by: Robert Cummings
        194399 by: raditha dissanayake
        194400 by: Xongoo!com: Central unit
        194402 by: Shawn McKenzie
        194407 by: John Holmes
        194408 by: Aidan Lister

Re: replace value of array by key of array  in string variable with pre
        194401 by: Shawn McKenzie
        194404 by: Justin Patrin

Administrivia:

To subscribe to the digest, e-mail:
        [EMAIL PROTECTED]

To unsubscribe from the digest, e-mail:
        [EMAIL PROTECTED]

To post to the list, e-mail:
        [EMAIL PROTECTED]


----------------------------------------------------------------------
--- Begin Message ---
Hy!

I have a relative simple question about mysqli_prepare:
When I prepare a statement and then bind parameters to it, does php automatically care for escaping strings etc.?


Example:
--------

<?php
$stmt = $mysqli->prepare("SELECT FROM table WHERE x=?");
$p = "5'"
$stmt->bind_param("i",$p);
# is "5'" converted to 5 like it would when i use intval($p)?

$stmt->reset();

$s = "'xyz' || x LIKE '%'";
$stmt->bind_param("s",$s);
# will those "'" be escaped?
?>

thanks for help
--- End Message ---
--- Begin Message ---
On Sat, 21 Aug 2004 04:10:53 +0000
[EMAIL PROTECTED] (Curt Zirzow) wrote:

> * Thus wrote Robert Cummings:
> > Hi All,
> > 
> >     I think I'm looking for something that doesn't exist, but just in
> > case thought I'd check the list. Does anyone know if a PHP function
> > exists to get the number of references on a given variable's data? I was
> > hoping to create a way for a factory to automatically recycle resources
> > without the need for the developer to call some kind of free() method.
> > If I could get the internal reference count then I'd be able to
> > determine if it is free by virtue of only 1 reference (the factory).
> > This is for PHP4 btw, the solution is trivial in PHP5 using destructors.
> 
> unfortantly there isn't a method to determain this.
> 
> Be careful with PHP5, i'm not sure if its applicable in your
> situation, but there does seem to be rumor that php5 objects are
> assigned by reference, which isn't true:
http://zend.com/expert_qa/qas.php?id=41&single=1
please read :)

 - Hannes

--- End Message ---
--- Begin Message ---
* Thus wrote Robert Cummings:
> On Sat, 2004-08-21 at 00:10, Curt Zirzow wrote:
> > * Thus wrote Robert Cummings:
> > > Hi All,
> > > 
> > >     I think I'm looking for something that doesn't exist, but just in
> > > case thought I'd check the list. Does anyone know if a PHP function
> > > exists to get the number of references on a given variable's data? I was
> > > hoping to create a way for a factory to automatically recycle resources
> > > without the need for the developer to call some kind of free() method.
> > > If I could get the internal reference count then I'd be able to
> > > determine if it is free by virtue of only 1 reference (the factory).
> > > This is for PHP4 btw, the solution is trivial in PHP5 using destructors.
> > 
> > unfortantly there isn't a method to determain this.
> > 
> > Be careful with PHP5, i'm not sure if its applicable in your
> > situation, but there does seem to be rumor that php5 objects are
> > assigned by reference, which isn't true:
> > 
> > $o1 = new object();
> > $o2 = $o1;
> > unset($o2);
> > 
> > the Object still exists, and the destructor isn't called.
> > 
> > vs.
> > 
> > $o3 = new object();
> > $o4 =& $o3;
> > unset($o3);

Sorry, those unset's should be:

$o2 = null;
and
$o3 = null;


It's was to demonstrate that = and =& are *not* the same thing as
one would believe since the term 'assigned by reference' is used.



Curt
-- 
First, let me assure you that this is not one of those shady pyramid schemes
you've been hearing about.  No, sir.  Our model is the trapezoid!

--- End Message ---
--- Begin Message ---
On Sat, 2004-08-21 at 10:24, Curt Zirzow wrote:
> * Thus wrote Robert Cummings:
> > On Sat, 2004-08-21 at 00:10, Curt Zirzow wrote:
> > > * Thus wrote Robert Cummings:
> > > > Hi All,
> > > > 
> > > >     I think I'm looking for something that doesn't exist, but just in
> > > > case thought I'd check the list. Does anyone know if a PHP function
> > > > exists to get the number of references on a given variable's data? I was
> > > > hoping to create a way for a factory to automatically recycle resources
> > > > without the need for the developer to call some kind of free() method.
> > > > If I could get the internal reference count then I'd be able to
> > > > determine if it is free by virtue of only 1 reference (the factory).
> > > > This is for PHP4 btw, the solution is trivial in PHP5 using destructors.
> > > 
> > > unfortantly there isn't a method to determain this.
> > > 
> > > Be careful with PHP5, i'm not sure if its applicable in your
> > > situation, but there does seem to be rumor that php5 objects are
> > > assigned by reference, which isn't true:
> > > 
> > > $o1 = new object();
> > > $o2 = $o1;
> > > unset($o2);
> > > 
> > > the Object still exists, and the destructor isn't called.
> > > 
> > > vs.
> > > 
> > > $o3 = new object();
> > > $o4 =& $o3;
> > > unset($o3);
> 
> Sorry, those unset's should be:
> 
> $o2 = null;
> and
> $o3 = null;
> 
> It's was to demonstrate that = and =& are *not* the same thing as
> one would believe since the term 'assigned by reference' is used.

As exemplified in the sample script I sent in my last response and by
the link sent by Hannes Magnusson, in PHP5 the following have identical
behaviour:

    $o1 = new Foo();
    $o2 = $o1;

    // Is same as...

    $o1 = new Foo();
    $o2 =& $o1;

This was not true in PHP4 which is what a developer from PHP4 coming to
PHP5 needs to be aware of when using objects.

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

--- End Message ---
--- Begin Message ---
* Thus wrote Robert Cummings:
> 
> As exemplified in the sample script I sent in my last response and by
> the link sent by Hannes Magnusson, in PHP5 the following have identical
> behaviour:
> 
>     $o1 = new Foo();
>     $o2 = $o1;
> 
>     // Is same as...
> 
>     $o1 = new Foo();
>     $o2 =& $o1;

But = and =& are not identical:

<?php
class foo { };

$a = new foo();
$b = $a;

var_dump($b); /* object #1 */
$a = null;
var_dump($b); /* object #1 */

$c = new foo();
$d = &$c;

var_dump($d); /* object #2 */
$c = null;
var_dump($d); /* NULL */

?>


Curt
-- 
First, let me assure you that this is not one of those shady pyramid schemes
you've been hearing about.  No, sir.  Our model is the trapezoid!

--- End Message ---
--- Begin Message ---
On Sat, 2004-08-21 at 11:21, Curt Zirzow wrote:
> * Thus wrote Robert Cummings:
> > 
> > As exemplified in the sample script I sent in my last response and by
> > the link sent by Hannes Magnusson, in PHP5 the following have identical
> > behaviour:
> > 
> >     $o1 = new Foo();
> >     $o2 = $o1;
> > 
> >     // Is same as...
> > 
> >     $o1 = new Foo();
> >     $o2 =& $o1;
> 
> But = and =& are not identical:
> 
> <?php
> class foo { };
> 
> $a = new foo();
> $b = $a;
> 
> var_dump($b); /* object #1 */
> $a = null;
> var_dump($b); /* object #1 */
> 
> $c = new foo();
> $d = &$c;
> 
> var_dump($d); /* object #2 */
> $c = null;
> var_dump($d); /* NULL */

Hmmm this is a different situation than you first presented. Here you
are modifying the variable and not unsetting it. Unsetting it does the
expected symbolic unlinking. However it seems (probably for
compatibility reasons) that PHP will not overwrite an object which was
symbolically linked without a reference. Now I can see what to watch out
for and indeed this isn't obvious :) To illustrate the confusion I've
modified your example to show that indeed we have a reference when
modifying the object (but as said re-assignment as "special" behaviour):

<?php

class foo { var $member = 1; };

$a = new foo();
$b = $a;

$a->member = 10;
var_dump($b); /* object #1 */
$a = null;
var_dump($b); /* object #1 */

$c = new foo();
$d = &$c;

$c->member = 20;
var_dump($d); /* object #2 */
$c = null;
var_dump($d); /* NULL */

We know that $b is a reference to $a because when we modified the member
var using $a->member then $b's member was also changed -- thus $b is the
same object as $a.

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

--- End Message ---
--- Begin Message --- Robert Cummings wrote:
In PHP5 to get a copy of, versus a reference to, an object the developer
must call the __clone() method for the target object:

    $obj = new Foo();
    $copyNotReference = $obj->__clone();

The correct syntax for that is:

        $obj = new Foo;
        $copyNotReference = clone $obj;


Cheers, Daniel

--- End Message ---
--- Begin Message ---
On Sat, 2004-08-21 at 14:38, Daniel Schierbeck wrote:
> Robert Cummings wrote:
> > In PHP5 to get a copy of, versus a reference to, an object the developer
> > must call the __clone() method for the target object:
> > 
> >     $obj = new Foo();
> >     $copyNotReference = $obj->__clone();
> 
> The correct syntax for that is:
> 
>       $obj = new Foo;
>       $copyNotReference = clone $obj;

Ahh cool. I guess I was reading an old document. Perhaps from before a
keyword was added for better code clarity :)

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

--- End Message ---
--- Begin Message --- Robert Cummings wrote:
On Sat, 2004-08-21 at 14:38, Daniel Schierbeck wrote:

Robert Cummings wrote:

In PHP5 to get a copy of, versus a reference to, an object the developer
must call the __clone() method for the target object:

   $obj = new Foo();
   $copyNotReference = $obj->__clone();

The correct syntax for that is:

        $obj = new Foo;
        $copyNotReference = clone $obj;


Ahh cool. I guess I was reading an old document. Perhaps from before a
keyword was added for better code clarity :)

Cheers,
Rob.
Hehe, no problem mate. By the way, does anyone know what happened to the "delete" keyword? I saw it in a Zend article before the final release of PHP 5, but i can't find it anywhere.

Ooops, off-topic, soory ;)

Best regards,
Daniel

--- End Message ---
--- Begin Message ---
I have a HTML form class which declares a format array on construction,
which contains the format of the form per line. Then it creates an array
which defines the input boxes styles of a form. I need a method of
arbitrarily adding each of the input boxes into the format array on
output. Could anyone help out with that?
 
Thanks in advance,
 
Watty

--- End Message ---
--- Begin Message ---
single ' will work fine. :) not double "

On Fri, 20 Aug 2004 11:49:10 +0200, aRZed <[EMAIL PROTECTED]> wrote:
> 
> > <?php
> > print("
> > if (document.rpcjs_louie.ColCustName.value == \"\") { walangfield +=
> > \"Missing: Firstname\n\"; }
> > ");
> > ?>
> 
> try simply escaping the "\" before the "n" with an additional "\" or in
> other words: use "\\n" instead of "\n". PHP will interpret "\\" as an "\".
> 
> 
> 
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
> 


-- 
Louie Miranda
http://www.axishift.com

--- End Message ---
--- Begin Message ---
Holla,

Maybe someone knows how to improve for loops
performance? When generating 1000's of pages, it
oads in 10-20 seconds. Maybe there is some
solution I don't know about?

Thanks!

--
Tadas Talaikis
[EMAIL PROTECTED]
http://www.xongoo.com

--- End Message ---
--- Begin Message ---
On Sat, 2004-08-21 at 12:17, Xongoo!com: Central unit wrote:
> Holla,
> 
> Maybe someone knows how to improve for loops
> performance? When generating 1000's of pages, it
> oads in 10-20 seconds. Maybe there is some
> solution I don't know about?

Give us an example of your for loop that is having efficiency problems.

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

--- End Message ---
--- Begin Message ---
Xongoo!com: Central unit wrote:

Holla,

Maybe someone knows how to improve for loops
performance? When generating 1000's of pages, it
oads in 10-20 seconds. Maybe there is some
solution I don't know about?



prey what is your algorithm?

Thanks!

--
Tadas Talaikis
[EMAIL PROTECTED]
http://www.xongoo.com





--
Raditha Dissanayake.
------------------------------------------------------------------------
http://www.radinks.com/sftp/         | http://www.raditha.com/megaupload
Lean and mean Secure FTP applet with | Mega Upload - PHP file uploader
Graphical User Inteface. Just 128 KB | with progress bar.

--- End Message ---
--- Begin Message ---
Here's:

for ($c=1; $c<$numpages; $c++){
echo "<a href=page$c.html>$c</a> | ";
}

When it generates 1-10, it is normal 0.05 s, but
often I need more than 1000, so when generating
100's - it is 0.5-1.5 sec., when more than 1000 -
> 10+ s.

I am thinking on running sopme sort of:
1) generate code of number of pages once a day
using cron,
2) store it in database,
3) show static generated code from database.

But before doing it I've thought to ask here.

--
Tadas Talaikis
[EMAIL PROTECTED]
http://www.xongoo.com

----- Original Message -----
From: "Robert Cummings" <[EMAIL PROTECTED]>
To: "Xongoo!com: Central unit" <[EMAIL PROTECTED]>
Cc: "PHP-General" <[EMAIL PROTECTED]>
Sent: Saturday, August 21, 2004 7:32 PM
Subject: Re: [PHP] for loops performance


> On Sat, 2004-08-21 at 12:17, Xongoo!com: Central
unit wrote:
> > Holla,
> >
> > Maybe someone knows how to improve for loops
> > performance? When generating 1000's of pages,
it
> > oads in 10-20 seconds. Maybe there is some
> > solution I don't know about?
>
> Give us an example of your for loop that is
having efficiency problems.
>
> Cheers,
> Rob.

--- End Message ---
--- Begin Message ---
Xongoo!Com: Central Unit wrote:

Here's:

for ($c=1; $c<$numpages; $c++){
echo "<a href=page$c.html>$c</a> | ";
}

When it generates 1-10, it is normal 0.05 s, but
often I need more than 1000, so when generating
100's - it is 0.5-1.5 sec., when more than 1000 -

10+ s.


I am thinking on running sopme sort of:
1) generate code of number of pages once a day
using cron,
2) store it in database,
3) show static generated code from database.

But before doing it I've thought to ask here.

--
Tadas Talaikis
[EMAIL PROTECTED]
http://www.xongoo.com

----- Original Message -----
From: "Robert Cummings" <[EMAIL PROTECTED]>
To: "Xongoo!com: Central unit" <[EMAIL PROTECTED]>
Cc: "PHP-General" <[EMAIL PROTECTED]>
Sent: Saturday, August 21, 2004 7:32 PM
Subject: Re: [PHP] for loops performance



On Sat, 2004-08-21 at 12:17, Xongoo!com: Central

unit wrote:

Holla,

Maybe someone knows how to improve for loops
performance? When generating 1000's of pages,

it

oads in 10-20 seconds. Maybe there is some
solution I don't know about?

Give us an example of your for loop that is

having efficiency problems.

Cheers,
Rob.

You can do this once and instead of echoing the links, save then in a session var and use that on subsequent pages.


Maybe on first page or before in some other script:

session_start();
for ($c=1; $c<$numpages; $c++){
    $_SESSION['links'] .= "<a href=page$c.html>$c</a> | ";
}

Then on other pages:

session_start();
echo $_SESSION['links'];

-Shawn
--- End Message ---
--- Begin Message --- Xongoo!com: Central unit wrote:
Maybe someone knows how to improve for loops
performance? When generating 1000's of pages, it
oads in 10-20 seconds. Maybe there is some
solution I don't know about?

If you only generate 100s of pages, it'll probably load about ten times faster...


--

---John Holmes...

Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/

php|architect: The Magazine for PHP Professionals – www.phparch.com
--- End Message ---
--- Begin Message ---
Hi Tadas,

PHP5 has very optimised code for loops, try upgrading and tell us how it
goes!

Also, if it's applicable, a plain "foreach" loop is actually faster.

Kind Regards,
Aidan


"Xongoo!Com: Central Unit" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> Holla,
>
> Maybe someone knows how to improve for loops
> performance? When generating 1000's of pages, it
> oads in 10-20 seconds. Maybe there is some
> solution I don't know about?
>
> Thanks!
>
> --
> Tadas Talaikis
> [EMAIL PROTECTED]
> http://www.xongoo.com

--- End Message ---
--- Begin Message --- Turbo wrote:
Hi,

I have array variable and string variable.
I want to replace value of array by key of array in string variable with preg_replace().


Example :
$message=array(
'name'=>'My Computer',
'version'=>'1.0'
);
$strValue="I am $name,build version $version\n";

How's to replace?

Yingyos

Why preg_replace?

Just do extract($message); then you have vars from the keys:

$name ='My Computer';
and
$version = '1.0';

Then those vars are used in the $strValue var.

-Shawn
--- End Message ---
--- Begin Message ---
On Sat, 21 Aug 2004 12:54:15 -0500, Shawn McKenzie <[EMAIL PROTECTED]> wrote:
> Turbo wrote:
> > Hi,
> >
> > I have array variable and string variable.
> > I want to replace value of array by key of array  in string variable
> > with preg_replace().
> >
> > Example :
> > $message=array(
> > 'name'=>'My Computer',
> > 'version'=>'1.0'
> > );
> > $strValue="I am $name,build version $version\n";
> >
> > How's to replace?

You could just change your string to be:
$strValue = 'I am '.$message['name'].',build version '.$message['version']."\n";

or

$output = preg_replace('/\$([A-Z_\-0-9]*)/ie', '$message["$1"]', $strValue);

This is not quite a complete solution and may not work (is not tested).

> >
> > Yingyos
> 
> Why preg_replace?
> 
> Just do extract($message); then you have vars from the keys:
> 
> $name ='My Computer';
> and
> $version = '1.0';
> 
> Then those vars are used in the $strValue var.
> 


-- 
DB_DataObject_FormBuilder - The database at your fingertips
http://pear.php.net/package/DB_DataObject_FormBuilder

paperCrane --Justin Patrin--

--- End Message ---

Reply via email to