php-general Digest 17 Mar 2010 00:55:41 -0000 Issue 6643

Topics (messages 302890 through 302919):

Re: Need routine to tell me number of dimensions in array.
        302890 by: Richard Quadling
        302893 by: Robert Cummings
        302894 by: Peter Lind
        302898 by: Robert Cummings
        302899 by: Robert Cummings
        302900 by: Peter Lind
        302901 by: Robert Cummings
        302912 by: Rene Veerman

Re: Spreadsheet_Excel_Reader problem
        302891 by: Jochen Schultz

$_FILE array being truncated
        302892 by: Richard H Lee
        302902 by: Daniel Egeberg
        302903 by: Ashley Sheridan
        302904 by: Richard H Lee
        302907 by: Kim Madsen
        302911 by: Rene Veerman

Re: Deleting multiple backslashes; regex?
        302895 by: Al

Re: PHP MySQL Insert Statements
        302896 by: Jan G.B.
        302897 by: Ryan Sun

best way to set up an include path for a multi-level project?
        302905 by: Robert P. J. Day
        302908 by: John Black
        302909 by: John Black
        302910 by: Ryan Sun
        302913 by: Rene Veerman

Re: PHP in HTML code
        302906 by: tedd
        302914 by: Rene Veerman
        302915 by: Ashley Sheridan
        302919 by: Adam Richardson

Database vs. Array
        302916 by: Richard S. Crawford
        302917 by: Bastien Koert
        302918 by: Ryan Sun

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
On 15 March 2010 23:45, Daevid Vincent <dae...@daevid.com> wrote:
> Anyone have a function that will return an integer of the number of
> dimensions an array has?

/**
 * Get the maximum depth of an array
 *
 * @param array &$Data A reference to the data array
 * @return int The maximum number of levels in the array.
 */
function arrayGetDepth(array &$Data) {
        static $CurrentDepth = 1;
        static $MaxDepth = 1;

        array_walk($Data, function($Value, $Key) use(&$CurrentDepth, 
&$MaxDepth) {
                if (is_array($Value)) {
                        $MaxDepth = max($MaxDepth, ++$CurrentDepth);
                        arrayGetDepth($Value);
                        --$CurrentDepth;
                }
        });

        return $MaxDepth;
}

Extending Jim and Roberts comments to this. No globals. By using a
reference to the array, large arrays are not copied (memory footprint
is smaller). And by using array_walk, a separate internal pointer is
used, so no need to worry about losing your position on the array.

Something to watch out for though is recursion in the array. If a
value in the array is a reference to another part of the array, you
are going to loop around for ever.

$Data = array(&$Data);

for example, with the line ...

                echo "$CurrentDepth, $MaxDepth, $Key\n";

in the callback function() will report 17701 before crashing out (no
stack error surprisingly enough).


Regards,

Richard Quadling.


-- 
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling

--- End Message ---
--- Begin Message ---


Richard Quadling wrote:
On 15 March 2010 23:45, Daevid Vincent <dae...@daevid.com> wrote:
Anyone have a function that will return an integer of the number of
dimensions an array has?

/**
 * Get the maximum depth of an array
 *
 * @param array &$Data A reference to the data array
 * @return int The maximum number of levels in the array.
 */
function arrayGetDepth(array &$Data) {
        static $CurrentDepth = 1;
        static $MaxDepth = 1;

        array_walk($Data, function($Value, $Key) use(&$CurrentDepth, 
&$MaxDepth) {
                if (is_array($Value)) {
                        $MaxDepth = max($MaxDepth, ++$CurrentDepth);
                        arrayGetDepth($Value);
                        --$CurrentDepth;
                }
        });

        return $MaxDepth;
}

Extending Jim and Roberts comments to this. No globals. By using a
reference to the array, large arrays are not copied (memory footprint
is smaller).

Using a reference actually increases overhead. References in PHP were mostly useful in PHP4 when assigning objects would cause the object to be copied. But even then, for arrays, a Copy on Write (COW) strategy was used (and is still used) such that you don't copy any values. Try it for yourself:

<?php

$copies = array();
$string = str_repeat( '*', 1000000 );

echo memory_get_usage()."\n";
for( $i = 0; $i < 1000; $i++ )
{
    $copies[] = $string;
}
echo memory_get_usage()."\n";

?>

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP

--- End Message ---
--- Begin Message ---
This is one example where references actually decrease memory usage.
The main reason is the recursive nature of the function. Try

<?php

echo memory_get_usage() . PHP_EOL;
$array = range(0,1000000);
$array[10] = range(0,10);
$array[20] = range(0,10);
$array[30] = range(0,10);
$array[40] = range(0,10);
$array[50] = range(0,10);
$array[60] = range(0,10);
$array[70] = range(0,10);
$array[80] = range(0,10);
$array[90] = range(0,10);
$array[100] = range(0,10);
echo memory_get_usage() . PHP_EOL;
carray($array);
function carray ($array)
{
    foreach ($array as $value)
    {
        if (is_array($value)) carray($value);
    }
    echo memory_get_usage() . PHP_EOL;
    echo count($array) . PHP_EOL;
}
echo memory_get_usage() . PHP_EOL;

