php-general Digest 18 Feb 2006 21:06:41 -0000 Issue 3971

Topics (messages 230680 through 230699):

Re: "isset" or "array_key_exists"?
        230680 by: Rafael
        230686 by: Robert Cummings
        230687 by: Satyam
        230694 by: Rafael

Re: ŽQ‰Á‚µ‚Ă݂܂¹‚ñ‚©H
        230681 by: sns-ƒRƒ~ƒ…ƒjƒeƒB[

Re: Clear POST variables
        230682 by: John Wells
        230692 by: Kim Christensen

Re: Problem with HUGE floating values and fmod
        230683 by: Marco Almeida

Re: DB Error ??? help please ...
        230684 by: Jochem Maas

Re: Saving a BLOB image to a file (SOLVED)
        230685 by: tedd

Subtracting Large Numbers
        230688 by: Bruce

Different Values for intval(float)
        230689 by: Bruce
        230690 by: Ashley M. Kirchner

Re: attaching MySQL 5.0.18 to PHP 4.4.2
        230691 by: Kim Christensen

Retrieve URL Typed
        230693 by: Ministério Público
        230696 by: Kim Briggs

Retrieve the Http Host from the Http Header
        230695 by: Ministério Público

Recursive permissions
        230697 by: tedd
        230699 by: Kim Christensen

Re: "!" in front of a variable?
        230698 by: Gerry Danen

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 --- After a little test, although the results are not conclusive, I would say that isset(), and also that array_key_exists() may even use isset() (or similiar) internally as a first step -let's remember that isset() only does a "fast search" and it returns FALSE if the value is NULL; on the other hand, array_key_exists() returns TRUE even if the value is NULL- I said this (as an hypotesis) because the difference in time when the key exists and when it doesn't is quite big, sometimes about 10 times slower.

The script I used to test it was this (it is assumed that any "extra" operation affected both loops equally)
<?php

    $arr_test = array( 'key' => array( 1 => -1 ) );
    $num_oprs = 1000;


    $start    = micro_time();
    for ( $i = 0;  $i < $num_oprs;  $i ++ ) {
        if ( isset($arr_test['key'][1]) ) ;
    }
    echo  '<p>isset: ', micro_time() - $start, "</p>\n";


    $start = micro_time();
    for ( $i = 0;  $i < $num_oprs;  $i ++ ) {
        if ( array_key_exists(1, $arr_test['key']) ) ;
    }
    echo  '<p>array_key_exists: ', micro_time() - $start, "</p>\n";



    function micro_time( ) {
        return  preg_replace('/^0(\.\d+) (\d+)$/X',
                             '$2$1', microtime());
    }

 ?>

Nikolay Rastegaev wrote:
Please, answer:

what construction works faster:

   if (  isset ( $result [$i] ["key"] )  )
   {    do something;   }

or

   if (  array_key_exists ( "key", $result [$i] )  )
   {    do something;   }

?
--
Atentamente,
J. Rafael Salazar Magaña
Innox - Innovación Inteligente
Tel: +52 (33) 3615 5348 ext. 205 / 01 800 2-SOFTWARE
[EMAIL PROTECTED]
http://www.innox.com.mx

--- End Message ---
--- Begin Message ---
On Sat, 2006-02-18 at 04:56, Rafael wrote:
>       After a little test, although the results are not conclusive, I would 
> say that isset(), and also that array_key_exists() may even use isset() 
> (or similiar) internally as a first step -let's remember that isset() 
> only does a "fast search" and it returns FALSE if the value is NULL; on 
> the other hand, array_key_exists() returns TRUE even if the value is 
> NULL-  I said this (as an hypotesis) because the difference in time when 
> the key exists and when it doesn't is quite big, sometimes about 10 
> times slower.

isset is a keyword in PHP
array_key_exists() is a function.

Keywords are much faster than functions due tot he overhead functions
occur for setting up the stack.

If you don't care about null values, use isset(). If you do, use
array_key_exists().

The reason isset() doesn't return true for null entries has been
described in the past. The official stance was that null is not a value.

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 ---

----- Original Message ----- From: "Robert Cummings" <[EMAIL PROTECTED]>
To: "Rafael" <[EMAIL PROTECTED]>
Cc: "PHP-General" <[email protected]>
Sent: Saturday, February 18, 2006 3:21 PM
Subject: Re: [PHP] Re: "isset" or "array_key_exists"?


On Sat, 2006-02-18 at 04:56, Rafael wrote:
After a little test, although the results are not conclusive, I would
say that isset(), and also that array_key_exists() may even use isset()
(or similiar) internally as a first step -let's remember that isset()
only does a "fast search" and it returns FALSE if the value is NULL; on
the other hand, array_key_exists() returns TRUE even if the value is
NULL-  I said this (as an hypotesis) because the difference in time when
the key exists and when it doesn't is quite big, sometimes about 10
times slower.

isset is a keyword in PHP
array_key_exists() is a function.

Keywords are much faster than functions due tot he overhead functions
occur for setting up the stack.

If you don't care about null values, use isset(). If you do, use
array_key_exists().

The reason isset() doesn't return true for null entries has been
described in the past. The official stance was that null is not a value.

Cheers,
Rob.
--


Accessing a non-existing element, doesn't create it? Thus, using isset to evaluate whether an element exists creates it, though with a null value. If you make a first pass on the array with isset, a second pass with array_key_exists would give true for all of them though isset would give the same results as in the first pass. I think this happened to me once when I went through an array with isset or isempty or some such to make some calculations and then on the second pass, when I printed it, I got lots of empty cells that were created empty in the first pass.

Satyam

--- End Message ---
--- Begin Message --- Actually, it doesn't have much sense that it creates a variable (or index), though it had sense why wouldn't be so easily detected, so I printed the array after the loops and there's no new keys. I think that if that was the case, it was definitely a bug that has been corrected (PHP 4.4.0)
*Note: I guess that's because isset() is not a function, but a keyword

That was very ilustrative Rob, thanks for the info (it's the kind of thing I shouldn't forget)

Satyam wrote:
[···]
isset is a keyword in PHP
array_key_exists() is a function.

Keywords are much faster than functions due tot he overhead functions
occur for setting up the stack.

If you don't care about null values, use isset(). If you do, use
array_key_exists().

The reason isset() doesn't return true for null entries has been
described in the past. The official stance was that null is not a value.

[···]

Accessing a non-existing element, doesn't create it? Thus, using isset to evaluate whether an element exists creates it, though with a null value. If you make a first pass on the array with isset, a second pass with array_key_exists would give true for all of them though isset would give the same results as in the first pass. I think this happened to me once when I went through an array with isset or isempty or some such to make some calculations and then on the second pass, when I printed it, I got lots of empty cells that were created empty in the first pass.
--
Atentamente,
J. Rafael Salazar Magaña
Innox - Innovación Inteligente
Tel: +52 (33) 3615 5348 ext. 205 / 01 800 2-SOFTWARE
[EMAIL PROTECTED]
http://www.innox.com.mx

--- End Message ---
--- Begin Message ---
        ■□■ コミュニティー・エンターテイメント ■□■
□■□ ソーシャルネットワーキングサイトに参加してみませんか? □■□

メンバーより招待された方のみで構成されている為、安心快適!!
ポイント代・料金等は一切御座いません。全て無料でお使いになれます!

http://modulation-petal.net/tada/keijiban3/
テレビ・雑誌で話題!有名芸能人のブログ、写真等を全て無料でご覧になれます!

■これまでの友人関係を活性化できる
信頼できる旧知の友人、お知り合いとのお付き合いの活性化を図るさまざまな
ツールをご用意しています。

■「友人の友人」と交流できる・芸能人、アイドル、俳優と友達になれる!

友人同士のネットワークをたどって「友人の友人」との交流が
簡単にできます。そこにはあなたの友人から繋がる信頼できるネットワークが
形成されています。どこかで繋がっている人同士が集まるコミュニティー
であり、これがソーシャルネットワーキングの特徴です。

テレビ・雑誌で話題!ご利用料金・登録料は一切御座いません!全て無料でお使いになれます。

情報・コミュニティーに最適!
http://modulation-petal.net/tada/keijiban3/
それでは、参加を心よりお待ちしております。
※2006/02/24まで有効

--- End Message ---
--- Begin Message ---
[snip]
If it's not "wrong" for me to push the "back" button, why are you
breaking it with all your re-directs :-)
[/snip]

