{"id":3341,"date":"2014-03-23T02:29:04","date_gmt":"2014-03-23T02:29:04","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/23\/phpanonymous-functionrelated-issues-collection-of-common-programming-errors\/"},"modified":"2014-03-23T02:29:04","modified_gmt":"2014-03-23T02:29:04","slug":"phpanonymous-functionrelated-issues-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/23\/phpanonymous-functionrelated-issues-collection-of-common-programming-errors\/","title":{"rendered":"php,anonymous-functionRelated issues-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/0df2f3c2d3b43605889eb90231fb6884?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nMatt Ball<br \/>\nphp computer-algebra-systems<br \/>\nI&#8217;m creating a CAS (Computer Algebra System) in PHP, but I&#8217;m stuck right now. I am using this website.Now I wrote a tokenizer. It will convert an equation like this:1+2x-3*(4-5*(3x))to this:NUMBER PLUS_OPERATOR NUMBER VAR[X] MINUS_OPERATOR NUMBER MULTIPLY_OPERATOR GROUP(where group is another set of tokens). How can I simplify this equation? Yeah, I know what you can do: adding X-vars, but they are in the sub-group. What is the best method I can use for handling those tokens?<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/336522ad5fe28d7dd03c490b4677bbf1?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\niainjames88<br \/>\nphp ubuntu-12.04 error-reporting internal-server-error<br \/>\nI&#8217;ve set up a fresh install of Ubuntu Server 12.04 LTS on Amazon AWS with *Apache2\/MySQL\/PHP5. When I run a PHP script and it encounters an error I don&#8217;t see any error reporting from PHP, all I see isHTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfil the request.I have checked my \/etc\/php5\/apache2\/php.ini file and as far as I can tell error reporting should be set up. The contents of the file (regarding errors) are:; displ<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/b6116ac3c4b5835c6745570ea73a85c1?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nCharles<br \/>\nphp oop<br \/>\nUsing PHP 5.3 I&#8217;m experiencing weird \/ non-intuitive behavior when applying empty() to dynamic object properties fetched via the __get() overload function. Consider the following code snippet:&lt;?phpclass Test {protected $_data= array(&#8216;id&#8217;=&gt; 23,&#8217;name&#8217;=&gt; &#8216;my string&#8217;);function __get($k) {return $this-&gt;_data[$k];}}$test= new Test(); var_dump(&#8220;Accessing directly:&#8221;); var_dump($test-&gt;name); var_dump($test-&gt;id); var_dump(empty($test-&gt;name)); var_dump(empty($test-&gt;id));var_dump(&#8220;Ac<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/ccc83a013527c186c03ea3a475552813?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\ncodaddict<br \/>\nphp operators ternary-operator<br \/>\nThis is what I wrote :$Myprovince = ( ($province == 6) ? &#8220;city-1&#8221; : ($province == 7) ? &#8220;city-2&#8221; : ($province == 8) ? &#8220;city-3&#8221; : ($province == 30) ? &#8220;city-4&#8221; : &#8220;out of borders&#8221; );But for every field I got the value city-4. I want to use ternary operators instead of switch\/if because I want to experiment and see how it would be done.What&#8217;s the problem with this code?<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/28da63e58ce7fa428494292973747c84?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nJess Telford<br \/>\nphp unit-testing phpunit<br \/>\nWhen running a PHPUnit test, I would like to be able to dump output so I can debug one or two things.I have tried the following (similar to the PHPUnit Manual example);class theTest extends PHPUnit_Framework_TestCase {\/*** @outputBuffering disabled*\/public function testOutput() {print_r(&#8220;Hello World&#8221;);print &#8220;Ping&#8221;;echo &#8220;Pong&#8221;;$out = &#8220;Foo&#8221;;var_dump($out);} }With the following result:PHPUnit @package_version@ by Sebastian Bergmann..Time: 0 seconds, Memory: 3.00MbOK (1 test, 0 assertions)Notice<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/c9080d169b52844739b7486ee71029c4?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nuser456885<br \/>\nphp multibyte<br \/>\nGiven certain multibyte character sets, am I correct in assuming that the following doesn&#8217;t do what it was intended to do?$string = str_replace(&#8216;&#8221;&#8216;, &#8216;\\\\&#8221;&#8216;, $string);In particular, if the input was in a character set that might have a valid character like 0xbf5c, so an attacker can inject 0xbf22 to get 0xbf5c22, leaving a valid character followed by an unquoted double quote (&#8220;).Is there an easy way to mitigate this problem, or am I misunderstanding the issue in the first place?(In my case, the st<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/d613b3ce5047befed24bc48163492084?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\ntestwork<br \/>\nphp facebook facebook-graph-api facebook-php-sdk<br \/>\nI have a problem with posting image in more than one facebook pages. I am creating an image using GD Library.I am using facebook api to post in the pages with pageid and page access token. When a submit button is clicked for the first time the image is not uploaded and gives the error &#8211; **OAuthException: An unexpected error has occurred. Please retry your request later.Fatal error: Uncaught OAuthException: An unexpected error has occurred. Please retry your request later thrown in \/sdk\/base_fac<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/efhgN.png?s=32&amp;g=1\" \/><br \/>\napaul34208<br \/>\nphp class codeigniter<br \/>\nBasically I have a helper file with several function. I auto load the helper in the config file so theoretically reloading it is not required.However when I try to use the function that I created(from this helper) within a new library that I am working on it will through this error:&#8221;Parse error: syntax error, unexpected &#8216;(&#8216;, expecting &#8216;,&#8217; or &#8216;;&#8217; in \/home\/techwork\/public_html\/giverhub\/application\/libraries\/Udetails.php on line 7&#8243;Whenever I use that function anywhere else(module,controller,views)<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/b3616645c168b0b8a65e109a6b1cb010?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nkhellang<br \/>\nphp javascript jquery ajax<br \/>\nBasically I am trying to post a comment via AJAX and then load the new comment into the HTML. I am having a problem with what I think is my AJAX success function. The data gets added to the database fine, but I still get an error &#8220;Sorry, unexpected error. Please try again later.&#8221; as set inside the success function itself.JS:function newComment(event) { event.preventDefault();\/\/Get the data from all the fieldsvar comment = $(&#8216;textarea&#8217;);var product_id = $(&#8216;input[name=product_id]&#8217;);var form<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/8e369643869d251ab5402ed28013931f?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nZack Morris<br \/>\nphp bash cli shell-exec suppress<br \/>\nIf you create a simple php script with this code:shell_exec(&#8216;&#8221;&#8216;);And run it with:php myscript.phpIt gives the following error in bash on my Mac:sh: -c: line 0: unexpected EOF while looking for matching `&#8221;&#8216; sh: -c: line 1: syntax error: unexpected end of fileI&#8217;ve tried everything I can think of:ob_start(); @shell_exec(&#8216;&#8221;&#8216;); ob_end_clean();@shell_exec(&#8216;&#8221; 2&gt; \/dev\/null&#8217;);But no matter what I try, I can&#8217;t suppress the message. The problem is that I&#8217;m creating a unit test to stress test $argc, $ar<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/f81dccf3d7e2f895329949bfefed2e32?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nJeff Hines<br \/>\nphp eval anonymous-function preg-replace-callback<br \/>\nI&#8217;m getting this error, but I cannot find out why:Parse error: syntax error, unexpected $end in C:\\wamp\\www\\test.php(19) : eval()&#8217;d code on line 1Any insight would be appreciated!$table = &#8216;&lt;table&gt;&lt;tr&gt;&lt;td&gt;${Q*10}&lt;\/td&gt;&lt;\/tr&gt;&lt;\/table&gt;&#8217;;$symbols = array(&#8216;Q&#8217; =&gt; 10);preg_replace_callback(&#8216;\/\\${(\\w+)([*+-\\\/])(\\d+)}\/&#8217;, function($matches) use ($symbols, $table) {return repl($matches, $symbols, $table); }, $table);function repl($tokens, $symbols, $table) {$replace = ar<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/ab4fac5b921c3f2eb37db87f0acf31dc?s=32&amp;d=identicon&amp;r=PG&amp;f=1\" \/><br \/>\nChemBlob9999<br \/>\njavascript ajax performance anonymous-function<br \/>\nThis question already has an answer here:Does it matter which equals operator (== vs ===) I use in JavaScript comparisons?24 answersMy site has several ajax functions essentially identical (except different variable names, etc.) to the following:function ajaxFunction(string) { var ajaxRequest;try {ajaxRequest = new XMLHttpRequest();} catch (e) {try {ajaxRequest = new ActiveXObject(&#8220;Msxml2.XMLHTTP&#8221;);} catch (e) {try {ajaxRequest = new ActiveXObject(&#8220;Microsoft.XMLHTTP&#8221;);} catch (e) { alert(&#8220;Your<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/6f9d967832644f9faa431e4e50b69bb0?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nCodemonkey<br \/>\nphp anonymous-function<br \/>\nPossible Duplicate:Workaround for basic syntax not being parsedWhy dont PHP attributes allow functions? I&#8217;m gettingParse error: syntax error, unexpected T_FUNCTION in \/home\/codemonkey\/dev\/broadnet\/bito.api2\/broadnet\/resources\/broadmapresource.php on line 87From this code:private $debugFunctions = array(&#8220;testing&#8221; =&gt; function() \/\/ Line 87{return &#8220;I&#8217;m a test function!&#8221;;},&#8221;foo&#8221; =&gt; function(){return &#8220;bar&#8221;;} );I was under the impression I could use anonymous PHP functions anywhere I could use $<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/5326707b2554a7de78429c540a841d32?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nrybosome<br \/>\nphp closures anonymous-function reduce<br \/>\nI&#8217;m trying to execute the following PHP code:$path_hierarchy = \/\/ function that returns an arrayreturn array_reduce($terms,function($val1, $val2) use ($path_hierarchy) {return $val1 || in_array($val2, $path_hierarchy);} );&#8230;but I&#8217;m getting the following PHP error:PHP Parse error: syntax error, unexpected &#8216;)&#8217;, expecting &#8216;{&#8216;So, I switched to the following syntax:$path_hierarchy = \/\/ function that returns an array$callback = function($val1, $val2) use ($path_hierarchy) {return $val1 || in_array($<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/MGhY2.png?s=32&amp;g=1\" \/><br \/>\nhakre<br \/>\nphp anonymous-function<br \/>\nI am trying to realize my own MVC framework and invented a very nice way to provide definitions of virtual fields and additional relations.According to some other high-voted post on stackoverflow, this should actually work:class User extends Model {public $hasOne = array(&#8216;UserSetting&#8217;);public $validate = array();public $virtualFields = array(&#8216;fullname&#8217; =&gt; function () {return $this-&gt;fname . ($this-&gt;mname ? &#8216; &#8216; . $this-&gt;mname : &#8221;) . &#8216; &#8216; . $this-&gt;lname;},&#8217;official_fullname&#8217; =&gt; fu<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/773d1a28df0344248b75ba9744ad02f9?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nOtiel<br \/>\nc# linq foreach extension-methods anonymous-function<br \/>\nI ran into what was to me an unexpected result when testing a simple ForEach extension method.ForEach methodpublic static void ForEach&lt;T&gt;(this IEnumerable&lt;T&gt; list, Action&lt;T&gt; action) {if (action == null) throw new ArgumentNullException(&#8220;action&#8221;);foreach (T element in list){action(element);} }Test method[TestMethod] public void BasicForEachTest() {int[] numbers = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };numbers.ForEach(num =&gt;{num = 0;});Assert.AreEqual(0, numbers.Sum()); }Wh<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/a2d818d801ce38a33807f68fdd92043a?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nBoltClock<br \/>\njavascript parsing syntax grammar anonymous-function<br \/>\nIf I try to execute a script whose only source line is an object:{prop:&#8217;value&#8217;}it parses fine (in both V8 and UglifyJS). Similarly I can put a string or number on its own as source code and there is no syntax error reported.However, both V8 and UglifyJS complain about this on its own:function(){}I get Uncaught SyntaxError: Unexpected token (.Why does this break when the object in the first example is fine? Aren&#8217;t functions just objects in javascript?I realise declaring an anonymous function with<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/51f8953a62f6a26545d474a34e367980?s=32&amp;d=identicon&amp;r=PG&amp;f=1\" \/><br \/>\npsionicgames<br \/>\njavascript angularjs anonymous-function<br \/>\nI am using angularjs and would like to pass an anonymous javascript function from an html element&#8217;s ng-click function as a parameter to a function on my scope. I have setup up a simple jsfiddle to demonstrate the problem: http:\/\/jsfiddle.net\/C4t4LystX\/gUj8x\/4\/. Whenever I try and do this in my code I get this type of error in the console: Syntax Error: Token &#8216;{&#8216; is unexpected, expecting [)] Any help or insight on how this can be accomplished would be appreciated.<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/EuMum.jpg?s=32&amp;g=1\" \/><br \/>\nDan Lugg<br \/>\nphp types closures anonymous-function<br \/>\nTo quote PHP:Anonymous functions are currently implemented using the Closure class. This is an implementation detail and should not be relied upon.Now, that said, the following checks are deemed unreliable:function myFunction(Closure $callback){}if(!($callback instanceof Closure)){}Which brings us to using is_callable(). This is fine, however if one requires a true &#8220;closure&#8221;, (as an argument, or what-such) then is_callable() isn&#8217;t strict enough. The following of course dumps bool(true) for each:<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/e2fe4151d54a4839817a5f4229762ecc?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nash<br \/>\nphp anonymous-function<br \/>\nI&#8217;m trying to use array_walk with an anonymous function, but I always get the error \/\/ Parse error: syntax error, unexpected T_FUNCTION in &#8230; on line Xif(!empty($myArray)) {array_walk($myArray, function(&amp;$value, $key){ \/\/ Line X$value = &#8216;&#8221;&#8216;.$value.'&#8221;&#8216;; \/\/ Add quotes});}The surrounding file syntax is correct. Any thoughts?<\/li>\n<\/ul>\n<p>Web site is in building<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Matt Ball php computer-algebra-systems I&#8217;m creating a CAS (Computer Algebra System) in PHP, but I&#8217;m stuck right now. I am using this website.Now I wrote a tokenizer. It will convert an equation like this:1+2x-3*(4-5*(3x))to this:NUMBER PLUS_OPERATOR NUMBER VAR[X] MINUS_OPERATOR NUMBER MULTIPLY_OPERATOR GROUP(where group is another set of tokens). How can I simplify this equation? Yeah, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3341","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3341","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3341"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3341\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}