And then compare with:

<?php

echo memory_get_usage() . PHP_EOL;
$array = range(0,1000000);
$array[10] = range(0,10);
$array[20] = range(0,10);
$array[30] = range(0,10);
$array[40] = range(0,10);
$array[50] = range(0,10);
$array[60] = range(0,10);
$array[70] = range(0,10);
$array[80] = range(0,10);
$array[90] = range(0,10);
$array[100] = range(0,10);
echo memory_get_usage() . PHP_EOL;
carray($array);
function carray (&$array)
{
    $i = 0;
    foreach ($array as $value)
    {
        if (is_array($value)) carray($value);
    }
    echo memory_get_usage() . PHP_EOL;
    echo count($array) . PHP_EOL;
}
echo memory_get_usage() . PHP_EOL;

The memory usage spikes in the first example when you hit the second
array level - you don't see the same spike in the second example.

Regards
Peter

On 16 March 2010 15:46, Robert Cummings <rob...@interjinn.com> wrote:
>
>
> Richard Quadling wrote:
>>
>> On 15 March 2010 23:45, Daevid Vincent <dae...@daevid.com> wrote:
>>>
>>> Anyone have a function that will return an integer of the number of
>>> dimensions an array has?
>>
>> /**
>>  * Get the maximum depth of an array
>>  *
>>  * @param array &$Data A reference to the data array
>>  * @return int The maximum number of levels in the array.
>>  */
>> function arrayGetDepth(array &$Data) {
>>        static $CurrentDepth = 1;
>>        static $MaxDepth = 1;
>>
>>        array_walk($Data, function($Value, $Key) use(&$CurrentDepth,
>> &$MaxDepth) {
>>                if (is_array($Value)) {
>>                        $MaxDepth = max($MaxDepth, ++$CurrentDepth);
>>                        arrayGetDepth($Value);
>>                        --$CurrentDepth;
>>                }
>>        });
>>
>>        return $MaxDepth;
>> }
>>
>> Extending Jim and Roberts comments to this. No globals. By using a
>> reference to the array, large arrays are not copied (memory footprint
>> is smaller).
>
> Using a reference actually increases overhead. References in PHP were mostly
> useful in PHP4 when assigning objects would cause the object to be copied.
> But even then, for arrays, a Copy on Write (COW) strategy was used (and is
> still used) such that you don't copy any values. Try it for yourself:
>
> <?php
>
> $copies = array();
> $string = str_repeat( '*', 1000000 );
>
> echo memory_get_usage()."\n";
> for( $i = 0; $i < 1000; $i++ )
> {
>    $copies[] = $string;
> }
> echo memory_get_usage()."\n";
>
> ?>
>
> Cheers,
> Rob.
> --
> http://www.interjinn.com
> Application and Templating Framework for PHP
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>



-- 
<hype>
WWW: http://plphp.dk / http://plind.dk
LinkedIn: http://www.linkedin.com/in/plind
Flickr: http://www.flickr.com/photos/fake51
BeWelcome: Fake51
Couchsurfing: Fake51
</hype>

--- End Message ---
--- Begin Message ---
Peter Lind wrote:
This is one example where references actually decrease memory usage.
The main reason is the recursive nature of the function. Try

<?php

echo memory_get_usage() . PHP_EOL;
$array = range(0,1000000);
$array[10] = range(0,10);
$array[20] = range(0,10);
$array[30] = range(0,10);
$array[40] = range(0,10);
$array[50] = range(0,10);
$array[60] = range(0,10);
$array[70] = range(0,10);
$array[80] = range(0,10);
$array[90] = range(0,10);
$array[100] = range(0,10);
echo memory_get_usage() . PHP_EOL;
carray($array);
function carray ($array)
{
    foreach ($array as $value)
    {
        if (is_array($value)) carray($value);
    }
    echo memory_get_usage() . PHP_EOL;
    echo count($array) . PHP_EOL;
}
echo memory_get_usage() . PHP_EOL;

And then compare with:

<?php

echo memory_get_usage() . PHP_EOL;
$array = range(0,1000000);
$array[10] = range(0,10);
$array[20] = range(0,10);
$array[30] = range(0,10);
$array[40] = range(0,10);
$array[50] = range(0,10);
$array[60] = range(0,10);
$array[70] = range(0,10);
$array[80] = range(0,10);
$array[90] = range(0,10);
$array[100] = range(0,10);
echo memory_get_usage() . PHP_EOL;
carray($array);
function carray (&$array)
{
    $i = 0;
    foreach ($array as $value)
    {
        if (is_array($value)) carray($value);
    }
    echo memory_get_usage() . PHP_EOL;
    echo count($array) . PHP_EOL;
}
echo memory_get_usage() . PHP_EOL;

The memory usage spikes in the first example when you hit the second
array level - you don't see the same spike in the second example.

Regards
Peter

Doh, forgot about that :)

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP

--- End Message ---
--- Begin Message ---
Peter Lind wrote:
This is one example where references actually decrease memory usage.
The main reason is the recursive nature of the function. Try

