php-general Digest 16 Mar 2013 07:55:12 -0000 Issue 8165
php-general Digest 16 Mar 2013 07:55:12 - Issue 8165 Topics (messages 320586 through 320605): Re: variable type - conversion/checking 320586 by: Ashley Sheridan 320589 by: richard gray 320591 by: Jim Lucas 320592 by: tamouse mailing lists 320596 by: Ashley Sheridan 320598 by: Andrew Ballard 320599 by: Sebastian Krebs 320600 by: Sebastian Krebs 320601 by: Andrew Ballard 320602 by: Andrew Ballard 320603 by: Matijn Woudt 320604 by: tamouse mailing lists Re: PHP context editor 320587 by: Ashley Sheridan 320593 by: tamouse mailing lists 320595 by: Curtis Maurand 320597 by: Curtis Maurand Re: Accessing Files Outside the Web Root 320588 by: Ashley Sheridan Re: PDO Transaction 320590 by: Lester Caine 320605 by: Simon Dániel Re: rather a HTML Q; however 2-FRAME 320594 by: tamouse mailing lists 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 -- ---BeginMessage--- On Fri, 2013-03-15 at 04:57 -0500, tamouse mailing lists wrote: On Fri, Mar 15, 2013 at 3:55 AM, Peter Ford p...@justcroft.com wrote: On 15/03/13 06:21, Jim Lucas wrote: On 3/14/2013 4:05 PM, Matijn Woudt wrote: On Thu, Mar 14, 2013 at 11:44 PM, Jim Lucas li...@cmsws.com wrote: On 03/14/2013 11:50 AM, Samuel Lopes Grigolato wrote: Something like if (is_numeric($var) $var == floor($var)) will do the trick. I don't know if there's a better (more elegant) way. On Thu, Mar 14, 2013 at 3:09 PM, Matijn Woudttijn...@gmail.com wrote: On Thu, Mar 14, 2013 at 7:02 PM, georggeorg.chamb...@telia.com** wrote: Hi, I have tried to find a way to check if a character string is possible to test whether it is convertible to an intger ! any suggestion ? BR georg You could use is_numeric for that, though it also accepts floats. - Matijn for that type of test I have always used this: if ( $val == (int)$val ) { http://www.php.net/manual/en/**language.types.integer.php#** language.types.integer.castinghttp://www.php.net/manual/en/language.types.integer.php#language.types.integer.casting I hope you're not serious about this... When comparing a string and an int, PHP will translate the string to int too, and of course they will always be equal then. So: $a = abc; if($a == (int)$a) echo YES; else echo NO; Will always return YES. - Matijn H... Interesting. Looking back at my code base where I thought I was doing that, turns out the final results were not that, but this: $value = asdf1234; if ( $value === (string)intval($value) ) { Looking back at the OP's request and after a little further searching, it seems that there might be a better possible solution for what the OP is requesting. ?php $values = array(asdf1234, 123.123, 123); foreach ( $values AS $value ) { echo $value; if ( ctype_digit($value) ) { echo ' - is all digits'; } else { echo ' - is NOT all digits'; } echo 'br /'.PHP_EOL; } returns... asdf1234 - is NOT all digits 123.123 - is NOT all digits 123 - is all digits http://www.php.net/manual/en/function.ctype-digit.php An important note: This function expects a string to be useful, so for example passing in an integer may not return the expected result. However, also note that HTML forms will result in numeric strings and not integers. See also the types section of the manual. -- Jim Integers can be negative too: I suspect your test would reject a leading '-'... For my money, `is_numeric()` does just what I want. The thing is, is_numeric() will not check if a string is a valid int, but any valid number, including a float. For something like this, wouldn't a regex be better? if(preg_match('/^\-?\d+$/', $string)) echo int Thanks, Ash http://www.ashleysheridan.co.uk ---End Message--- ---BeginMessage--- On 15/03/2013 22:00, Ashley Sheridan wrote: On Fri, 2013-03-15 at 04:57 -0500, tamouse mailing lists wrote: For my money, `is_numeric()` does just what I want. The thing is, is_numeric() will not check if a string is a valid int, but any valid number, including a float. For something like this, wouldn't a regex be better? if(preg_match('/^\-?\d+$/', $string)) echo int I'm late in on this thread so apologies if I have missed something here .. but wouldn't is_int() do what the OP wants? rich ---End Message--- ---BeginMessage--- On 03/15/2013 02:33 PM, richard gray wrote: On 15/03/2013 22:00, Ashley Sheridan wrote: On Fri, 2013-03-15 at 04:57 -0500, tamouse mailing lists
Re: [PHP] variable type - conversion/checking
On Fri, Mar 15, 2013 at 8:34 PM, Ashley Sheridan a...@ashleysheridan.co.uk wrote: On Thu, Mar 14, 2013 at 7:02 PM, georggeorg.chamb...@telia.com** I have tried to find a way to check if a character string is possible to test whether it is convertible to an intger ! The op wasn't about casting a string to an int but detecting if a string was just a string representation of an int. Hence using a regex to determine that. Regular expressions are not just about giving feedback to the user. Seemed to me that was exactly what it was about. Thanks, Ash http://www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PDO Transaction
2013/3/15 Lester Caine les...@lsces.co.uk Simon Dániel wrote: Hi, I have a trouble with PDO transactions. I would like to start using transactions, but I am not familiar with it. I have got a 'There is no active transaction' exception, however, I am using beginTransaction method, and also I have set PDO::ATTR_AUTOCOMMIT to false right after connecting to the database. In my whole code I have used the commit method only once. Between beginTransaction and commit methods, as far as I know, I did not use anything that could activate auto-commit. In my test code, there is only an exec method of PDO, and an execute of PDOStatement. Maybe one of these activated auto-commit? And what are the possible commands which are able to activate auto-commit? I know that the commit method can do that, but - as I already wrote - I have issued it only once, and it is in the destructor of a singleton class. So what could be the problem? You don't say which database you are trying to access. Not all actually support transactions, and some need a connection correctly set. I am using MySQL.
[PHP] FW: No Subject
http://www.coachholidays.co.uk/ramlm/hfoqauyyvomdvio.kaxkucbebccxpzq
Re: [PHP] variable type - conversion/checking
On Fri, 2013-03-15 at 22:32 -0400, Andrew Ballard wrote: Guess regex are the only useful solution here. When you consider to use built-in functions, just remember, that for example '0xAF' is an integer too, but '42.00' isn't. Shoot...I hadn't considered how PHP might handle hex or octal strings when casting to int. (Again, not in front of a computer where I can test it right now. ) Regexes have problems with more than 9 digits for 32-bit ints. I guess to some degree it depends on how likely you are to experience values that large. Andrew Do they? Regex's deal with strings, so I don't see why they should have such issues. I've certainly never come across that problem, or heard of it before. Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] PDO Transaction
Simon Dániel wrote: 2013/3/15 Lester Caine les...@lsces.co.uk Simon Dániel wrote: Hi, I have a trouble with PDO transactions. I would like to start using transactions, but I am not familiar with it. I have got a 'There is no active transaction' exception, however, I am using beginTransaction method, and also I have set PDO::ATTR_AUTOCOMMIT to false right after connecting to the database. In my whole code I have used the commit method only once. Between beginTransaction and commit methods, as far as I know, I did not use anything that could activate auto-commit. In my test code, there is only an exec method of PDO, and an execute of PDOStatement. Maybe one of these activated auto-commit? And what are the possible commands which are able to activate auto-commit? I know that the commit method can do that, but - as I already wrote - I have issued it only once, and it is in the destructor of a singleton class. So what could be the problem? You don't say which database you are trying to access. Not all actually support transactions, and some need a connection correctly set. I am using MySQL. Have you read the warning on http://php.net/manual/en/ref.pdo-mysql.php? It would be of more use if it actually identified what works and what does not :( I think you need to be running in the InnoDB engine to get transactions? MyISAM will throw errors on transactions. -- Lester Caine - G8HFL - Contact - http://lsces.co.uk/wiki/?page=contact L.S.Caine Electronic Services - http://lsces.co.uk EnquirySolve - http://enquirysolve.com/ Model Engineers Digital Workshop - http://medw.co.uk Rainbow Digital Media - http://rainbowdigitalmedia.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Type of a variable in PHP
Thanks Nora On Fri, Mar 15, 2013 at 4:34 PM, Sebastian Krebs krebs@gmail.comwrote: 2013/3/15 Kevin Peterson qh.res...@gmail.com Have two questions - 1. How to find type of a variable in PHP. gettype(), or one of the is_*()-functions. But for me more interesting for me: Why do you _need_ this? 2. How to find the type of an array in PHP. An array is of type array :) is_array() or again gettype(). Can you maybe describe what you want to achieve? In a dynamically typed language it is rarely _required_, that you know the _concrete_ type. Please help. -- github.com/KingCrunch
Re: [PHP] Type of a variable in PHP
Try this function get_type($var) { if(is_object($var)) return get_class($var); if(is_null($var)) return 'null'; if(is_string($var)) return 'string'; if(is_array($var)) return 'array'; if(is_int($var)) return 'integer'; if(is_bool($var)) return 'boolean'; if(is_float($var)) return 'float'; if(is_resource($var)) return 'resource'; //throw new NotImplementedException(); return 'unknown'; } On Fri, Mar 15, 2013 at 3:28 PM, Karim Geiger gei...@b1-systems.de wrote: Hi, On Fri, 2013-03-15 at 09:55 +, Kevin Peterson wrote: Have two questions - 1. How to find type of a variable in PHP. 2. How to find the type of an array in PHP. Please help. Use this: http://php.net/manual/en/function.gettype.php Regards Karim -- Karim Geiger B1 Systems GmbH Osterfeldstraße 7 / 85088 Vohburg / http://www.b1-systems.de GF: Ralph Dehner / Unternehmenssitz: Vohburg / AG: Ingolstadt,HRB 3537
Re: [PHP] variable type - conversion/checking
On Mar 16, 2013 6:14 AM, Ashley Sheridan a...@ashleysheridan.co.uk wrote: On Fri, 2013-03-15 at 22:32 -0400, Andrew Ballard wrote: Guess regex are the only useful solution here. When you consider to use built-in functions, just remember, that for example '0xAF' is an integer too, but '42.00' isn't. Shoot...I hadn't considered how PHP might handle hex or octal strings when casting to int. (Again, not in front of a computer where I can test it right now. ) Regexes have problems with more than 9 digits for 32-bit ints. I guess to some degree it depends on how likely you are to experience values that large. Andrew Do they? Regex's deal with strings, so I don't see why they should have such issues. I've certainly never come across that problem, or heard of it before. Thanks, Ash http://www.ashleysheridan.co.uk Sure. If the string is nine or fewer digits, they can all be 0-9. If it is 10 digits, the first digit can only be 1-2. If it's 1, the remaining nine digits can still be 0-9, but if the first digit is 2, the second digit can only be 0-1. If the second digit is 0, the remaining eight digits can still be 0-9, but if it is 1, the third digit can only be 0-4. If the third digit is 0-3, the remaining seven digits can still be 0-9, but if it is 4, the fourth digit can only be 0-7. This pattern would continue for each of the remaining digits. Hopefully you get the idea. When you get to the final digit, its range depends not only on the nine preceding digits, but also the sign. If this is 64-bit, that adds even more wrinkles (including being aware of whether your implementation supports 64-bit ints). It may be possible to do with regular expressions, but it would definitely be complex and probably a time sink. As I said, if you KNOW you won't be dealing with integers that are more than nine digits, the regex should work fine. Remember, the OP didn't ask if it was an integer in the realm of infinite pure integers; he asked how to tell if a string was a number that could be converted to an int (presumably without loss). Andrew
Re: [PHP] variable type - conversion/checking
On Sat, 2013-03-16 at 11:46 -0400, Andrew Ballard wrote: On Mar 16, 2013 6:14 AM, Ashley Sheridan a...@ashleysheridan.co.uk wrote: On Fri, 2013-03-15 at 22:32 -0400, Andrew Ballard wrote: Guess regex are the only useful solution here. When you consider to use built-in functions, just remember, that for example '0xAF' is an integer too, but '42.00' isn't. Shoot...I hadn't considered how PHP might handle hex or octal strings when casting to int. (Again, not in front of a computer where I can test it right now. ) Regexes have problems with more than 9 digits for 32-bit ints. I guess to some degree it depends on how likely you are to experience values that large. Andrew Do they? Regex's deal with strings, so I don't see why they should have such issues. I've certainly never come across that problem, or heard of it before. Thanks, Ash http://www.ashleysheridan.co.uk Sure. If the string is nine or fewer digits, they can all be 0-9. If it is 10 digits, the first digit can only be 1-2. If it's 1, the remaining nine digits can still be 0-9, but if the first digit is 2, the second digit can only be 0-1. If the second digit is 0, the remaining eight digits can still be 0-9, but if it is 1, the third digit can only be 0-4. If the third digit is 0-3, the remaining seven digits can still be 0-9, but if it is 4, the fourth digit can only be 0-7. This pattern would continue for each of the remaining digits. Hopefully you get the idea. When you get to the final digit, its range depends not only on the nine preceding digits, but also the sign. If this is 64-bit, that adds even more wrinkles (including being aware of whether your implementation supports 64-bit ints). It may be possible to do with regular expressions, but it would definitely be complex and probably a time sink. As I said, if you KNOW you won't be dealing with integers that are more than nine digits, the regex should work fine. Remember, the OP didn't ask if it was an integer in the realm of infinite pure integers; he asked how to tell if a string was a number that could be converted to an int (presumably without loss). Andrew Ah, I see. I think that's not an issue as such with regular expressions having problems, more that bit limitations has a problem with numbers! Bearing that in mind, this does the trick: $string1 = '9'; $string2 = '99'; $string3 = '99'; var_dump($string === (intval($string1).'')); var_dump($string === (intval($string2).'')); var_dump($string === (intval($string3).'')); I'm getting the expected results on my machine (32-bit) but a 64-bit machine would get the correct results for larger numbers. Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] variable type - conversion/checking
Hi, I have tried to find a way to check if a character string is possible to test whether it is convertible to an intger ! any suggestion ? BR georg All responses in this thread have been very nice; but you could also try a much simpler 2-step check: 1. is_numeric 2. if true check if there's a decimal character in the string: if(is_numeric($str) false === strpos('.', $str)) { // it's an int for sure } else { // might be a number, but it's definitly not an int } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] variable type - conversion/checking
On Sat, Mar 16, 2013 at 6:52 PM, Maciek Sokolewicz maciek.sokolew...@gmail.com wrote: Hi, I have tried to find a way to check if a character string is possible to test whether it is convertible to an intger ! any suggestion ? BR georg All responses in this thread have been very nice; but you could also try a much simpler 2-step check: 1. is_numeric 2. if true check if there's a decimal character in the string: if(is_numeric($str) false === strpos('.', $str)) { // it's an int for sure } else { // might be a number, but it's definitly not an int } Wrong. is_numeric will accept 1e1, which is a float, so you would need to check for e or E too. - Matijn
Re: [PHP] variable type - conversion/checking
On Sat, Mar 16, 2013 at 12:21 PM, Ashley Sheridan a...@ashleysheridan.co.uk wrote: On Sat, 2013-03-16 at 11:46 -0400, Andrew Ballard wrote: On Mar 16, 2013 6:14 AM, Ashley Sheridan a...@ashleysheridan.co.uk wrote: On Fri, 2013-03-15 at 22:32 -0400, Andrew Ballard wrote: Guess regex are the only useful solution here. When you consider to use built-in functions, just remember, that for example '0xAF' is an integer too, but '42.00' isn't. Shoot...I hadn't considered how PHP might handle hex or octal strings when casting to int. (Again, not in front of a computer where I can test it right now. ) Regexes have problems with more than 9 digits for 32-bit ints. I guess to some degree it depends on how likely you are to experience values that large. Andrew Do they? Regex's deal with strings, so I don't see why they should have such issues. I've certainly never come across that problem, or heard of it before. Thanks, Ash http://www.ashleysheridan.co.uk Sure. If the string is nine or fewer digits, they can all be 0-9. If it is 10 digits, the first digit can only be 1-2. If it's 1, the remaining nine digits can still be 0-9, but if the first digit is 2, the second digit can only be 0-1. If the second digit is 0, the remaining eight digits can still be 0-9, but if it is 1, the third digit can only be 0-4. If the third digit is 0-3, the remaining seven digits can still be 0-9, but if it is 4, the fourth digit can only be 0-7. This pattern would continue for each of the remaining digits. Hopefully you get the idea. When you get to the final digit, its range depends not only on the nine preceding digits, but also the sign. If this is 64-bit, that adds even more wrinkles (including being aware of whether your implementation supports 64-bit ints). It may be possible to do with regular expressions, but it would definitely be complex and probably a time sink. As I said, if you KNOW you won't be dealing with integers that are more than nine digits, the regex should work fine. Remember, the OP didn't ask if it was an integer in the realm of infinite pure integers; he asked how to tell if a string was a number that could be converted to an int (presumably without loss). Andrew Ah, I see. I think that's not an issue as such with regular expressions having problems, more that bit limitations has a problem with numbers! ANY computer system is going to have limitations with numbers -- you can't store infinity in a finite system. LOL Bearing that in mind, this does the trick: $string1 = '9'; $string2 = '99'; $string3 = '99'; var_dump($string === (intval($string1).'')); var_dump($string === (intval($string2).'')); var_dump($string === (intval($string3).'')); That's the same thing I posted, just different syntax. You are still converting the string to an int, converting the int back to a string, and comparing the resulting value to the original string using the identical (===) operator. Depending on one's needs, it could be tweaked a little to handle leading/trailing spaces, leading zeroes, etc. function is_int_hiding_as_string($value) { if (is_string($value)) { // remove any spaces around the value. $value = trim($value); // check for a sign :-) $sign = (substr($value, 0, 1) === '-') ? '-' : ''; // strip off the sign, any additional leading spaces or zeroes $value = ltrim($value, ' 0-'); // I didn't strip off trailing zeroes after the decimal because // I consider that a loss of precision, but you could do so // if necessary. return ($value === $sign . (int)$value); } return false; } I'm getting the expected results on my machine (32-bit) but a 64-bit machine would get the correct results for larger numbers. As I understood the original post, these ARE the correct results on ANY system. If the string value can be safely converted to an int *in the environment under which the script is executing* without loss of precision, this will return true; if it cannot, it returns false. Sorry for getting carried away, but it is SO much easier to respond on an actual keyboard than my phone. :-) Andrew -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php