I was a bit short in my explanation -- I tend to use the redirect
method so that users *can* use the back button (or refresh).  I'm
trying to *not* break it. :)

That said, the unique token method is very interesting, I'll
definitely check it out.  But I'm curious, if you check for an
existing token and do find one (so the user has possibly refreshed the
browser), don't you have to program a particular way to handle it? And
might that handling method be different for each type of action being
taken? Does this run the risk of overcomplicating the code as well?

Then again, being forced to consider and handle *exceptions* is a very
good thing when developing a stable application...

I'll enjoy playing around with these ideas...

John




On 2/17/06, Curt Zirzow <[EMAIL PROTECTED]> wrote:
> On Thu, Feb 16, 2006 at 09:34:12PM -0600, Mike Tuller wrote:
> > ...
> >
> > This is how I learned in some book somewhere. Is everyone saying that
> > I need to either use sessions, or redirect so that when someone
> > refreshes insert.php, it doesn't submit the information again? To me
> > it seems that there has to be a more efficient way. I don't
> > understand the token thing that some are suggesting.
>
> Since  web requests are stateless you need to protect yourself
> in some ways, this is a method to prevent those duplicate entries
> in the db when someone refreshes the browser and reposts the data.
>
> The only difference with richards code with what I have is that he stores it
> differently than I generally do. The concept is as follows:
>
> form.php:
> <?php
>
> // generate a token
> $my_token = md5(uniqid('thisformid', true));
>
> // store the token in a place that can be retrieved
> // on the next place, richard uses a db, i usually just use the
> // _SESSION where it is stored isn't relevent
>
> $_SESSION['tokens'][$my_token] = time(); // use time() so we can expire
>
> // put the token in the form to be passed to the next page
> ?>
> <form>
>   <input type="hidden" name="form_token" value="<?php echo $my_token?>">
> </form>
>
>
> action.php:
> <?php
>
> // grab the token in the form:
> $token = $_POST['form_token'];
>
> // test it against what we stored in the previous page.
> if (isset($_SESSION['tokens'][$token]) ) {
>
>   // forget the token
>   unset($_SESSION['tokens'][$token]); // very important
>
>   // do stuff..
>
> } else {
>   // form submitted twice or they tried to access this page
>   // directly.. a no no.
> }
>
>
> Curt.
> --
> cat .signature: No such file or directory
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--- End Message ---
--- Begin Message ---
On 2/18/06, John Wells <[EMAIL PROTECTED]> wrote:
> That said, the unique token method is very interesting, I'll
> definitely check it out.  But I'm curious, if you check for an
> existing token and do find one (so the user has possibly refreshed the
> browser), don't you have to program a particular way to handle it? And
> might that handling method be different for each type of action being
> taken? Does this run the risk of overcomplicating the code as well?