BTW, it's not the recursive nature of the function causing the problem. It's the movement of the internal pointer within the array. When it moves the COW realizes the copy's pointer has moved and splits off the copy. You can verify this by echoing the memory usage when you first enter carray(). The spike occurs inside the foreach loop.

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP

--- End Message ---
--- Begin Message ---
Hmm, will probably have to look inside PHP for this ... the foreach
loop will copy each element as it loops over it (without actually
copying, obviously), however there's no change happening to the
element at any point and so there's nothing to suggest to the
copy-on-write to create a new instance of the sub-array.

It should look like this:
$a = array(0, 1, 2, array(0, 1, 2, 3), 4, 5, 6, .... n);
$b = $a[3];
doStuffs($b);

Whether or not you loop over $a and thus move the internal pointer,
you don't change (well, shouldn't, anyway) $b as that's a subarray
which has it's own internal pointer, that isn't touched.

Or maybe I've gotten this completely backwards ...

Regards
Peter

On 16 March 2010 17:12, Robert Cummings <rob...@interjinn.com> wrote:
> Peter Lind wrote:
>>
>> This is one example where references actually decrease memory usage.
>> The main reason is the recursive nature of the function. Try
>
> BTW, it's not the recursive nature of the function causing the problem. It's
> the movement of the internal pointer within the array. When it moves the COW
> realizes the copy's pointer has moved and splits off the copy. You can
> verify this by echoing the memory usage when you first enter carray(). The
> spike occurs inside the foreach loop.
>
> Cheers,
> Rob.
> --
> http://www.interjinn.com
> Application and Templating Framework for PHP
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>



-- 
<hype>
WWW: http://plphp.dk / http://plind.dk
LinkedIn: http://www.linkedin.com/in/plind
Flickr: http://www.flickr.com/photos/fake51
BeWelcome: Fake51
Couchsurfing: Fake51
</hype>

--- End Message ---
--- Begin Message ---
Peter Lind wrote:
Hmm, will probably have to look inside PHP for this ... the foreach
loop will copy each element as it loops over it (without actually
copying, obviously), however there's no change happening to the
element at any point and so there's nothing to suggest to the
copy-on-write to create a new instance of the sub-array.

It should look like this:
$a = array(0, 1, 2, array(0, 1, 2, 3), 4, 5, 6, .... n);
$b = $a[3];
doStuffs($b);

Whether or not you loop over $a and thus move the internal pointer,
you don't change (well, shouldn't, anyway) $b as that's a subarray
which has it's own internal pointer, that isn't touched.

Or maybe I've gotten this completely backwards ...

I'm not sure of the exact reason... PHP has the following comment:

    Note: Unless the array is referenced, foreach operates on a copy
          of the specified array and not the array itself. foreach
          has some side effects on the array pointer. Don't rely on
          the array pointer during or after the foreach without
          resetting it.

    http://php.net/manual/en/control-structures.foreach.php

I've always found the foreach loop to a be a bit on the bizarre size, this is probably just another one of those bizarrities :)

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP

--- End Message ---
--- Begin Message ---
maybe you should be foreach()ing with references?
php.net : search "foreach" :


As of PHP 5, you can easily modify array's elements by preceding
$value with &. This will assign reference instead of copying the
value.
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
    $value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
unset($value); // break the reference with the last element
?>
This is possible only if iterated array can be referenced (i.e. is variable),



On Tue, Mar 16, 2010 at 5:43 PM, Robert Cummings <rob...@interjinn.com> wrote:
> Peter Lind wrote:
>>
>> Hmm, will probably have to look inside PHP for this ... the foreach
>> loop will copy each element as it loops over it (without actually
>> copying, obviously), however there's no change happening to the
>> element at any point and so there's nothing to suggest to the
>> copy-on-write to create a new instance of the sub-array.
>>
>> It should look like this:
>> $a = array(0, 1, 2, array(0, 1, 2, 3), 4, 5, 6, .... n);
>> $b = $a[3];
>> doStuffs($b);
>>
>> Whether or not you loop over $a and thus move the internal pointer,
>> you don't change (well, shouldn't, anyway) $b as that's a subarray
>> which has it's own internal pointer, that isn't touched.
>>
>> Or maybe I've gotten this completely backwards ...
>
> I'm not sure of the exact reason... PHP has the following comment:
>
>    Note: Unless the array is referenced, foreach operates on a copy
>          of the specified array and not the array itself. foreach
>          has some side effects on the array pointer. Don't rely on
>          the array pointer during or after the foreach without
>          resetting it.
>
>    http://php.net/manual/en/control-structures.foreach.php
>
> I've always found the foreach loop to a be a bit on the bizarre size, this
> is probably just another one of those bizarrities :)
>
> Cheers,
> Rob.
> --
> http://www.interjinn.com
> Application and Templating Framework for PHP
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--- End Message ---
--- Begin Message ---
Or maybe you want to check out this:

http://www.codeplex.com/PHPExcel

I havn't checked it out for your special purpose but i found it useful for some other jobs (especially excell 2007 support).

regards
Jochen

Ashley Sheridan schrieb:
On Tue, 2010-03-16 at 20:16 +0800, I am on the top of the world!
Borlange University wrote:

