Re: [PHP] Switch statement Question
Hi, I have a code snippet here as in the following: //Switch statements between the four options switch($string) { case : $string= NOT book.author='All'; break; default: $string= $string . AND NOT book.author='All'; break; } This code does work, but I am wondering if it is possible in the switch statement clauses for me to do something like case does not equal to a certain author name if I don't want $string with that content to be processed. or, do I always use default in this case? Thanks in advance. Alice _ All-in-one security and maintenance for your PC. Get a free 90-day trial! http://www.windowsonecare.com/purchase/trial.aspx?sc_cid=wl_wlmail Well I will have to mention that switch becomes if after all internally so you could always use the if statement. I am a great fan of switch but since eclipse fail to format correctly embedded switch statements I am starting to use the old all good if() since it is the same thing. It is a matter of style actually although switch is slightly slower (so slightly that you can't notice in any case). I think there was a thread in this list for this comparison a few days ago. -- Thodoris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Switch statement Question
-Original Message- From: Alice Wei [mailto:aj...@alumni.iu.edu] Sent: Thursday, January 29, 2009 3:02 PM To: php-general@lists.php.net Subject: [PHP] Switch statement Question Hi, I have a code snippet here as in the following: //Switch statements between the four options switch($string) { case : $string= NOT book.author='All'; break; default: $string= $string . AND NOT book.author='All'; break; } This code does work, but I am wondering if it is possible in the switch statement clauses for me to do something like case does not equal to a certain author name if I don't want $string with that content to be processed. or, do I always use default in this case? It's a bit non-conventional, but the switch block can be used like so: switch(true) { case (x y): dosomething(); break; case (y == 0): dosomethingelse(); break; default: somethingelseentirely(); break; } ...this way, your case statements can be expressions themselves, and it will always pick at least one of them to fire. HTH, // Todd -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Switch statement Question
Boyd, Todd M. schreef: -Original Message- From: Alice Wei [mailto:aj...@alumni.iu.edu] Sent: Thursday, January 29, 2009 3:02 PM To: php-general@lists.php.net Subject: [PHP] Switch statement Question Hi, I have a code snippet here as in the following: //Switch statements between the four options switch($string) { case : $string= NOT book.author='All'; break; default: $string= $string . AND NOT book.author='All'; break; } This code does work, but I am wondering if it is possible in the switch statement clauses for me to do something like case does not equal to a certain author name if I don't want $string with that content to be processed. or, do I always use default in this case? It's a bit non-conventional, but the switch block can be used like so: switch(true) { case (x y): dosomething(); break; case (y == 0): dosomethingelse(); break; default: somethingelseentirely(); break; } some people really don't like this kind of thing (hi Robbert :-)), either way beware that the equality test is not strict, that is to say autocasting occurs (variable type doesn't have to match) an example: switch (true) { case 1: echo did you expect this?\n; // -- this is output break; case true: echo or this?\n; break; } ...this way, your case statements can be expressions themselves, and it will always pick at least one of them to fire. HTH, // Todd -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] If statement question
On Mon, June 26, 2006 1:23 pm, Robert Cummings wrote: I can't think of any language that processes the contents of a conditional block when the test condition fails. I believe that PHP with Runkit would let you set that up to happen, if it was something you actually wanted... :-) And Common Lisp will cheerfully let you re-define NIL so that all the truth-values come out quite differently from what you expect, if you want to be intentionally obfuscating code. These are not, however, what one would consider normal operations -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] If statement question
On Mon, 26 Jun 2006 19:10:59 +0100, Alex Major [EMAIL PROTECTED] wrote: Hi list. Basically, I'm still learning new things about php and I was wondering if things inside an if statement get 'looked at' by a script if the condition is false. For example, would this mysql query get executed if $number = 0 ? If ($number == 1) { mysql_query($blah) } NO! -- - Lic. Martín Marqués | SELECT 'mmarques' || Centro de Telemática| '@' || 'unl.edu.ar'; Universidad Nacional| DBA, Programador, del Litoral | Administrador - -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] If statement question
On Mon, 2006-06-26 at 14:10, Alex Major wrote: Hi list. Basically, I'm still learning new things about php and I was wondering if things inside an if statement get 'looked at' by a script if the condition is false. For example, would this mysql query get executed if $number = 0 ? If ($number == 1) { mysql_query($blah) } I know that's not really valid php, but hope it gets my point across. I was just wondering from an optimisation perspective, as I don't want sql commands being executed when they don't need to be (unnecessary server usage). I can't think of any language that processes the contents of a conditional block when the test condition fails. 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. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] If statement question
On Monday 26 June 2006 13:10, Alex Major wrote: Hi list. Basically, I'm still learning new things about php and I was wondering if things inside an if statement get 'looked at' by a script if the condition is false. For example, would this mysql query get executed if $number = 0 ? If ($number == 1) { mysql_query($blah) } Nope. That's the definition of an if statement (conditional), in any language. If the text fails, the contents of the block never happen. -- Larry Garfield AIM: LOLG42 [EMAIL PROTECTED] ICQ: 6817012 If nature has made any one thing less susceptible than all others of exclusive property, it is the action of the thinking power called an idea, which an individual may exclusively possess as long as he keeps it to himself; but the moment it is divulged, it forces itself into the possession of every one, and the receiver cannot dispossess himself of it. -- Thomas Jefferson -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] IF statement question...
[snip] Is it possible to request that a string CONTAINS another string...? EG: $string = 1, 2, 3, 7, 8, 9; if ($string CONTAINS 7) { // Do stuff } [/snip] Almost any regex function would work here, for instance $string = 1, 2, 3, 7, 8, 9; if (preg_match(/7/, $string)){ //evaluates to true // Do stuff } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF statement question...
[EMAIL PROTECTED] wrote: Is it possible to request that a string CONTAINS another string...? EG: $string = 1, 2, 3, 7, 8, 9; if ($string CONTAINS 7) { // Do stuff } int strpos ( string haystack, string needle [, int offset]) is what you are looking for. HTH, Oliver Hankeln -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] IF statement question...
-Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Sent: 19 May 2004 12:55 Is it possible to request that a string CONTAINS another string...? EG: $string = 1, 2, 3, 7, 8, 9; if ($string CONTAINS 7) { // Do stuff } if (strpos($string, 7)!==FALSE) Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Headingley Campus, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF statement question...
Jay Blanchard wrote: [snip] Is it possible to request that a string CONTAINS another string...? EG: $string = 1, 2, 3, 7, 8, 9; if ($string CONTAINS 7) { // Do stuff } [/snip] Almost any regex function would work here, for instance $string = 1, 2, 3, 7, 8, 9; if (preg_match(/7/, $string)){ //evaluates to true // Do stuff } From php.net: Tipp: Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] IF statement question...
[snip] Tipp: Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster. [/snip] This brings up a good point. Just exactly how much faster would one be over another in this small example? How big would the string have to be to note any degredation of performance? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF statement question...
Jay Blanchard wrote: [snip] Tipp: Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster. [/snip] This brings up a good point. Just exactly how much faster would one be over another in this small example? How big would the string have to be to note any degredation of performance? I wrote a small script to test this: 10 Searches in a rather small string took 0.38s with strpos() and 0.55s with preg_match() While this is not too much in absolute time preg_match uses 145% of the strpos time. You can play with the script: ?php $haystack=This is a string. It is not really long, but I hope it is long enough for this test ;-); $needle=test; function getmicrotime() { $time=microtime(); list($usec, $sec) = explode( , $time); return ((float)$usec + (float)$sec); } $t1=getmicrotime(); for($i=0;$i10;$i++) $pos=strpos($haystack,$needle); $t2=getmicrotime(); for($i=0;$i10;$i++) preg_match(/.$needle./,$haystack); $t3=getmicrotime(); printf(Time for strpos: %1.2f sbr,$t2-$t1); printf(Time for preg_match: %1.2f sbr,$t3-$t2); ? Oliver -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF statement question...
* Thus wrote Oliver Hankeln ([EMAIL PROTECTED]): Jay Blanchard wrote: [snip] Tipp: Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster. [/snip] This brings up a good point. Just exactly how much faster would one be over another in this small example? How big would the string have to be to note any degredation of performance? I wrote a small script to test this: 10 Searches in a rather small string took 0.38s with strpos() and 0.55s with preg_match() ... $t1=getmicrotime(); for($i=0;$i10;$i++) $pos=strpos($haystack,$needle); $t2=getmicrotime(); for($i=0;$i10;$i++) preg_match(/.$needle./,$haystack); $t3=getmicrotime(); Make sure your benchmarks aren't bias: - assignment takes time - concating string takes time strpos($haystack,$needle); v.s. preg_match($regex,$haystack); Curt -- I used to think I was indecisive, but now I'm not so sure. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF statement question...
Curt Zirzow wrote: * Thus wrote Oliver Hankeln ([EMAIL PROTECTED]): 10 Searches in a rather small string took 0.38s with strpos() and 0.55s with preg_match() Make sure your benchmarks aren't bias: - assignment takes time - concating string takes time You are right. I updated the script to: [...] $regexp=/.$needle./; $t1=getmicrotime(); for($i=0;$i10;$i++) strpos($haystack,$needle); $t2=getmicrotime(); for($i=0;$i10;$i++) preg_match($regexp,$haystack); $t3=getmicrotime(); [...] Now the time is 0.33s vs. 0.46s. preg_match needs 139.4% of the strpos time Oliver -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF statement question...
If I'm being Dumb, I apologies... but When using this: $row[bands] = 1,2,3,4,5,6,7,8; $row2[id] = 7; if (strpos($row[bands], $row2[id]) != FALSE) { // do stuff } I get the No 13 (as it's inthe 13th place in the string) as my result. Now I'm aware that it should work, as it's not returning a false value... But I'm still not getting the correct output on my page... Any other ideas? Oliver Hankeln [EMAIL PROTECTED] 19/05/2004 15:49 To [EMAIL PROTECTED] cc Subject Re: [PHP] IF statement question... Curt Zirzow wrote: * Thus wrote Oliver Hankeln ([EMAIL PROTECTED]): 10 Searches in a rather small string took 0.38s with strpos() and 0.55s with preg_match() Make sure your benchmarks aren't bias: - assignment takes time - concating string takes time You are right. I updated the script to: [...] $regexp=/.$needle./; $t1=getmicrotime(); for($i=0;$i10;$i++) strpos($haystack,$needle); $t2=getmicrotime(); for($i=0;$i10;$i++) preg_match($regexp,$haystack); $t3=getmicrotime(); [...] Now the time is 0.33s vs. 0.46s. preg_match needs 139.4% of the strpos time Oliver -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php * The information contained in this e-mail message is intended only for the personal and confidential use of the recipient(s) named above. If the reader of this message is not the intended recipient or an agent responsible for delivering it to the intended recipient, you are hereby notified that you have received this document in error and that any review, dissemination, distribution, or copying of this message is strictly prohibited. If you have received this communication in error, please notify us immediately by e-mail, and delete the original message. *** -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] IF statement question...
[snip] If I'm being Dumb, I apologies... but When using this: $row[bands] = 1,2,3,4,5,6,7,8; $row2[id] = 7; if (strpos($row[bands], $row2[id]) != FALSE) { // do stuff } [/snip] What is the expected output? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF statement question...
Curt Zirzow wrote: Make sure your benchmarks aren't bias: - assignment takes time - concating string takes time strpos($haystack,$needle); v.s. preg_match($regex,$haystack); Just for shits and giggles (and because it's a slow work day), the below script output this... strpos() : 0.18918436765671 preg_match() : 0.26665662288666 ini_set ( max_execution_time, 72000 ); function getmicrotime() { list ( $usec, $sec ) = explode( , microtime() ); return ( (float)$usec + (float)$sec ); } $haystack= Bob is good.; $needle = is; $regex = /is/; $strpos_temp = 0; $preg_match_temp = 0; for ( $j = 0; $j 1; $j++ ) { $t1 = getmicrotime(); for ( $i = 0; $i 10; $i++ ) { $pos = strpos ( $haystack, $needle ); } $t2 = getmicrotime(); for ( $i = 0; $i 10; $i++ ) { preg_match ( $regex, $haystack ); } $t3 = getmicrotime(); $strpos_temp += $t2 - $t1; $preg_match_temp += $t3 - $t2; } $strpos = $strpos_temp / $j; $preg_match = $preg_match_temp / $j; echo ( strpos() : . $strpos . br /\npreg_match() : . $preg_match . br /\n ); -- John C. Nichel KegWorks.com 716.856.9675 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] IF statement question...
[snip] strpos() : 0.18918436765671 preg_match() : 0.26665662288666 [/snip] 1/3rd of a second slowerinteresting. You know what though, my original assertion is correctalmost any of the regex functions will work. Tristan, why not convert the string to an array and then use in_array()? John, can you add the array speed test to your script? I'd like to know how that stacks up too. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] IF statement question...
-Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Sent: 19 May 2004 15:47 If I'm being Dumb, I apologies... but When using this: $row[bands] = 1,2,3,4,5,6,7,8; $row2[id] = 7; if (strpos($row[bands], $row2[id]) != FALSE) { // do stuff } I get the No 13 (as it's inthe 13th place in the string) as my result. Now I'm aware that it should work, as it's not returning a false value... But I'm still not getting the correct output on my page... (i) needs to be !== not != (since the substring might appear in position 0 and 0==FALSE). (ii) post some lines of your actual code showing what you expect, and what you actually get. (iii) to know exactly what we're dealing with, var_dump $row and $row2 before you do the strpos, and cut'n'paste the results for us. (iv) also, quote your string subscripts ($row['bands']) -- not vital, but suppresses a constant-lookup and notice for each one, and hence is more efficient. Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Headingley Campus, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] IF statement question...
Jay Blanchard wrote: [snip] strpos() : 0.18918436765671 preg_match() : 0.26665662288666 [/snip] 1/3rd of a second slowerinteresting. You know what though, my original assertion is correctalmost any of the regex functions will work. Personally, I prefer using preg_match() 9 times out of 10 vs. strpos(), strstr(), etc. Whatever performance penalty I pay for doing this is a statistical drop in the bucket compared to the overhead of, say, opening a connection to a database (which nearly all of my pages do). I'm also a perl guy so regex's are more natural to me to read and write so I tend to favor them... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF statement question...
Jay Blanchard wrote: [snip] strpos() : 0.18918436765671 preg_match() : 0.26665662288666 [/snip] 1/3rd of a second slowerinteresting. You know what though, my original assertion is correctalmost any of the regex functions will work. Tristan, why not convert the string to an array and then use in_array()? John, can you add the array speed test to your script? I'd like to know how that stacks up too. Okay, below is the new script...test will take a few to run. I'm doing the explode of the haystack outside of the test, so it's only going to test the speed of in_array(), and not take into account how long it would take to create the array from a string. I'll post the result in a few. ini_set ( max_execution_time, 72000 ); function getmicrotime() { list ( $usec, $sec ) = explode( , microtime() ); return ( (float)$usec + (float)$sec ); } $haystack= Bob is good.; $needle = is; $regex = /is/; $array = explode ( , $haystack ); $strpos_temp = 0; $preg_match_temp = 0; $in_array_temp = 0; for ( $j = 0; $j 1; $j++ ) { $t1 = getmicrotime(); for ( $i = 0; $i 10; $i++ ) { $pos = strpos ( $haystack, $needle ); } $t2 = getmicrotime(); for ( $i = 0; $i 10; $i++ ) { preg_match ( $regex, $haystack ); } $t3 = getmicrotime(); for ( $i = 0; $i 10; $i++ ) { in_array ( $needle, $array ); } $t4 = getmicrotime(); $strpos_temp += $t2 - $t1; $preg_match_temp += $t3 - $t2; $in_array_temp += $t4 - $t3; } $strpos = $strpos_temp / $j; $preg_match = $preg_match_temp / $j; $in_array = $in_array_temp / $j; echo ( strpos() : . $strpos . br /\npreg_match() : . $preg_match . br /\nin_array() : . $in_array ); -- John C. Nichel KegWorks.com 716.856.9675 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF statement question...
John Nichel wrote: Jay Blanchard wrote: [snip] strpos() : 0.18918436765671 preg_match() : 0.26665662288666 [/snip] 1/3rd of a second slowerinteresting. You know what though, my original assertion is correctalmost any of the regex functions will work. Tristan, why not convert the string to an array and then use in_array()? John, can you add the array speed test to your script? I'd like to know how that stacks up too. Okay, below is the new script...test will take a few to run. I'm doing the explode of the haystack outside of the test, so it's only going to test the speed of in_array(), and not take into account how long it would take to create the array from a string. I'll post the result in a few. Results : strpos() : 0.19018315076828 preg_match() : 0.26157474517822 in_array() : 0.26403407096863 -- John C. Nichel KegWorks.com 716.856.9675 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] IF statement question...
[snip] strpos() : 0.19018315076828 preg_match() : 0.26157474517822 in_array() : 0.26403407096863 [/snip] Very interesting...thanks! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF statement question...
* Thus wrote Jay Blanchard ([EMAIL PROTECTED]): [snip] strpos() : 0.19018315076828 preg_match() : 0.26157474517822 in_array() : 0.26403407096863 [/snip] Very interesting...thanks! So if we're not going to search though a string 10,000 times you'll save ~0.07139159440994 seconds. or am I missing something? Curt -- I used to think I was indecisive, but now I'm not so sure. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] switch statement question
[snip] Say i have a variable $string_var = Once upon a time; is there a way to do this: Switch($string_var) { case(contains Once): doSomething(); break; case(contains the end.) doOtherThing(); break; case(contains time) doNothing(); break; default: echo ERROR } [/snip] http://www.php.net/manual/en/control-structures.switch.php Yes Virginia, there is a PHP function :^]. You can explode your $string_var into an array, loop through the array and test each word for a match. HTH! Jay -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch statement question
Alternatively Alexander, take a look at the string functions eg strstr(), stristr(), and strpos() all of which can be used to make a condition within the case() criteria. Regards, =dn [snip] Say i have a variable $string_var = Once upon a time; is there a way to do this: Switch($string_var) { case(contains Once): doSomething(); break; case(contains the end.) doOtherThing(); break; case(contains time) doNothing(); break; default: echo ERROR } [/snip] http://www.php.net/manual/en/control-structures.switch.php Yes Virginia, there is a PHP function :^]. You can explode your $string_var into an array, loop through the array and test each word for a match. HTH! Jay -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] selcet statement question
[snip] I know that i can retrieve records 1-24 in my db with a select statement like SELECT whatever FROM tablename ORDER BY id ASC LIMIT 23, but how can I get records 25 to 50 if I have 100 records in my DB? [/snip] Different DB's have differing ways of doing this, like MySQL; SELECT * FROM foo ORDER BY id ASC LIMIT 0, 24 ** first 25 records SELECT * FROM foo ORDER BY id ASC LIMIT 25, 24 * next 25 records Don't forget that the row numbering starts with 0. HTH! Jay Oh, sure but whats the speed of DARK? * * Want to meet other PHP developers * * in your area? Check out: * * http://php.meetup.com/* * No developer is an island ... * * -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php