Re: [PHP] Function-return-array idea
On Jan 16, 2008 6:32 PM, Stijn Leenknegt [EMAIL PROTECTED] wrote: Hello I've an idea for PHP6. Let's kickoff with an example. ?php $info = getUserInformation($id); //return an array with all the information of an user. echo $info['naam']; ? This is nice, but when I want one element of the returned array, I have to store the returned array into a variable and then call the variable. The next code example is my idea. ?php echo getUserInformation($id)['naam']; ? Let's look further then this small example. ?php echo $object-fetchObjects()[0]-method(); ? This example is more realistic when you use OO. This code style is faster for developing applications. This code style is available in Java, Javascript, C, C++, Perl, Python, ... So why not in PHP(6)? You can read more on my blog i think this is pretty much a dup of a thread from about a week ago; http://www.nabble.com/Why-is-some_function()-some_index--invalid-syntax--td14749459.html -nathan
Re: [PHP] Function-return-array idea
On Jan 16, 2008 6:32 PM, Stijn Leenknegt [EMAIL PROTECTED] wrote: Hello I've an idea for PHP6. Let's kickoff with an example. ?php $info = getUserInformation($id); //return an array with all the information of an user. echo $info['naam']; ? This is nice, but when I want one element of the returned array, I have to store the returned array into a variable and then call the variable. The next code example is my idea. ?php echo getUserInformation($id)['naam']; ? Let's look further then this small example. ?php echo $object-fetchObjects()[0]-method(); ? This example is more realistic when you use OO. This code style is faster for developing applications. This code style is available in Java, Javascript, C, C++, Perl, Python, ... So why not in PHP(6)? You can read more on my blog. http://www.eos-team.org/2007/09/06/php6-function-return-array-idea.html I hope for good response and also response from php-dev team. Greetings Stijn Leenknegt My reply is a bit pointless as I'd never do/rely on this. But nonetheless it was an interesting test. function testObj() { $arr = array(); $arr['key1'] = array(1, 2, 3); $arr['key3'] = array('a','b','c'); return (object)$arr; } echo 'pre'; echo Key1: \n; print_r(testObj()-key1); echo Key3: \n; print_r(testObj()-key3); --- output Key1: Array ( [0] = 1 [1] = 2 [2] = 3 ) Key3: Array ( [0] = a [1] = b [2] = c ) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Function-return-array idea
Jim Lucas wrote: I think this would be an easier/quicker fix for you then requesting that the PHP developers re-write a large portion of the way PHP currently works. Here is the reference that I have been looking for. http://en.wikipedia.org/wiki/Message_passing Jim -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Function-return-array idea
On Wed, January 16, 2008 5:32 pm, Stijn Leenknegt wrote: I've an idea for PHP6. Let's kickoff with an example. This belongs on php-internals... ?php echo getUserInformation($id)['naam']; ? where it has already been discussed at length, and, as I recall, rejected as too obfuscated for the intent of PHP. I apologize to php-internals for my gross over-simplification of a zillion posts. :-) Read the php-internals archives for the gory details. -- Some people have a gift link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Function-return-array idea
I think this would be an easier/quicker fix for you then requesting that the PHP developers re-write a large portion of the way PHP currently works. ?php function i($arr, $i) { return $arr[$i]; } echo i(getUserInformation($id), 'naam'); #or echo i($object-fetchObjects(), 0)-method(); ? Stijn Leenknegt wrote: Hello I've an idea for PHP6. Let's kickoff with an example. ?php $info = getUserInformation($id); //return an array with all the information of an user. echo $info['naam']; ? This is nice, but when I want one element of the returned array, I have to store the returned array into a variable and then call the variable. The next code example is my idea. ?php echo getUserInformation($id)['naam']; ? Let's look further then this small example. ?php echo $object-fetchObjects()[0]-method(); ? This example is more realistic when you use OO. This code style is faster for developing applications. This code style is available in Java, Javascript, C, C++, Perl, Python, ... So why not in PHP(6)? You can read more on my blog. http://www.eos-team.org/2007/09/06/php6-function-return-array-idea.html I hope for good response and also response from php-dev team. Greetings Stijn Leenknegt -- Jim Lucas Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them. Twelfth Night, Act II, Scene V by William Shakespeare -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Function-return-array idea
Stijn Leenknegt wrote: This is nice, but when I want one element of the returned array, I have to store the returned array into a variable and then call the variable. The next code example is my idea. ?php echo getUserInformation($id)['naam']; ? Let's look further then this small example. ?php echo $object-fetchObjects()[0]-method(); ? Yeah, I like the idea. I've often used that style out of habit, only to have to revert to assigning the result to a variable etc. I don't know why it isn't supported by PHP, but I suspect it is related to the type-concept. /Per Jessen, Zürich -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Function return
On 11/2/07, Dan Shirah [EMAIL PROTECTED] wrote: TGIF!! I believe that's Copyright (C) TCompuserve. Careful you don't get tsued. -- Daniel P. Brown [office] (570-) 587-7080 Ext. 272 [mobile] (570-) 766-8107 Give a man a fish, he'll eat for a day. Then you'll find out he was allergic and is hospitalized. See? No good deed goes unpunished -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Function return
On 11/2/07, Stut [EMAIL PROTECTED] wrote: Dan Shirah wrote: That is correct, the due_date field should only accept a valid date format, such as MM/DD/. To bypass the need for a validation check for this field I simply set the text field to disabled and supplied the user with a javascript popup calendar that upon selection populates the date in the format I want. :) Client-side limits are not an effective defense against dangerous inputs. Server-side validation is a must regardless of any client-side checking that goes on. this is an excellent point; consider the scenario where someone posts directly to your page, bypasing the client side checks. this is a trival technique; inspect the form attributes; determine the submission url and the submission method. look at the name attributes of inputs on the page; and then build a simple form that posts those inputs to the same url with the same method. -nathan
Re: [PHP] Function return
Dan Shirah wrote: That is correct, the due_date field should only accept a valid date format, such as MM/DD/. To bypass the need for a validation check for this field I simply set the text field to disabled and supplied the user with a javascript popup calendar that upon selection populates the date in the format I want. :) Client-side limits are not an effective defense against dangerous inputs. Server-side validation is a must regardless of any client-side checking that goes on. -Stut -- http://stut.net/ On 11/2/07, Nathan Nobbe [EMAIL PROTECTED] wrote: On 11/2/07, Dan Shirah [EMAIL PROTECTED] wrote: Ah, okay. So I could probably simplfy it more by trimming it from the start like this?? $due_date = trim($_POST['due_date']); that works; i personally prefer to initialize a variable then only set it if the user input meets some conditions; its called white-box validation. $due_date = ''; if(isset($_POST['due_date'])) !empty($POST['due_date'])) { $due_date = trim($_POST['due_date']); } the more you know about what the contents of due_date are supposed to be, the stronger you can make the check; for instance here, it sounds like it should be a date so you wouldnt allow, say 'somecrazySting', to pass the validation. -nathan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Function return
On 11/2/07, Dan Shirah [EMAIL PROTECTED] wrote: Ah, okay. So I could probably simplfy it more by trimming it from the start like this?? $due_date = trim($_POST['due_date']); that works; i personally prefer to initialize a variable then only set it if the user input meets some conditions; its called white-box validation. $due_date = ''; if(isset($_POST['due_date'])) !empty($POST['due_date'])) { $due_date = trim($_POST['due_date']); } the more you know about what the contents of due_date are supposed to be, the stronger you can make the check; for instance here, it sounds like it should be a date so you wouldnt allow, say 'somecrazySting', to pass the validation. -nathan
Re: [PHP] Function return
On 11/2/07, Dan Shirah [EMAIL PROTECTED] wrote: Okay, so instead I should probably use: if($due_date != ) $insert2.=, due_date='$due_date'; Instead of using empty() using emtpy is fine; just store the results of trim in a temporary variable: $trimmed = trim($due_date); if(!empty($trimmed)) { $insert2 .= , due_date='$due_date'; } -nathan
Re: [PHP] Function return
Okay, so instead I should probably use: if($due_date != ) $insert2.=, due_date='$due_date'; Instead of using empty() On 11/2/07, Nathan Nobbe [EMAIL PROTECTED] wrote: On 11/2/07, Dan Shirah [EMAIL PROTECTED] wrote: TGIF!! I have an insert statement that checks to see if a condition is met. If it is, then it adds that value to the insert statement. However, when I try to run it I get the error: Can't use function return value in write context Below is a sample of what I am using which gives me the error(The error occurs while trying to insert the due_date) $insert2 = INSERT INTO misc_service_payment_request SET; if(empty($_POST['max_id'])) { $insert2.= id = scope_identity(),; } else { $insert2.= id = '$max_id',; } if(!empty(trim('$due_date')) $insert2.=, due_date='$due_date'; mssql_query($insert2) or die (Query failed: br /.mssql_get_last_message()); Any ideas? dont call trim() as an argument to empty(); it doesnt work that way. see the manual http://us.php.net/manual/en/function.empty.php: Note: empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)). -nathan
Re: [PHP] Function return
On 11/2/07, Dan Shirah [EMAIL PROTECTED] wrote: TGIF!! I have an insert statement that checks to see if a condition is met. If it is, then it adds that value to the insert statement. However, when I try to run it I get the error: Can't use function return value in write context Below is a sample of what I am using which gives me the error(The error occurs while trying to insert the due_date) $insert2 = INSERT INTO misc_service_payment_request SET; if(empty($_POST['max_id'])) { $insert2.= id = scope_identity(),; } else { $insert2.= id = '$max_id',; } if(!empty(trim('$due_date')) $insert2.=, due_date='$due_date'; mssql_query($insert2) or die (Query failed: br /.mssql_get_last_message()); Any ideas? dont call trim() as an argument to empty(); it doesnt work that way. see the manual http://us.php.net/manual/en/function.empty.php: Note: empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)). -nathan
Re: [PHP] Function return
Ah, okay. So I could probably simplfy it more by trimming it from the start like this?? $due_date = trim($_POST['due_date']); On 11/2/07, Nathan Nobbe [EMAIL PROTECTED] wrote: On 11/2/07, Dan Shirah [EMAIL PROTECTED] wrote: Okay, so instead I should probably use: if($due_date != ) $insert2.=, due_date='$due_date'; Instead of using empty() using emtpy is fine; just store the results of trim in a temporary variable: $trimmed = trim($due_date); if(!empty($trimmed)) { $insert2 .= , due_date='$due_date'; } -nathan
Re: [PHP] Function return
That is correct, the due_date field should only accept a valid date format, such as MM/DD/. To bypass the need for a validation check for this field I simply set the text field to disabled and supplied the user with a javascript popup calendar that upon selection populates the date in the format I want. :) On 11/2/07, Nathan Nobbe [EMAIL PROTECTED] wrote: On 11/2/07, Dan Shirah [EMAIL PROTECTED] wrote: Ah, okay. So I could probably simplfy it more by trimming it from the start like this?? $due_date = trim($_POST['due_date']); that works; i personally prefer to initialize a variable then only set it if the user input meets some conditions; its called white-box validation. $due_date = ''; if(isset($_POST['due_date'])) !empty($POST['due_date'])) { $due_date = trim($_POST['due_date']); } the more you know about what the contents of due_date are supposed to be, the stronger you can make the check; for instance here, it sounds like it should be a date so you wouldnt allow, say 'somecrazySting', to pass the validation. -nathan
Re: [PHP] Function return
I know that while you should be doing proper validation on the server side and everything, you could somewhat diminish the risk of someone building their own form and just posting. If you were to check the referring site which made the post then you could be pretty sure someone didn't make their own form. Although this too is insecure, since all you need is a Firefox extension, firebug or grease monkey which let you execute JavaScript could let you change the page however you want while it's live, the refer would be proper, etc. What I'm really saying is there is NO secure way to validate client side, none, absolutely none. Check that the date coming in is a valid date then create a date object with it, that way even if they somehow do slip past your date validation then the object just won't be created correctly. There's lots to validate. - Dan Nathan Nobbe [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] On 11/2/07, Stut [EMAIL PROTECTED] wrote: Dan Shirah wrote: That is correct, the due_date field should only accept a valid date format, such as MM/DD/. To bypass the need for a validation check for this field I simply set the text field to disabled and supplied the user with a javascript popup calendar that upon selection populates the date in the format I want. :) Client-side limits are not an effective defense against dangerous inputs. Server-side validation is a must regardless of any client-side checking that goes on. this is an excellent point; consider the scenario where someone posts directly to your page, bypasing the client side checks. this is a trival technique; inspect the form attributes; determine the submission url and the submission method. look at the name attributes of inputs on the page; and then build a simple form that posts those inputs to the same url with the same method. -nathan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Function: return multple values
Hi Faisal, Is it possible to return multiple values in a function. For instance, i want to do something like this: function calculate_money($sum) { // some hanky panky calculations return $type; return $amount; } You have to return the values in an array, and use list() to break them up. Try this: function calculate_money($sum) { // do stuff return array($type, $amount); } list($type, $amount) = calculate_money($whatever); Cheers Jon -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] function return
Hi, I don't know if there is an easier way to do this but why don't you concatenate the two values to be returned and then split the returned value ? ex : function myFunction($some_vars) { $max=count($some_vars); $some_value=($max/2); $result = $some_value.;.$max; return $result; } $resultado = myFunction($some_other_name_vars); list($var1,$var2) = split(;,$resultado); I hope it helps. SR $var1=should be the value of $some_value $var2=should be the value of $max W. Enserink a écrit : Hi all, I have a Q about functions. My function should return 2 values. What syntax should I use and how do I call these values outside the function? Are these values returned as en array or something? this is what I have now, - function myFunction($some_vars) { $max=count($some_vars); $some_value=($max/2); return($some_value $max); } $var1=should be the value of $some_value $var2=should be the value of $max thx. Wilbert - Pas de Deux Van Mierisstraat 25 2526 NM Den Haag tel 070 4450855 fax 070 4450852 http://www.pdd.nl [EMAIL PROTECTED] - -- 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] function return
I have a Q about functions. My function should return 2 values. What syntax should I use and how do I call these values outside the function? Are these values returned as en array or something? I don't know if there is an easier way to do this but why don't you concatenate the two values to be returned and then split the returned value ? ex : function myFunction($some_vars) { $max=count($some_vars); Personally, I'd put both values to be returned into an array and then return the array. CYA, Dave -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] function return
Hi, function myFunction($some_vars, $max, $some_value) { $max=count($some_vars); $some_value=($max/2); //return true; } Rudolf Visagie Principal Software Developer Digital Healthcare Solutions mailto:[EMAIL PROTECTED] Tel: + 27 011 2655478 Cell: + 27 82 895 1598 -Original Message- From: W. Enserink [mailto:[EMAIL PROTECTED]] Sent: Tuesday, May 28, 2002 10:02 AM To: [EMAIL PROTECTED] Subject: [PHP] function return Hi all, I have a Q about functions. My function should return 2 values. What syntax should I use and how do I call these values outside the function? Are these values returned as en array or something? this is what I have now, - function myFunction($some_vars) { $max=count($some_vars); $some_value=($max/2); return($some_value $max); } $var1=should be the value of $some_value $var2=should be the value of $max thx. Wilbert - Pas de Deux Van Mierisstraat 25 2526 NM Den Haag tel 070 4450855 fax 070 4450852 http://www.pdd.nl [EMAIL PROTECTED] - -- 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] function return
Return an array function whatever($var1, $var2) { // do whatever $ret[0] = $var1 + $var2; $ret[1] = $var1 - $var2; $ret['something'] = ($var1/$var2) * 100; return $ret; } $value = whatever(5,4); echo $value[0]; echo $value['something']; ---John Holmes... - Original Message - From: W. Enserink [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Tuesday, May 28, 2002 4:01 AM Subject: [PHP] function return Hi all, I have a Q about functions. My function should return 2 values. What syntax should I use and how do I call these values outside the function? Are these values returned as en array or something? this is what I have now, - function myFunction($some_vars) { $max=count($some_vars); $some_value=($max/2); return($some_value $max); } $var1=should be the value of $some_value $var2=should be the value of $max thx. Wilbert - Pas de Deux Van Mierisstraat 25 2526 NM Den Haag tel 070 4450855 fax 070 4450852 http://www.pdd.nl [EMAIL PROTECTED] - -- 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] function return
On Tue, 28 May 2002, W. Enserink wrote: I have a Q about functions. My function should return 2 values. What syntax should I use and how do I call these values outside the function? Are these values returned as en array or something? this is what I have now, - function myFunction($some_vars) { $max=count($some_vars); $some_value=($max/2); return($some_value $max); } function myFunction ($some_vars) { $max = count($some_vars); $some_value = ($max / 2); return array($some_value, $max); } list($x, $y) = myFunction($_SERVER); echo returned $x and $y; miguel -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] function return
should work(works for me).probly something simple. ?php function conn(){ $db = mysql_pconnect("localhost", "" ,""); mysql_select_db("database",$db); return $db; } $db = conn(); $result= mysql_query("select * from table",$db); while($myrow=MySQL_fetch_array($result)) { $thing=$myrow["item"]; echo "$thing br "; } ? - Original Message - From: Kurth Bemis [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Friday, April 06, 2001 4:10 PM Subject: [PHP] function return i'm trying to make all my include files into functions for manageability. I have one function that i'd like to get to work.heres teh code.. ?php $db = mysql_pconnect("localhost", "user" ,"pass"); mysql_select_db("database",$db); ? i had it in a function and had the function return $db.but didn't work.any ideas? ~kurth -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] function return
I usually declare $db as global in the function, so I can just execute the function and continue with my querys. -- Plutarck Should be working on something... ...but forgot what it was. "Kurth Bemis" [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... i'm trying to make all my include files into functions for manageability. I have one function that i'd like to get to work.heres teh code.. ?php $db = mysql_pconnect("localhost", "user" ,"pass"); mysql_select_db("database",$db); ? i had it in a function and had the function return $db.but didn't work.any ideas? ~kurth -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]