i have a problem of reading values from excel file via
spreadsheet_excel_reader which is a php class used to manipulate excel
files.

$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('CP936');
$data->read("d:\\tmp.xls");
$rows=$data->sheets[0]['numRows'];
$cell = $data->sheets[0]['cells'][1][1];

if i type many rows,say 1000, in the tmp.xls, it can read, $rows shows 1000,
however, 1000 rows of data are copied from another excel file and these
cells may have different background or other changes that differ from normal
cells, it fails to read.the variable $rows shows nothing....what i can do
now is to split these data into small groups, small enough to be read, it
really takes plenty of time.


i dont know why, has somebody met this problem ever?


If it's just the data you need, try using a csv file instead. The Excel
format is closed, and as such, the PHP classes won't have full support
for all of it's features. It seems like formatting is causing the data
to be written to the spreadsheet differently maybe.

Thanks,
Ash
http://www.ashleysheridan.co.uk




--
 Sport Import GmbH   - Amtsgericht Oldenburg  - Tel:   +49-4405-9280-63
 Industriestrasse 39 - HRB 1202900            -
 26188 Edewecht      - GF: Michael Müllmann

--- End Message ---
--- Begin Message ---
p.general,

I have a form with 75 or so file input controls: <input type="file" ...

Usually when I submit the form, I only upload two or so files. So in the post request, it sends the two files along with the other blank 73 fields. This has been working fine on my live and test servers so far.

However as of the past few days only the first 20 file fields are recieved on the live server. I saw this by dumping the $_FILES array. This does not happen on the test server. I can see all 75 file fields been sent across in the POST header in wireshark, but but only the first 20 appear in the $_FILES array.

Has anyone come across this problem of the $_FILE array being truncated? I don't recall changing anything on the live server.

Richard

--- End Message ---
--- Begin Message ---
On Tue, Mar 16, 2010 at 15:19, Richard H Lee <rich...@webdezign.co.uk> wrote:
> p.general,
>
> I have a form with 75 or so file input controls: <input type="file" ...
>
> Usually when I submit the form, I only upload two or so files. So in the
> post request, it sends the two files along with the other blank 73 fields.
> This has been working fine on my live and test servers so far.
>
> However as of the past few days only the first 20 file fields are recieved
> on the live server. I saw this by dumping the $_FILES array. This does not
> happen on the test server. I can see all 75 file fields been sent across in
> the POST header in wireshark, but but only the first 20 appear in the
> $_FILES array.
>
> Has anyone come across this problem of the $_FILE array being truncated? I
> don't recall changing anything on the live server.
>
> Richard

Check out max_file_uploads which was added in PHP 5.2.12 and defaults to 20.

-- 
Daniel Egeberg

--- End Message ---
--- Begin Message ---
On Tue, 2010-03-16 at 18:25 +0100, Daniel Egeberg wrote:

> On Tue, Mar 16, 2010 at 15:19, Richard H Lee <rich...@webdezign.co.uk> wrote:
> > p.general,
> >
> > I have a form with 75 or so file input controls: <input type="file" ...
> >
> > Usually when I submit the form, I only upload two or so files. So in the
> > post request, it sends the two files along with the other blank 73 fields.
> > This has been working fine on my live and test servers so far.
> >
> > However as of the past few days only the first 20 file fields are recieved
> > on the live server. I saw this by dumping the $_FILES array. This does not
> > happen on the test server. I can see all 75 file fields been sent across in
> > the POST header in wireshark, but but only the first 20 appear in the
> > $_FILES array.
> >
> > Has anyone come across this problem of the $_FILE array being truncated? I
> > don't recall changing anything on the live server.
> >
> > Richard
> 
> Check out max_file_uploads which was added in PHP 5.2.12 and defaults to 20.
> 
> -- 
> Daniel Egeberg
> 


I really wouldn't rely on a form that contains more than 20 file upload
boxes though. If someone uploads some large files, they're stuck with an
extremely long wait which will slow down your server a bit as well if a
lot of people are using the same form at the same time.

Have you looked at using a multi-file upload element instead? 

Thanks,
Ash
http://www.ashleysheridan.co.uk



--- End Message ---
--- Begin Message ---
> Check out max_file_uploads which was added in PHP 5.2.12 and defaults
to
> 20.

Yes that was the problem, I set max_file_uploads higher and it works
now.

Thanks

--- End Message ---
--- Begin Message ---
Ashley Sheridan wrote on 16/03/2010 18:28:

I really wouldn't rely on a form that contains more than 20 file upload
boxes though. If someone uploads some large files, they're stuck with an
extremely long wait which will slow down your server a bit as well if a
lot of people are using the same form at the same time.

True. Instead make the upload with AJAX, so the file starts uploading when the field is changed (onChange()) or out of focus (is there such a function? onUnFocus()? :-)). See gmail attachment for an example.

--
Kind regards
Kim Emax - masterminds.dk

--- End Message ---
--- Begin Message ---
jumploader.com might be interesting to you..

