php-general Digest 17 Dec 2010 14:21:50 -0000 Issue 7091
php-general Digest 17 Dec 2010 14:21:50 - Issue 7091 Topics (messages 310094 through 310099): Does ReflectionMethod::setAccessible() do anything? 310094 by: David Harkness 310096 by: Nathan Nobbe Re: String passed to object constructor turning into aninstance of that object? 310095 by: Nathan Nobbe Re: Error Querying Database 310097 by: Phred White Re: PHPInfo disabled due to security 310098 by: Paul S Confusion About WordPress Cache 310099 by: ФêÏ 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--- According to the manual page for setAccessible() [1] the feature is available with 5.3.2, and I'm running 5.3.2-1ubuntu4.5 with Suhosin-Patch (cli) (built: Sep 17 2010 13:49:46) so I should be good to go. However, even the simplest test to make a protected or private method accessible fails. php class Foo { protected function bar() { echo foobar\n; } } php $m = new ReflectionMethod('Foo', 'bar'); php $m-setAccessible(true); php $foo = new Foo(); php $foo-bar(); Fatal error: Call to protected method Foo::bar() from context '' in php shell code on line 1 I've tried creating the object first, getting to the ReflectionMethod via ReflectionClass::getMethod(), and making a public method inaccessible, but all fail. Has anyone used this feature? The reason I want to do this, in case anyone can suggest a work-around, is to allow direct unit testing of protected/private methods in classes. What I do now is have a function that eval()s a dynamic subclass with a __call() method that proxies to protected methods. This doesn't allow testing private methods, and it breaks if the class under test has a private/protected constructor. I can solve the second problem with some further hacking, but there's nothing I can do about exposing private methods temporarily. setAccessible() seems perfectly designed to do what I need. Thanks, David [1] http://php.net/manual/en/reflectionmethod.setaccessible.php -- David Harkness Senior Software Engineer High Gear Media ---End Message--- ---BeginMessage--- On Thu, Dec 16, 2010 at 6:37 PM, David Harkness davi...@highgearmedia.comwrote: According to the manual page for setAccessible() [1] the feature is available with 5.3.2, and I'm running 5.3.2-1ubuntu4.5 with Suhosin-Patch (cli) (built: Sep 17 2010 13:49:46) so I should be good to go. However, even the simplest test to make a protected or private method accessible fails. php class Foo { protected function bar() { echo foobar\n; } } php $m = new ReflectionMethod('Foo', 'bar'); php $m-setAccessible(true); php $foo = new Foo(); php $foo-bar(); you just have to invoke the function from the context of the ReflectionMethod instance ?php class Foo { protected function bar() { echo foobar\n; } } $m = new ReflectionMethod('Foo', 'bar'); $m-setAccessible(true); $m-invokeArgs(new Foo(), array()); ? -nathan ---End Message--- ---BeginMessage--- On Thu, Dec 16, 2010 at 3:21 PM, Kris Deugau kdeu...@vianet.ca wrote: Nathan Nobbe wrote: Why not test for the type of $name at each point of interest in the SelectBoxOption constructor? If you're passing a string value to the constructor it almost has to be getting changed by the Tag constructor, right ? class SelectBoxOption extends Tag { function SelectBoxOption($name, $value, $selected=false) { var_dump(is_string($name)); parent::Tag(option, $name); var_dump(is_string($name)); Ah, that gives... well, it slightly alters the confusion. Using var_dump(is_string($name)) gives... two results? bool(true) bool(false) so you put one check before the call to parent::Tag() one directly after right? That means *somehow* $name is getting set to an instance of SelectBoxOption in the parent constructor which makes little to no sense.. especially after looking at implementation from your later post. Main things are $name is local in the child constructor and there is no pass by reference on the $name parameter in the parent constructor definition. if this code runs w/o error on your 5.2 box, then there's something spurious going on in that old library; ?php class Tag { function Tag($sTag='', $sValue='') { $this-_sTag = $sTag; $this-_sValue = $sValue; } } class Child extends Tag { function Child($name) { var_dump($name); parent::Tag('option', $name); var_dump($name); } } $oChild = new Child('content'); ? expected output: string(7) content string(7) content I'd still recommend moving to the php5 notation throughout the library, especially if doing that fixes the problem w/
[PHP]Confusion About WordPress Cache
I am using WordPress Cache to cache data retrieved from database, using WP_Cache http://codex.wordpress.org/Function_Reference/WP_Cache. After I used wp_cache_set to set some data in the cache, I try to get it by wp_cache_get in ANOTHER post request. However, it failed to retrieve the data I have previously saved. It is ok if I try to retrieve the data in just one request(place the *set*and *get * in the same script). *However, in separate * * * *requests, I doesn't work.* I wonder if I got the wrong idea of what *cache* is. I have it in mind that *cache *can be preserved even in different requests from the same user.*Then the cache has nothing different with a variable, I think!!* And I am doubting my idea now! Can anyone help? Or it is not what I am thinking. *Is there any way to retrieve the same data which has been saved * * * *before(not using database) in separate requests? * Really thanks for any response! Best regards! Xiaohan
Re: [PHP] PHPInfo disabled due to security
On Dec 16, 2010, at 10:39 PM, Paul S wrote: On Thu, 16 Dec 2010 00:13:31 +0700, Daniel P. Brown daniel.br...@parasane.net wrote: Well, phpinfo() does, by default, divulge some things that could be considered security concerns --- particularly in poorly-managed environments. Primarily, this is by giving a synopsis of versions and paths of software, but some versions and configurations will also broadcast information about the currently logged-in user (PTS/TTY) in the $_ENV display. Sure, you can display everything manually that phpinfo() does automatically, but it's easier for some to vilify something because they heard it was bad than to actually address the greater issues. In cases like this, I'd agree with Al's response; there are plenty of other web hosts out there. Well, I was hoping for stronger arguments to get that DONE. I would think there be something in the PHP license that would FORBID disabling functionality. After all, 'phpinfo' is essential, really, to achieving secure applications, isn't it? My setups are secure, I want to keep it that way. Shouldn't hosters be required to provide an alternative phpinfo, say behind the login control panel? I don't know that I would say that phpinfo() is essential. Helpful, yes. A pain in the neck when you need it and you don't have it - absolutely. But, there are ways around it. As daniel had mentioned already, you can call it all manually. If changing hosting is a problem, sit down, take an hour and write your own phpinfo(), all the info you need is in the manual. I can't see that anyone could upload a phpinfo command to a properly configured server and execute it. I have renamed my 'phpinfo.php' file to something innocuous. You have taken precautions, but it doesn't mean that another fella on the same server did. Unfortunately I've found changing hosting companies to often result in a lot of work for just as obnoxious tech service as the last. Perhaps writing a bit more portable code would alleviate the extra work. Of course, I do not know your specific situation, so it is not my call if it is even possible. But, the software engineer in me says to spend extra time writing code that can move from server to server easily. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP]Confusion About WordPress Cache
On Dec 17, 2010, at 8:21 AM, 肖晗 wrote: I am using WordPress Cache to cache data retrieved from database, using WP_Cache http://codex.wordpress.org/Function_Reference/WP_Cache. After I used wp_cache_set to set some data in the cache, I try to get it by wp_cache_get in ANOTHER post request. However, it failed to retrieve the data I have previously saved. It is ok if I try to retrieve the data in just one request(place the *set*and *get * in the same script). *However, in separate * * * *requests, I doesn't work.* I wonder if I got the wrong idea of what *cache* is. I have it in mind that *cache *can be preserved even in different requests from the same user.*Then the cache has nothing different with a variable, I think!!* And I am doubting my idea now! Can anyone help? Or it is not what I am thinking. *Is there any way to retrieve the same data which has been saved * * * *before(not using database) in separate requests? * Really thanks for any response! Best regards! Xiaohan I am not by any means saying that everyone here does not know the answer. Correct me if I am wrong, but this seems like a bit more of a specifically WP question than a PHP question, and you may get a better response from one of the WP mailing lists. http://codex.wordpress.org/Mailing_Lists
Re: [PHP] PHPInfo disabled due to security
On Thu, Dec 16, 2010 at 23:39, Paul S pau...@roadrunner.com wrote: Well, I was hoping for stronger arguments to get that DONE. I would think there be something in the PHP license that would FORBID disabling functionality. Really? You would really think that? Because we wouldn't. After all, 'phpinfo' is essential, really, to achieving secure applications, isn't it? No. Writing good code is essential. -- /Daniel P. Brown Network Infrastructure Manager Documentation, Webmaster Teams http://www.php.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP]Confusion About WordPress Cache
On Fri, Dec 17, 2010 at 09:32, Nicholas Kell n...@monkeyknight.com wrote: On Dec 17, 2010, at 8:21 AM, 肖晗 wrote: I am using WordPress Cache to cache data retrieved from database, using WP_Cache http://codex.wordpress.org/Function_Reference/WP_Cache. [snip!] Can anyone help? Or it is not what I am thinking. *Is there any way to retrieve the same data which has been saved * I am not by any means saying that everyone here does not know the answer. Correct me if I am wrong, but this seems like a bit more of a specifically WP question than a PHP question, and you may get a better response from one of the WP mailing lists. http://codex.wordpress.org/Mailing_Lists Exactly. -- /Daniel P. Brown Network Infrastructure Manager Documentation, Webmaster Teams http://www.php.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Problems w/ goto
Dear List - I am sending this again since it does not seem to have posted. Ethan +++ Dear List - Thank you with your excellent help in the past. Here is another puzzler I am trying to write a program that can have two(2) independent forms in one PHP file. When I run the code below [from PHP - A Beginner's Guide], to which I have added a second form, it freezes. Without the goto statements, it runs. When it does run, it displays both forms on one Web screen. What I desire is for the first form to be displayed, the data entered and then the second form displayed. In an actual, not test program like this one, the data in the second form would be dependent on the first form. What did I do wrong? Thanks in advance. Here is the code: !DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN DTD/xhtml1-transitional.dtd html xmlns=http://www.w3.org/1999/xhtml; xml:lang=en lang=en head titleProject 4-4: Age Calculator/title /head body h2Project 4-4: Age Calculator/h2 ?php // if form not yet submitted // display form $ender = 0; begin: if($ender == 1) exit(); if (!isset($_POST['dob'])) { start1: echo form method=\post\ action=\agecalc2.php\; echo Enter your date of birth, in mm/dd/ format: br /; echo input type=\text\ name=\dob\ /; echo p; echo input type=\submit\ name=\submit\ value=\Submit\ /; echo /form; goto begin; // if form submitted // process form input } else { starter: if (isset($_POST['cat'])) goto purr; // split date value into components $dateArr = explode('/', $_POST['dob']); // calculate timestamp corresponding to date value $dateTs = strtotime($_POST['dob']); // calculate timestamp corresponding to 'today' $now = strtotime('today'); // check that the value entered is in the correct format if (sizeof($dateArr) != 3) { die('ERROR: Please enter a valid date of birth'); } // check that the value entered is a valid date if (!checkdate($dateArr[0], $dateArr[1], $dateArr[2])) { die('ERROR: Please enter a valid date of birth'); } // check that the date entered is earlier than 'today' if ($dateTs = $now) { die('ERROR: Please enter a date of birth earlier than today'); } // calculate difference between date of birth and today in days // convert to years // convert remaining days to months // print output $ageDays = floor(($now - $dateTs) / 86400); $ageYears = floor($ageDays / 365); $ageMonths = floor(($ageDays - ($ageYears * 365)) / 30); echo You are approximately $ageYears years and $ageMonths months old.; goto meow; } meow: if (!isset($_POST['dob'])) goto begin; if (!isset($_POST['cat'])) { echo form method=\post\ action=\agecalc2.php\; echo br /br /Enter your kitten's name: br /; echo input type=\text\ name=\cat\ /; echo p; echo input type=\submit\ name=\submit\ value=\Submit Kitten\ /; echo /form; } else { purr: $name_cat = $_POST['cat']; echo Your Kitten is $name_cat; $ender = 1; } if ($ender == 0) goto begin; first_step: ? /body /html Ethan MySQL 5.1 PHP 5 Linux [Debian (sid)] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String passed to object constructor turning into aninstance of that object?
David Harkness wrote: I've never used the old-style constructors, but perhaps the semantics of parent:: changed and you need to instead use $this- as in $this-Tag(option, $name); That's a total guess. I don't have 5.2 handy to try it out, but both work in 5.3 using a simple example. Can you post the constructor of one of the Tag subclasses that work? Maybe we can find a common denominator. The most similar is Column, but all of them do very similar things - it's just the one class that seems to take a string and mutate it into what looks like an array with a string at [0] and something closely resembling what the whole object instance *should* be at [1]. class Table extends Tag { function Table() { parent::Tag('table'); $this-addAttribute('cellspacing', 0); $this-addAttribute('cellpadding', 0); $this-addAttribute('border', 0); $this-columns = array(); $this-rows = array(); } class Row extends Tag { function Row($table='') { parent::Tag('tr'); $this-table = ''; } class Column extends Tag { function Column($data) { parent::Tag('td', $data); $this-tagContent = $data; } class FormObject extends Tag { function FormObject($name='') { parent::Tag(); $this-addAttribute(name, $name); $this-name = $name; } -kgd -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Problems w/ goto
[snip] Thank you with your excellent help in the past. Here is another puzzler I am trying to write a program that can have two(2) independent forms in one PHP file. When I run the code below [from PHP - A Beginner's Guide], to which I have added a second form, it freezes. Without the goto statements, it runs. When it does run, it displays both forms on one Web screen. What I desire is for the first form to be displayed, the data entered and then the second form displayed. In an actual, not test program like this one, the data in the second form would be dependent on the first form. What did I do wrong? [/snip] You used GOTO. In this case I would recommend using something like jQuery to 'hide' one form until the other form is complete. PHP has sent the output to the browser already, both forms are there and display when you remove the GOTO. GOTO should never be used like this. GOTO should never be used. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String passed to object constructor turning into aninstance of that object?
Nathan Nobbe wrote: 2. try modifying Tag SelectBoxOption to have __construct() instead of Tag() SelectBoxOption(), then call parent::__construct() from inside of SelectBoxOption::__construct(); see if that clears up your problem under 5.2 (read: this will only be a partial solution as it only addresses one child of Tag). Mmm. I hoped this would help, but all it seems to have done was cascade errors across the rest of Tag's object children. :( to be expected, but did it fix the problem w/ SelectBoxOption? I'm not sure, but I don't think so; the original symptom was Object of class SelectBoxOption could not be converted to string - the original code didn't include a toString() method in SelectBoxOption, and since the code works on an older PHP, it must be using the parent object's toString(). (Which some children of Tag do explicitly, but SelectBoxOption doesn't for whatever reason.) In trying to add the toString() method, I found that the calls used by other tags to retrieve the HTML tag name, value, etc weren't working. So I looked up at the constructor to see if the pieces passed in were getting passed and stored correctly - and quite obviously they're not ($name mutates into what looks like an array with a string at [0] and something closely resembling what the whole object instance *should* be at [1], and $value just seems to disappear). Putting aside actually fixing the constructor correctly, after a bit of poking I found that $this-tagContent-. works to retrieve the data and actually output the option tag correctly. var_dump tells me that the real data is actually in there... it's just not instantiated correctly. $name apparently arrives at the constructor for SelectOptionBox like this: string(8) Abegweit object(SelectBoxOption)#65 (5) { [attributes]= array(1) { [0]= object(TagAttribute)#66 (3) { [name]= string(5) value [value]= string(1) 4 [hasValue]= bool(true) } } [tagContent]= string(8) Abegweit [tag]= string(6) option [showEndTag]= bool(false) [children]= array(0) { } } I'll try converting all of the constructors to your recommendation as above, but given that the problem is only happening with this one class, I'm not sure that will do much. hopefully that clears it up .. Well, I ran out of Call to undefined ParentClass::parentclass in path/to/file/for/subclass.php errors (at least on the page I'm testing with) but $name is still going in on the calling side as a string, and coming out as a funky array. and hopefully you're using version control :D Bah! Real man never make mistaaake! ... ooops. g (I've got the live site, on the old server, as reference, plus the regular backups of that machine.) -kgd -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Problems w/ goto
On Fri, 2010-12-17 at 10:50 -0600, Jay Blanchard wrote: [snip] Thank you with your excellent help in the past. Here is another puzzler I am trying to write a program that can have two(2) independent forms in one PHP file. When I run the code below [from PHP - A Beginner's Guide], to which I have added a second form, it freezes. Without the goto statements, it runs. When it does run, it displays both forms on one Web screen. What I desire is for the first form to be displayed, the data entered and then the second form displayed. In an actual, not test program like this one, the data in the second form would be dependent on the first form. What did I do wrong? [/snip] You used GOTO. In this case I would recommend using something like jQuery to 'hide' one form until the other form is complete. PHP has sent the output to the browser already, both forms are there and display when you remove the GOTO. GOTO should never be used like this. GOTO should never be used. Wow... that brought me back to 1990... using basic and batch files... I honestly didn't even know that the GOTO was still in existence, especially within PHP. I had to show the people in my office, and we all got a chuckle from teh XKCD comic in the PHP documentation for GOTO http://ca2.php.net/goto Steve -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Problems w/ goto
On 17 December 2010 17:08, Steve Staples sstap...@mnsi.net wrote: On Fri, 2010-12-17 at 10:50 -0600, Jay Blanchard wrote: [snip] Thank you with your excellent help in the past. Here is another puzzler I am trying to write a program that can have two(2) independent forms in one PHP file. When I run the code below [from PHP - A Beginner's Guide], to which I have added a second form, it freezes. Without the goto statements, it runs. When it does run, it displays both forms on one Web screen. What I desire is for the first form to be displayed, the data entered and then the second form displayed. In an actual, not test program like this one, the data in the second form would be dependent on the first form. What did I do wrong? [/snip] You used GOTO. In this case I would recommend using something like jQuery to 'hide' one form until the other form is complete. PHP has sent the output to the browser already, both forms are there and display when you remove the GOTO. GOTO should never be used like this. GOTO should never be used. Wow... that brought me back to 1990... using basic and batch files... I honestly didn't even know that the GOTO was still in existence, especially within PHP. I had to show the people in my office, and we all got a chuckle from teh XKCD comic in the PHP documentation for GOTO http://ca2.php.net/goto Steve -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php And have you seen all the sad faces ... : { on http://docs.php.net/manual/en/control-structures.goto.php#92763 Can't be good for them. -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Problems w/ goto
On Dec 17, 2010, at 11:08 AM, Steve Staples wrote: [snip /] GOTO should never be used like this. GOTO should never be used. Wow... that brought me back to 1990... using basic and batch files... I honestly didn't even know that the GOTO was still in existence, especially within PHP. I had to show the people in my office, and we all got a chuckle from teh XKCD comic in the PHP documentation for GOTO http://ca2.php.net/goto Steve I didn't know it existed in PHP either. The cartoon is priceless. I should probably hang that up in my office somewhere.
[PHP] HTML id attribute and arrays
Hi, PHP converts x[a]=b parameter of the HTTP request as an array named x with its item named a set to value b. So, it seems possible to have the following (X)HTML code: input type=text name=x[a] id=x[a] value=b / Unfortunatelly, HTML specification does not allow neither [ nor ] inside the id attribute. Specifically: * Must begin with a letter A-Z or a-z * Can be followed by: letters (A-Za-z), digits (0-9), hyphens (-), underscores (_), colons (:), and periods (.) * Values are case-sensitive How should I follow the HTML specification while having the passed parameters automatically converted to arrays in PHP? Thank you for any tips, Martin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] HTML id attribute and arrays
Martin C m...@seznam.cz wrote: How should I follow the HTML specification while having the passed parameters automatically converted to arrays in PHP? The name attribute, not the id attribute, is used as the key when submitting form values. The name and id attributes do not have to be the same. -- Benjamin Hawkes-Lewis -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Problems w/ goto
On Fri, 2010-12-17 at 12:22 -0500, Robert Cummings wrote: On 10-12-17 12:08 PM, Steve Staples wrote: On Fri, 2010-12-17 at 10:50 -0600, Jay Blanchard wrote: [snip] Thank you with your excellent help in the past. Here is another puzzler I am trying to write a program that can have two(2) independent forms in one PHP file. When I run the code below [from PHP - A Beginner's Guide], to which I have added a second form, it freezes. Without the goto statements, it runs. When it does run, it displays both forms on one Web screen. What I desire is for the first form to be displayed, the data entered and then the second form displayed. In an actual, not test program like this one, the data in the second form would be dependent on the first form. What did I do wrong? [/snip] You used GOTO. In this case I would recommend using something like jQuery to 'hide' one form until the other form is complete. PHP has sent the output to the browser already, both forms are there and display when you remove the GOTO. GOTO should never be used like this. GOTO should never be used. Wow... that brought me back to 1990... using basic and batch files... I honestly didn't even know that the GOTO was still in existence, especially within PHP. I had to show the people in my office, and we all got a chuckle from teh XKCD comic in the PHP documentation for GOTO http://ca2.php.net/goto Steve I was one of the people that argued in favour of GOTO on the Internals list a few years ago. GOTO has a use, and a very good one at that. It is by far the most efficient construct when creating parsers or other similar types of logic flow. The demonized GOTO of the 80s was primarily due to jumping to arbitrary points in the code, or in the case of basic to arbitrary line numbers in the code which had little meaning for future readers. When used properly within a well defined scope and with well named labels, GOTO can be a superior choice. If you think GOTO doesn't exist in many types of software, you need only grep for it in the C source code for PHP, MySQL, and Apache. Cheers, Rob. Oh, i can see where it would be useful, i just hadn't realized it was still in existence... and the cartoon, was blown up, and put in my cubical :) RAWR!! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: [PHP-DB] Re: [PHP] Problems w/ goto
On Fri, Dec 17, 2010 at 12:16, Richard Quadling rquadl...@gmail.com wrote: And have you seen all the sad faces ... : { on http://docs.php.net/manual/en/control-structures.goto.php#92763 Can't be good for them. If only people knew how many hours - literally, hours - it took me to keep that page clean and free from vandalism after GOTO was introduced. -- /Daniel P. Brown Network Infrastructure Manager Documentation, Webmaster Teams http://www.php.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Problems w/ goto
On Fri, Dec 17, 2010 at 12:22, Robert Cummings rob...@interjinn.com wrote: I was one of the people that argued in favour of GOTO on the Internals list a few years ago. GOTO has a use, and a very good one at that. It is by far the most efficient construct when creating parsers or other similar types of logic flow. The demonized GOTO of the 80s was primarily due to jumping to arbitrary points in the code, or in the case of basic to arbitrary line numbers in the code which had little meaning for future readers. Not to mention failure to properly break, as in the OP's code example. -- /Daniel P. Brown Dedicated Servers, Cloud and Cloud Hybrid Solutions, VPS, Hosting (866-) 725-4321 http://www.parasane.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Problems w/ goto
On 12/17/10 11:57 AM, Steve Staples wrote: I had to show the people in my office, and we all got a chuckle from teh XKCD comic in the PHP documentation for GOTO http://ca2.php.net/goto Steve I was one of the people that argued in favour of GOTO on the Internals list a few years ago. GOTO has a use, and a very good one at that. It is by far the most efficient construct when creating parsers or other similar types of logic flow. The demonized GOTO of the 80s was primarily due to jumping to arbitrary points in the code, or in the case of basic to arbitrary line numbers in the code which had little meaning for future readers. When used properly within a well defined scope and with well named labels, GOTO can be a superior choice. If you think GOTO doesn't exist in many types of software, you need only grep for it in the C source code for PHP, MySQL, and Apache. Cheers, Rob. Oh, i can see where it would be useful, i just hadn't realized it was still in existence... and the cartoon, was blown up, and put in my cubical :) RAWR!! It's really a labeling problem. Goto usually refers to jump to arbitrary line, at least when used in the vernacular. That's horribly bad and evil. What PHP has implemented is named break statements, as I understand it. Those are not inherently evil. It doesn't break program flow any more than an exception does, but it's useful for non-error-handling cases. However, it reuses the keyword goto which is easy to confuse with the abomination above. It was a rather poor choice of name, frankly. --Larry Garfield -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] How to define a data range for graphing?
Hey all - I'm trying to provide reporting to users of our widget. Some may get 0 to 5 hits a day; others may get up to 10,000 hits a day. I need to define the range of the graph (using one of Google's). If their max day is 7, I'd like the graph to go from 0 to 10. If their max day is 5678, I'd probably like it to go from 0 to 6000. I'm guessing the way to do this is with a ceiling function? - Brian -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to define a data range for graphing?
On 18/12/2010, at 8:45 AM, Brian Dunning wrote: Hey all - I'm trying to provide reporting to users of our widget. Some may get 0 to 5 hits a day; others may get up to 10,000 hits a day. I need to define the range of the graph (using one of Google's). If their max day is 7, I'd like the graph to go from 0 to 10. If their max day is 5678, I'd probably like it to go from 0 to 6000. I'm guessing the way to do this is with a ceiling function? - Brian -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Personally, I'd use something like: if($hits 10) { $top = 10; } else { $top = ($hits[0] + 1) * pow(10, strlen($hits) - 1); } Basically, add one to the first digit and make the rest of the digits 0. Depending on how you want to handle the cases ending in 0s, you may want an extra check in there. General disclaimer about code typed directly into mail client. Happy Saturday :) --- Simon Welsh Admin of http://simon.geek.nz/ Who said Microsoft never created a bug-free program? The blue screen never, ever crashes! http://www.thinkgeek.com/brain/gimme.cgi?wid=81d520e5e -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to define a data range for graphing?
On Friday, 17 December 2010, Simon J Welsh si...@welsh.co.nz wrote: On 18/12/2010, at 8:45 AM, Brian Dunning wrote: Hey all - I'm trying to provide reporting to users of our widget. Some may get 0 to 5 hits a day; others may get up to 10,000 hits a day. I need to define the range of the graph (using one of Google's). If their max day is 7, I'd like the graph to go from 0 to 10. If their max day is 5678, I'd probably like it to go from 0 to 6000. I'm guessing the way to do this is with a ceiling function? - Brian -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Personally, I'd use something like: if($hits 10) { $top = 10; } else { $top = ($hits[0] + 1) * pow(10, strlen($hits) - 1); } Basically, add one to the first digit and make the rest of the digits 0. Depending on how you want to handle the cases ending in 0s, you may want an extra check in there. General disclaimer about code typed directly into mail client. Happy Saturday :) Personally I'd add a percentage on top, say 5 or 10 percent. Regards Peter -- hype WWW: plphp.dk / plind.dk LinkedIn: plind BeWelcome/Couchsurfing: Fake51 Twitter: kafe15 /hype -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] array question
Hello all! I have a question regarding arrays and the way I can use a value. Let's say I have this string: $s = 'banana,apple,mellon,grape,nut,orange' I want to explode it, and get the third value. For this I would normally do: $a = explode(',', $s); echo $s[2]; That's all fine, but is there a way to get the value directly, without having to write another line in my script. I mean something like this: echo explode(',', $s)[2]; or echo {explode(',', $s)}[2]; I couldn't find out this answer anywhere, that's why I posted here. Cheers and thanks!
RE: [PHP] array question
[snip] I have a question regarding arrays and the way I can use a value. Let's say I have this string: $s = 'banana,apple,mellon,grape,nut,orange' I want to explode it, and get the third value. For this I would normally do: $a = explode(',', $s); echo $s[2]; That's all fine, but is there a way to get the value directly, without having to write another line in my script. I mean something like this: echo explode(',', $s)[2]; or echo {explode(',', $s)}[2]; I couldn't find out this answer anywhere, that's why I posted here. [/snip] Because the array is not formed until after the explode you cannot do it with one command, but you could place 2 commands on one line :) $a = explode(',', $s); echo $a[2]; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array question
On Fri, Dec 17, 2010 at 15:52, Sorin Buturugeanu m...@soin.ro wrote: Hello all! I have a question regarding arrays and the way I can use a value. Let's say I have this string: $s = 'banana,apple,mellon,grape,nut,orange' I want to explode it, and get the third value. For this I would normally do: $a = explode(',', $s); echo $s[2]; That's all fine, but is there a way to get the value directly, without having to write another line in my script. I mean something like this: echo explode(',', $s)[2]; or echo {explode(',', $s)}[2]; I couldn't find out this answer anywhere, that's why I posted here. Unfortunately, no --- at least, not yet. Chaining discussions come up now and again, so it's quite possible that future versions of PHP will have something similar. That said, for now you could do something like this: ?php /** * mixed return_item( string $car, mixed $pos ) * - $str The original string * - $charThe delimiting character(s) by which to explode * - $pos The position to return * - $shift Whether or not we should see 1 as the first array position */ function return_item($str,$char,$pos=null,$shift=false) { // Make sure $char exists in $str, return false if not. if (!strpos($str,$char)) return false; // Split $char by $str into the array $arr $arr = explode($char,$str); // If $pos undefined or null, return the whole array if (is_null($pos)) return $arr; // If $pos is an array, return the requested positions if (isset($pos) is_array($pos) !empty($pos)) { // Instantiate a second array container for return $ret = array(); // Iterate foreach ($pos as $i) { // This is just in case it was given screwy or a number as a non-integer if (!is_int($i) is_numeric($i)) $i = (int)round($i); // Make sure $i is now an integer and that position exists if (!is_int($i) || !isset($arr[$i]) || empty($arr[$i])) continue; // If all seems okay, append this to $ret $ret[] = $arr[$i]; } // Return the array return $ret; } /** * If $pos is a number (integer or round()'able number), * we'll go ahead and make sure the position is there. * If so, we'll return it. */ if (is_int($pos) || is_numeric($pos)) { // This is just in case it was given screwy or as a non-integer if (!is_int($pos)) $pos = (int)round($pos); // If we want to start the array count at 1, do that now if (isset($shift) ($shift === true || $shift === 1)) { // but only if the number isn't zero if ($pos !== 0) --$pos; } // Return the single position if it exists if (isset($arr[$pos]) !empty($arr[$pos])) return $arr[$pos]; } /** * If we've failed every case, something is either * wrong or we supplied bad data. Return false. * Either way, feel free to add some trigger_error() * stuff here if you want to have the function hold * your hand. */ return false; } /** * Some simple examples */ $foo = 'apple,banana,carrot,orange,carrot,lettuce,tomato,beer,carrot,idiot'; return_item($foo,',',7); // Returns 'beer' return_item($foo,'carrot',0); // Returns 'apple,banana,' return_item($foo,','); // Returns all items in an array return_item($foo,',',array(0,'2',6.6)); // Returns array: apple,carrot,beer return_item($foo,',',1,true); // Returns 'apple' ? Of course, as with almost all code I submit here, it's typed directly into this window and is untested, so you use it at your own risk, your mileage may vary, see a doctor if you have an erection lasting more than four hours, et cetera. Happy Friday, all. -- /Daniel P. Brown Network Infrastructure Manager Documentation, Webmaster Teams http://www.php.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array question
Tanks for all of your responses! I guess a function is the way to go. I just have to see if the situation comes up enough times to justify the function approach. @Dan: I really enjoyed your disclaimer :D -- Sorin Buturugeanu www.soin.ro http://www.facebook.com/buturugeanu http://www.twitter.com/soinrohttp://www.soin.ro/feed/blogblog: Despre Launch48 si ce poti face in 2 zile http://www.soin.ro/b75 On 17 December 2010 23:48, Daniel Brown danbr...@php.net wrote: On Fri, Dec 17, 2010 at 15:52, Sorin Buturugeanu m...@soin.ro wrote: Hello all! I have a question regarding arrays and the way I can use a value. Let's say I have this string: $s = 'banana,apple,mellon,grape,nut,orange' I want to explode it, and get the third value. For this I would normally do: $a = explode(',', $s); echo $s[2]; That's all fine, but is there a way to get the value directly, without having to write another line in my script. I mean something like this: echo explode(',', $s)[2]; or echo {explode(',', $s)}[2]; I couldn't find out this answer anywhere, that's why I posted here. Unfortunately, no --- at least, not yet. Chaining discussions come up now and again, so it's quite possible that future versions of PHP will have something similar. That said, for now you could do something like this: ?php /** * mixed return_item( string $car, mixed $pos ) * - $str The original string * - $charThe delimiting character(s) by which to explode * - $pos The position to return * - $shift Whether or not we should see 1 as the first array position */ function return_item($str,$char,$pos=null,$shift=false) { // Make sure $char exists in $str, return false if not. if (!strpos($str,$char)) return false; // Split $char by $str into the array $arr $arr = explode($char,$str); // If $pos undefined or null, return the whole array if (is_null($pos)) return $arr; // If $pos is an array, return the requested positions if (isset($pos) is_array($pos) !empty($pos)) { // Instantiate a second array container for return $ret = array(); // Iterate foreach ($pos as $i) { // This is just in case it was given screwy or a number as a non-integer if (!is_int($i) is_numeric($i)) $i = (int)round($i); // Make sure $i is now an integer and that position exists if (!is_int($i) || !isset($arr[$i]) || empty($arr[$i])) continue; // If all seems okay, append this to $ret $ret[] = $arr[$i]; } // Return the array return $ret; } /** * If $pos is a number (integer or round()'able number), * we'll go ahead and make sure the position is there. * If so, we'll return it. */ if (is_int($pos) || is_numeric($pos)) { // This is just in case it was given screwy or as a non-integer if (!is_int($pos)) $pos = (int)round($pos); // If we want to start the array count at 1, do that now if (isset($shift) ($shift === true || $shift === 1)) { // but only if the number isn't zero if ($pos !== 0) --$pos; } // Return the single position if it exists if (isset($arr[$pos]) !empty($arr[$pos])) return $arr[$pos]; } /** * If we've failed every case, something is either * wrong or we supplied bad data. Return false. * Either way, feel free to add some trigger_error() * stuff here if you want to have the function hold * your hand. */ return false; } /** * Some simple examples */ $foo = 'apple,banana,carrot,orange,carrot,lettuce,tomato,beer,carrot,idiot'; return_item($foo,',',7); // Returns 'beer' return_item($foo,'carrot',0); // Returns 'apple,banana,' return_item($foo,','); // Returns all items in an array return_item($foo,',',array(0,'2',6.6)); // Returns array: apple,carrot,beer return_item($foo,',',1,true); // Returns 'apple' ? Of course, as with almost all code I submit here, it's typed directly into this window and is untested, so you use it at your own risk, your mileage may vary, see a doctor if you have an erection lasting more than four hours, et cetera. Happy Friday, all. -- /Daniel P. Brown Network Infrastructure Manager Documentation, Webmaster Teams http://www.php.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php