In my particular case I normally just unset the $_REQUEST array, which
in turn makes my form handling code not run (i always check for
existing form values before processing them, but who doesn't...) and,
depending on where on the site everything takes place, redirect them
to a proper place.

--
Kim Christensen
[EMAIL PROTECTED]

--- End Message ---
--- Begin Message ---
THANKS!

That did the trick.

For future reference to all people needing to calculate check digits with
ISO 7604 (MOD 97-10) method:

$check=98-bcmod(($num."00"),97);

The ."00" part is imporant... If $num was multiplied by 100 we wouldn't get
a correct value in bcmod. 


Marco Almeida
Project Manager

WEBDADOS
Tecnologias de Informação

Telef.:  +351 21 466 93 00      
Fax:     +351 21 466 42 81      
Móvel:   +351 96 324 43 44      
Email:   [EMAIL PROTECTED]      
www:     http://www.webdados.pt 
Morada:  R. 9 de Abril, 3-3A, Sala 4
2765-545 S. Pedro do Estoril    

-----Original Message-----
From: Curt Zirzow [mailto:[EMAIL PROTECTED] 
Sent: sexta-feira, 17 de Fevereiro de 2006 20:40
To: [email protected]; [EMAIL PROTECTED]
Subject: Re: [PHP] Problem with HUGE floating values and fmod

On Fri, Feb 17, 2006 at 06:48:13PM -0000, Marco Almeida wrote:
> Hi there,
>  
> I'm trying to calculate check digits for a payment system, using the 
> ISO
> 7064 (MOD 97-10) algorithm, wich is, already translated to PHP:
> 
> $check=98-fmod(($num*100),97);
> 
> Where $num is a huge number...
> 
> Example: 90150123123400043211 should have check digit 51 In windows 
> calculator: 98 - ( (90150123123400043211 * 100) mod 97) works OK and I 
> get 51 as result
>
> In PHP I get 13...

You will need to use GMP or BCMath to do math operations numbers that size.

See:
http://php.net/language.types.integer.php


Curt.
--
cat .signature: No such file or directory

--- End Message ---
--- Begin Message ---
Mehmet Fatih Akbulut wrote:
hi all,
today, i get this error frequently :

do people keep coming up to you
in the street and shouting 'DB Error' at
you or what?


A fatal error has occurred
DB Error: extension not found
[line 542 of /var/www/horde/kronolith/lib/Driver/sql.php]
...

but php5-mysql is installed. and also i installed pear db ...

has PEAR DB got anything to do with this? I doubt it given that
there seems to be a custom DB driver included in whatever
you are trying to run.

and what is php5-mysql exactly (probably a RPM?)

what may cause this problem ? and how can i fix this ?

install the mysql extension or make the code work with
the myslqi extension:

http://nl2.php.net/manual/en/ref.mysql.php
http://nl2.php.net/manual/en/ref.mysqli.php

help please ...
many thanks in advance ...


--- End Message ---
--- Begin Message ---
On 2/16/06, tedd <[EMAIL PROTECTED]> wrote:

 However, after given it some thought, I would imagine that one could
 get a unique generated string, create a file with that string, give
 the file the correct permissions, then pull the image from the dB and
 save, do image processing, and then reload the image into a web page,
 and delete the file without any conflict. I can envision lot's of
 users doing that simultaneously without problems. BUT, I haven't
 tried it yet!

I have used a user's IP address (dots removed) and microtime appended
to create a unique file name. The target directory is world writable.
Not the most elegant solution... I found that sometimes the file had
not been written yet and trying to display it on a page right away
resulted in file not found...

--
Gerry

Gerry:

Above, I said that I haven't tried it yet, but I just finished the code and it works well enough, I think. The technique is pretty simple -- just create a tmp directory with 777 permissions and then run this code.

<?php

$token = md5(uniqid(1));
$token .= ".txt";
$filename = "tmp/$token";
$contents = "This is the content of the file.";

echo("<hr><br/>Writing file <br/><br/>");

$file = fopen( $filename, "w" );
fwrite( $file, $contents);
fclose( $file );

if( file_exists( $filename ) )
{ $file_length = filesize( $filename );
echo("File created at $filename<br/> ");
echo( "File size: $file_length bytes<br/>");
echo( "<br/>$contents<br/>" );
}
else
{
echo( "<br/>Unable to create file<br/>" );
}

echo("<hr><br/>Reading file:<br/><br/>");

$filesize = filesize($filename);
$file = fopen( $filename, "r" );
$text = fread( $file, $filesize );
fclose( $file );

echo( "File read at: $filename<br/> ");
echo( "File Size: $filesize bytes<br/>" );
echo( "<br/>$text<br/>" );

echo("<hr><br/>Deleting file <br/>");

$return = unlink($filename);
if($return)
{
echo("<br/>Successful delete of: $filename<br/>");
}
else
{
echo("<br/>Failed to delete of: $filename<br/>");
}

?>
--
--------------------------------------------------------------------------------
http://sperling.com/

--- End Message ---
--- Begin Message ---
I am puzzled by the following code:

<?php
print "<p>MaxInt=".PHP_INT_MAX;
$AA = -190668411;
$BB = -2181087916;
print "<br>AA=$AA";
print "<br>BB=$BB";
$AA = (int)$AA + (int)$BB;
print "<br>AA+BB=$AA";
?>

On some systems, I get:

MaxInt=2147483647
AA=-190668411
BB=-2181087916
AA+BB=-2338152059

On others, I get:

MaxInt=2147483647
AA=-190668411
BB=-2181087916
AA+BB=1923210969

Why the difference?

Thanks...Bruce

--- End Message ---
--- Begin Message ---
Consider the following code:

        $BB = -2181087916;
        $AA = (int)$BB;
        $AA = intval($BB);

On some systems, $AA will be int(-2147483648), which is actually
consistent with the documentation.

On most systems, however, $AA will be int(2113879380), which is the
same value truncated at 32 bits.

I actually need the latter behavior, as it needs to duplicate Delphi
code, which, like C, behaves the same way.

This is encryption code that does a bunch of calculations on signed
32-bit numbers and ignores any overflows.

Any ideas how I can do this?

--- End Message ---
--- Begin Message ---
Bruce wrote:
Consider the following code:

        $BB = -2181087916;
        $AA = (int)$BB;
        $AA = intval($BB);

On some systems, $AA will be int(-2147483648), which is actually
consistent with the documentation.

On most systems, however, $AA will be int(2113879380), which is the
same value truncated at 32 bits.
Interestingly enough, I tried it on several of my machines, which are different platforms and different (sub)versions of PHP and I got different results:

   RH7.3 with PHP 4.1.2:         2113879380

   IRIX 6.5.11 with PHP 4.2.3:   2147483647

   FC1 with PHP 4.3.6:           2113879380
   RH7.3 with PHP 4.3.9:         2113879380

   FC3 with PHP 4.3.10:         -2147483648
   FC4 with PHP 4.4.0:          -2147483648
   FC4 with PHP 5.0.5:          -2147483648

   So my question now, is it version or platform related?

--
H | I haven't lost my mind; it's backed up on tape somewhere.
 +--------------------------------------------------------------------
 Ashley M. Kirchner <mailto:[EMAIL PROTECTED]>   .   303.442.6410 x130
 IT Director / SysAdmin / WebSmith             .     800.441.3873 x130
 Photo Craft Imaging                       .     3550 Arapahoe Ave. #6
http://www.pcraft.com ..... . . . Boulder, CO 80303, U.S.A.
--- End Message ---
--- Begin Message ---
On 2/17/06, Paul Goepfert <[EMAIL PROTECTED]> wrote:
> I forgot to mention that that php and mysql are installed on a windows
>  machine which means I didn't have to run the configure script that
> would associate mysql with php.  How do I do this on a windows
> machine?

Open up php.ini, check that you have extension_dir set to the
directory where all your PHP extensions reside (typically C:\php\ext
on PHP5, and C:\php\extensions on PHP4). If the path is OK, then
scroll to section where all the extensions are to be loaded and
uncomment the line with "php_mysql.dll" in it. Restart your web server
and you should be set to go - check phpinfo() afterwards to see if it
loads ok.

--
Kim Christensen
[EMAIL PROTECTED]

--- End Message ---
--- Begin Message ---
I need a way to retrieve a URL typed by anyone who is visiting my site so
that I can redirect someone who typed for exemple www.site.com, to one of
the folders I have in the directory, and if that user typed
www.othersite.com he would be sent to another folder. I thank any insight.
Rodrigo

--- End Message ---
--- Begin Message ---
On 2/18/06, Ministério Público <[EMAIL PROTECTED]> wrote:
>
> I need a way to retrieve a URL typed by anyone who is visiting my site so
> that I can redirect someone who typed for exemple www.site.com, to one of
> the folders I have in the directory, and if that user typed
> www.othersite.com he would be sent to another folder. I thank any insight.
> Rodrigo


Hi Rodrigo,

Not really a PHP questions, but you can most likely make a local .htaccess
file to take care of this, even in a shared hosting environment.  I don't
know the details, but here is an example I copied to make all www.domain.comand
domain.com requests get grouped together for page ranking, etc.

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^www.kimbriggs\.com
RewriteRule ^(.*)$ http://kimbriggs.com/$1 [R=permanent,L]

cheers,
--
http://kimbriggs.com

--- End Message ---
--- Begin Message ---
I need a way to retrieve the URL typed by anyone visiting the website, so
that I can rerout them to the apropriate folder, does anyone know how to do
this?
 I got a message saying that I should use the phpinfo and use the variables
that are given as response, but the problem is that the variable that is
usefull is the HTTP HEADER HOST and I dont know how to retrieve it. I thank
anyone who can help.
Rodrigo

--- End Message ---
--- Begin Message ---
Hi gang:

Question: I know you can set a directory to have certain permissions, but how do you set the permissions to be recursive? In other words, for example, if you set the directory to be 755, then everything placed within that directory will also be 755.

Thanks.

tedd

--
--------------------------------------------------------------------------------
http://sperling.com/

--- End Message ---
--- Begin Message ---
On 2/18/06, tedd <[EMAIL PROTECTED]> wrote:
> Hi gang:
>
> Question: I know you can set a directory to have certain permissions,
> but how do you set the permissions to be recursive? In other words,
> for example, if you set the directory to be 755, then everything
> placed within that directory will also be 755.

http://php.net/manual/en/function.umask.php

--
Kim Christensen
[EMAIL PROTECTED]

--- End Message ---
--- Begin Message ---
! reverses a true/false input. I use it to flip a setting on or off.

Gerry

On 2/17/06, Jeff <[EMAIL PROTECTED]> wrote:
> Hey all,
>
> I've got some code from someone else I've inherited and need to sort out
> some problems with.  The programmer that wrote it originally was much
> better than I and programmed a little over my head to say the least.
>
> One function that I've come across that has 5 variables as input:
>
> function($var1,$var2,!$var2,$var3->cc,$var3->bcc);
>
> The question I have is on the 3rd input variable, what does the "!" in
> front of $var2 do to that variable?

--- End Message ---

Reply via email to