On Tue, Mar 16, 2010 at 3:19 PM, Richard H Lee <rich...@webdezign.co.uk> wrote:
> p.general,
>
> I have a form with 75 or so file input controls: <input type="file" ...
>
> Usually when I submit the form, I only upload two or so files. So in the
> post request, it sends the two files along with the other blank 73 fields.
> This has been working fine on my live and test servers so far.
>
> However as of the past few days only the first 20 file fields are recieved
> on the live server. I saw this by dumping the $_FILES array. This does not
> happen on the test server. I can see all 75 file fields been sent across in
> the POST header in wireshark, but but only the first 20 appear in the
> $_FILES array.
>
> Has anyone come across this problem of the $_FILE array being truncated? I
> don't recall changing anything on the live server.
>
> Richard
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--- End Message ---
--- Begin Message ---


On 3/15/2010 5:03 PM, Jim Lucas wrote:
Al wrote:
Anyone have a regex pattern for deleting multiple backslashes e.g., \\\\\\\

I pretty good with regex; but, be damned if I can delete them with
preg_replace()

I've tried "\\\\" as the manual says

preg_replace("/\\\\/", '', $str);

preg_replace("/(\\\\)+/", '', $str);

preg_replace("/\x5C/", '', $str);

preg_replace("/\\x5c/", '', $str);

And lots of others.

stripslashes() and stripcslashes() are limited.




Might I ask, how are the multiple slashes getting generated in the first place?
  Where is the data coming from?

Next question would be: Do you want to completely remove all instances of
multiple backslashes?  Or, do you want to replace all instances of multiple
backslashes with a single backslash?

I would try something like this:

<plaintext><?php

$in = '\\\as\\\\asdf\\\asdf\asdf\\\\\asdf\\\\\asdf';

# to remove all backslashes, us this
echo preg_replace('|[\\\\]+|', '', $in).PHP_EOL;

# to remove all backslashes, us this
echo str_replace('\\', '', $in).PHP_EOL;

# to replace consecutive instances of backslashes with a single backslash
echo preg_replace('|[\\\\]+|', '\\', $in).PHP_EOL;

?>
done!



As I reported earlier, problem was my code was reloading a POST array following the backlash removal. Dumb error on my part.

Re: "Might I ask, how are the multiple slashes getting generated in the first place? Where is the data coming from?" It comes from a client-side editor where users can enter them at will. It is my standard practice to do a good job of protecting users from themselves. I originally just had the usual stripslashes() but found it didn't take care of users adding several.


--- End Message ---
--- Begin Message ---
2010/3/12 Martine Osias <webi...@gmail.com>

> Hi,
>
> My insert statements on this web page don't execute. The select statements
> do work. This tells me that the database connection is working. The username
> and password are the administrator's. What else could prevent the insert
> statements from executing?
>
> The assigned permissions to that account.
Using MySQL, you can define *who* is allowed to do *what*. (i.e. SELECT,
INSERT, GRANT)
(or syntax errors in your insert statements, like others said before.)
Regards

--- End Message ---
--- Begin Message ---
Always make sure your dynamic sql string in php code are as expected
var_dump($sql) before query

On Thu, Mar 11, 2010 at 10:13 PM, Martine Osias <webi...@gmail.com> wrote:
> Hi,
>
> My insert statements on this web page don't execute. The select statements
> do work. This tells me that the database connection is working. The username
> and password are the administrator's. What else could prevent the insert
> statements from executing?
>
> Thank you.
>
>
> Martine
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--- End Message ---
--- Begin Message ---
  i have a project (let's call it "proj") which lives in the "proj"
directory and which contains several subdirs, some of which might
contain their own subdirs and so on.  some of those subdirs might
contain utility classes that i want to include or require elsewhere,
so i want to be able to just:

  require 'util.php';

and have the PHP include path "do the right thing."  right now, it's
ugly, as in, having PHP files that do:

  require '../../proj/util.php';

meaning every single file that wants to include another proj-related
file has to know its relative position in the proj hierarchy.   barf.

  based on my experience with shell and makefile-based projects, my
preference would be that anyone who checks out a copy of the project
can check it out anywhere they want, then they need to set the single
shell environment variable, say PROJ_DIR, to the top-level directory
location, say:

  $ export PROJ_DIR=/home/rpjday/php/things/proj

  i would then make sure that env vars are available to PHP scripts
via /etc/php.ini:

  variables_order = "EGPCS"
                     ^

and all PHP scripts would start off with something like:

set_include_path(get_include_path() . PATH_SEPARATOR . getenv('PROJ_DIR'));

at that point (and correct me if i'm wrong), all subsequent includes
or requires would simply need to reflect the location of the file
relative to its PROJ_DIR location (barring any weird conflicts with
system files having the same name, of course).

  does that sound about right?  is there a standard way to do this?
that is, to have a sizable PHP hierarchy and allow any files within
that hierarchy to include others conveniently without having to
hardcode directory names.

  requiring users to simply set a single env var has always worked for
me.  is that what others do to solve this?  thanks.

rday
--

========================================================================
Robert P. J. Day                               Waterloo, Ontario, CANADA

            Linux Consulting, Training and Kernel Pedantry.

Web page:                                          http://crashcourse.ca
Twitter:                                       http://twitter.com/rpjday
========================================================================

--- End Message ---
--- Begin Message ---
On 03/16/2010 06:57 PM, Robert P. J. Day wrote:
   i have a project (let's call it "proj") which lives in the "proj"
directory and which contains several subdirs, some of which might
contain their own subdirs and so on.  some of those subdirs might
contain utility classes that i want to include or require elsewhere,
so i want to be able to just:
   require 'util.php';
and have the PHP include path "do the right thing."  right now, it's
ugly, as in, having PHP files that do:
   require '../../proj/util.php';
meaning every single file that wants to include another proj-related
file has to know its relative position in the proj hierarchy.   barf.

I used to use auto detecting absolute paths but switched to relative paths when a client decided to switch his hosting company, I *think* it was GoDaddy. They required relative paths when using the shared SSL server or the browser would throw errors.

It is really not bad as long a you keep your directory structure consistent. I have never received a bug report because of an include path issue. It also does not matter where the root directory of the app is located since everything is relative anyway. There is also no need for the user to configure anything because of the relative paths.

So I just set $include = './include/abc/def/' at the top of the executing php page and use it further down. You make $include a global or simply pass $include to functions or classes when they need to include files. I use the later approach.

As I said I never had a problem with it, it just requires consistency.

--
John
Eine der erstaunlichsten Erscheinungen ist, daß man sich einbildet,
von abhängigen Menschen unabhängige Meinungen erwarten zu dürfen.
[Sigmund Graff]

--- End Message ---
--- Begin Message ---
On 03/16/2010 08:50 PM, John Black wrote:
So I just set $include = './include/abc/def/' at the top of the

correction, I set $include to the relative path of the include directory and then use it like this:

$include = '../../include/';

require $include.'abc/file1.php';
require_once $include.'abc/def/file2.php';
require_once $include.'file3.php';

Still works the same way but since all my include files are in include or some sub directory of it this works perfect.


--
John
Vorurteil ist das Kind von Ignoranz.
[William Hazlitt]

--- End Message ---
--- Begin Message ---
On Tue, Mar 16, 2010 at 1:57 PM, Robert P. J. Day <rpj...@crashcourse.ca> wrote:
>
> and all PHP scripts would start off with something like:
>
> set_include_path(get_include_path() . PATH_SEPARATOR . getenv('PROJ_DIR'));
>

just utilize include_path directive in php.ini

--- End Message ---
--- Begin Message ---
On Tue, Mar 16, 2010 at 9:48 PM, Ryan Sun <ryansu...@gmail.com> wrote:
> just utilize include_path directive in php.ini

yea, or via ini_set('include_path', ....);

--- End Message ---
--- Begin Message ---
At 5:54 PM +0000 3/15/10, Jochem Maas wrote:
Op 3/13/10 3:49 PM, Jorge Gomes schreef:
 First of all, i recommend the use of normal php tags (<?php ...  ?>) because
 the short tags are atm marked as* **DEPRECATED*.

that's a documentation error.


 You should also echo your values to the page, instead using the shortcut <?=
 (stop being a lazy ass! :P):

it's not lazy, it's succinct and much easier to read (once you know what it means),

Yes, but like all web languages, they don't live in a vacuum -- they must play well with others to survive. Programming is dynamic not static.

While using "<?=" identifies what follows "to you", it doesn't "to others" and therein lies the problem. If XML (and possibility others) don't accept the short term tag, then why use it?

Using "Standards" like this help promote better communication between all languages -- what's wrong with that? Simply put, either communicate better or don't -- that's your choice -- but your decision is also a demonstration to your client/employer/peers as to your desire to produce the "best" possible code.

I look at code containing "<?=" the same way as I see html containing tables and embedded styling for presentation -- "This must be old code OR the programmer still doesn't get it".

Cheers,

tedd

--
-------
http://sperling.com  http://ancientstones.com  http://earthstones.com

--- End Message ---
--- Begin Message ---
maybe adding a <?php= as equivalent to <?= and <?php echo ,
then deprecating <?= would be useful.

On Tue, Mar 16, 2010 at 7:18 PM, tedd <tedd.sperl...@gmail.com> wrote:
> At 5:54 PM +0000 3/15/10, Jochem Maas wrote:
>>
>> Op 3/13/10 3:49 PM, Jorge Gomes schreef:
>>>
>>>  First of all, i recommend the use of normal php tags (<?php ...  ?>)
>>> because
>>>  the short tags are atm marked as* **DEPRECATED*.
>>
>> that's a documentation error.
>>
>>>
>>>  You should also echo your values to the page, instead using the shortcut
>>> <?=
>>>  (stop being a lazy ass! :P):
>>
>> it's not lazy, it's succinct and much easier to read (once you know what
>> it means),
>
> Yes, but like all web languages, they don't live in a vacuum -- they must
> play well with others to survive. Programming is dynamic not static.
>
> While using "<?=" identifies what follows "to you", it doesn't "to others"
> and therein lies the problem. If XML (and possibility others) don't accept
> the short term tag, then why use it?
>
> Using "Standards" like this help promote better communication between all
> languages -- what's wrong with that? Simply put, either communicate better
> or don't -- that's your choice -- but your decision is also a demonstration
> to your client/employer/peers as to your desire to produce the "best"
> possible code.
>
> I look at code containing "<?=" the same way as I see html containing tables
> and embedded styling for presentation -- "This must be old code OR the
> programmer still doesn't get it".
>
> Cheers,
>
> tedd
>
> --
> -------
> http://sperling.com  http://ancientstones.com  http://earthstones.com
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--- End Message ---
--- Begin Message ---
On Tue, 2010-03-16 at 23:42 +0100, Rene Veerman wrote:

> maybe adding a <?php= as equivalent to <?= and <?php echo ,
> then deprecating <?= would be useful.
> 
> On Tue, Mar 16, 2010 at 7:18 PM, tedd <tedd.sperl...@gmail.com> wrote:
> > At 5:54 PM +0000 3/15/10, Jochem Maas wrote:
> >>
> >> Op 3/13/10 3:49 PM, Jorge Gomes schreef:
> >>>
> >>>  First of all, i recommend the use of normal php tags (<?php ...  ?>)
> >>> because
> >>>  the short tags are atm marked as* **DEPRECATED*.
> >>
> >> that's a documentation error.
> >>
> >>>
> >>>  You should also echo your values to the page, instead using the shortcut
> >>> <?=
> >>>  (stop being a lazy ass! :P):
> >>
> >> it's not lazy, it's succinct and much easier to read (once you know what
> >> it means),
> >
> > Yes, but like all web languages, they don't live in a vacuum -- they must
> > play well with others to survive. Programming is dynamic not static.
> >
> > While using "<?=" identifies what follows "to you", it doesn't "to others"
> > and therein lies the problem. If XML (and possibility others) don't accept
> > the short term tag, then why use it?
> >
> > Using "Standards" like this help promote better communication between all
> > languages -- what's wrong with that? Simply put, either communicate better
> > or don't -- that's your choice -- but your decision is also a demonstration
> > to your client/employer/peers as to your desire to produce the "best"
> > possible code.
> >
> > I look at code containing "<?=" the same way as I see html containing tables
> > and embedded styling for presentation -- "This must be old code OR the
> > programmer still doesn't get it".
> >
> > Cheers,
> >
> > tedd
> >
> > --
> > -------
> > http://sperling.com  http://ancientstones.com  http://earthstones.com
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
> 


I think that would just add to an already confusing situation.

Thanks,
Ash
http://www.ashleysheridan.co.uk



--- End Message ---
--- Begin Message ---
On Tue, Mar 16, 2010 at 7:06 PM, Ashley Sheridan
<a...@ashleysheridan.co.uk>wrote:

> On Tue, 2010-03-16 at 23:42 +0100, Rene Veerman wrote:
>
> > maybe adding a <?php= as equivalent to <?= and <?php echo ,
> > then deprecating <?= would be useful.
> >
> > On Tue, Mar 16, 2010 at 7:18 PM, tedd <tedd.sperl...@gmail.com> wrote:
> > > At 5:54 PM +0000 3/15/10, Jochem Maas wrote:
> > >>
> > >> Op 3/13/10 3:49 PM, Jorge Gomes schreef:
> > >>>
> > >>>  First of all, i recommend the use of normal php tags (<?php ...  ?>)
> > >>> because
> > >>>  the short tags are atm marked as* **DEPRECATED*.
> > >>
> > >> that's a documentation error.
> > >>
> > >>>
> > >>>  You should also echo your values to the page, instead using the
> shortcut
> > >>> <?=
> > >>>  (stop being a lazy ass! :P):
> > >>
> > >> it's not lazy, it's succinct and much easier to read (once you know
> what
> > >> it means),
> > >
> > > Yes, but like all web languages, they don't live in a vacuum -- they
> must
> > > play well with others to survive. Programming is dynamic not static.
> > >
> > > While using "<?=" identifies what follows "to you", it doesn't "to
> others"
> > > and therein lies the problem. If XML (and possibility others) don't
> accept
> > > the short term tag, then why use it?
> > >
> > > Using "Standards" like this help promote better communication between
> all
> > > languages -- what's wrong with that? Simply put, either communicate
> better
> > > or don't -- that's your choice -- but your decision is also a
> demonstration
> > > to your client/employer/peers as to your desire to produce the "best"
> > > possible code.
> > >
> > > I look at code containing "<?=" the same way as I see html containing
> tables
> > > and embedded styling for presentation -- "This must be old code OR the
> > > programmer still doesn't get it".
> > >
> > > Cheers,
> > >
> > > tedd
> > >
> > > --
> > > -------
> > > http://sperling.com  http://ancientstones.com  http://earthstones.com
> > >
> > > --
> > > PHP General Mailing List (http://www.php.net/)
> > > To unsubscribe, visit: http://www.php.net/unsub.php
> > >
> > >
> >
>
>
> I think that would just add to an already confusing situation.
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>
I'll confess that I work on ASP.Net just as much as PHP (If you must throw
tomatoes, make sure their really ripe so they don't hurt as much ;)  I
appreciate the different tags offered in ASP.Net, as they succinctly provide
some convenient capabilities (one version provides similar capabilites to
PHP's <?=$var_to_echo?>.)

I believe that although they're removing the ASP version of tags (e.g., <%
%>) from PHP 6, they're keeping the short tag option debated in this thread,
and I hope they do.

To say that using short tags is bad form seems quite strong.  Additionally,
saying that XML doesn't accept the syntax is also extreme.  It's not that
XML doesn't accept the short tag.  Actually, PHP's parser is confused by the
XML declaration.  When you work with generating an XML document with PHP, it
only takes one line of code to accommodate PHP's parser.  However, the vast
majority of the time I'm generating XHTML 1 (or now more and more XHTML 5),
and neither of these require the XML declaration for validation purposes.

Additionally, wanting to write less code to perform the same action isn't
necessarily an act of laziness.  Am I lazy for loving the that I can use
simple message passing capabilities in Scala or transactional memory in
Clojure to perform tasks in parallel as opposed to the olden days of dealing
with threads in Java?  When I can type less, maintain clarity, and perform
the same action, I consider it an act of beauty.

When you compare the code samples below, I find that I prefer the short tag
version or the last example when I'm reworking the XHTML, as there's less
code to sift through.  There seems to be a trend in other templating
languages to shorten the amount of typing (e.g., Google Go uses
http://json-template.googlecode.com/svn/trunk/doc/Introducing-JSON-Template.html),
and I hope PHP will continue to keep pace with the trend, too.  That said,
I'm not taking exception with those who don't use the short tag, only with
those who say I shouldn't.

Adam

<section>
    <article>
        <h2><a href="<?php echo $url; ?>"><?php echo $title; ?></a></h2>
        <p>
            <?php echo $description; ?>
            <span><?php echo $date; ?></span>
        </p>
    </article>
    <article>
        <h2><a href="<?= $url ?>"><?= $title ?></a></h2>
        <p>
            <?= $description ?>
            <span><?= $date ?></span>
        </p>
    </article>
    <!--
        This last example is just to show the scheme I use in my own web
framework, which is similar to JSON template
    -->
    <article>
        <h2><a href="{url}">{title}</a></h2>
        <p>
            {description}
            <span>{date}</span>
        </p>
    </article>
</section>

-- 
Nephtali:  PHP web framework that functions beautifully
http://nephtaliproject.com

--- End Message ---
--- Begin Message ---
I have a script that connects to an external database to process about 4,000
records. Each record needs to be operated on pretty heavily, and the script
overall takes about half an hour to execute. We've hit a wall where the
script's memory usage exceeds the amount allocated to PHP. I've increased
the allotted memory to 32MB, but that's not ideal for our purposes.

So my thought is, would it be more efficient, memory-wise, to read the
database entries into an array and process the array records, rather than
maintain the database connection for the entire run of the script. This is
not an issue I've come across before, so any thoughts would be much
appreciated.

Thanks in advance.

-- 
Richard S. Crawford (rich...@underpope.com)
http://www.underpope.com
Publisher and Editor in Chief, Daikaijuzine (http://www.daikaijuzine.com)

--- End Message ---
--- Begin Message ---
What I usually do is to pull a limited set of records ( like 10 or 50
) and the do the operations on them, update a column in that table to
mark them completed and use JavaScript to reload the page and pull the
next set out where that flag field is null.

No memory issue, no need to large timeouts and it's self recovering.

Bastien

On Tuesday, March 16, 2010, Richard S. Crawford <rich...@underpope.com> wrote:
> I have a script that connects to an external database to process about 4,000
> records. Each record needs to be operated on pretty heavily, and the script
> overall takes about half an hour to execute. We've hit a wall where the
> script's memory usage exceeds the amount allocated to PHP. I've increased
> the allotted memory to 32MB, but that's not ideal for our purposes.
>
> So my thought is, would it be more efficient, memory-wise, to read the
> database entries into an array and process the array records, rather than
> maintain the database connection for the entire run of the script. This is
> not an issue I've come across before, so any thoughts would be much
> appreciated.
>
> Thanks in advance.
>
> --
> Richard S. Crawford (rich...@underpope.com)
> http://www.underpope.com
> Publisher and Editor in Chief, Daikaijuzine (http://www.daikaijuzine.com)
>

-- 

Bastien

Cat, the other other white meat

--- End Message ---
--- Begin Message --- Maybe you want to optimize your script first, and I don't think read entire data set into array would save you much time. Don't create new variables when its unnecessary, cache data when its necessary, try memcached, and I think heavy php cli scripts are always likely to resume a lot of resource, I think GC of php is not so reliable... or maybe I don't know how to use it.

On 3/16/2010 7:13 PM, Richard S. Crawford wrote:
I have a script that connects to an external database to process about 4,000
records. Each record needs to be operated on pretty heavily, and the script
overall takes about half an hour to execute. We've hit a wall where the
script's memory usage exceeds the amount allocated to PHP. I've increased
the allotted memory to 32MB, but that's not ideal for our purposes.

So my thought is, would it be more efficient, memory-wise, to read the
database entries into an array and process the array records, rather than
maintain the database connection for the entire run of the script. This is
not an issue I've come across before, so any thoughts would be much
appreciated.

Thanks in advance.



--- End Message ---

Reply via email to