Fatal Error Cannot Use For Reading
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and laravel cannot use [] for reading policies of this site About Us Learn more about Stack Overflow the
Array_push
company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users array push Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes php array a minute: Sign up Cannot use [] for reading up vote 21 down vote favorite 3 In one of my scripts, I try to do the following $data[] = self::get($row['sr_id']); // <-- line 55 However, PHP does not allow me to do this, giving me this error Fatal error: Cannot use [] for reading in /file.php on line 55 The self::get function return
Php Foreach
either a bool, or an object. Edit: The get function creates a new object which again loads data from a mysql database. php arrays fatal-error share|improve this question edited Sep 12 at 6:46 Nikhil Vaghla 1,057422 asked Sep 29 '10 at 9:20 eriktm 3171314 2 Check what $row contains –Eiko Sep 29 '10 at 9:23 1 print_r($row) –Filip Ekberg Sep 29 '10 at 9:25 Can you show the surrounding code / the get method? –halfdan Sep 29 '10 at 9:30 1 Try to find out whether the error is about $data[] = or about $row['sr_id']. –Sjoerd Sep 29 '10 at 9:39 we need more context –Quamis Sep 29 '10 at 11:53 | show 2 more comments 4 Answers 4 active oldest votes up vote 11 down vote accepted Old PHP versions accepted $var[] in expressions, allowed reading out the $var content regardless of syntax. PHP 5.1 made that illegal. But sometimes the error is triggered outside of the intented context. So my guess (again: show more code) is that the preceeding line contains an u
Leave a comment May 29, 2015 When to use [] This is a shortcut for array_push. It's easier and even better if you add one element to the array. Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function. If you make a mistake and you try to use this on something else than an array, you will have a warning both case. In the doc, you have something else Note: array_push() will raise a warning if the first argument is not an array. This differs from http://stackoverflow.com/questions/3820258/cannot-use-for-reading the $var[] behaviour where a new array is created. $arr = 5; array_push($arr, 4); // PHP Warning: array_push() expects parameter 1 to be array, integer given $arr = 5; $arr[] = 4; // PHP Warning: Cannot use a scalar value as an array // PHP 5.6.7-1+deb.sury.org~trusty+1 (cli) (built: Mar 24 2015 11:21:10) // Copyright (c) 1997-2015 The PHP Group // Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies // with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2015, https://phpolyk.wordpress.com/2015/05/29/cannot-use-for-reading-what-im-doing-wrong/ by Zend Technologies // with Xdebug v2.3.1, Copyright (c) 2002-2015, by Derick Rethans When not to use [] This is a list of when not to use [] and will result in this error $a = array(); $b = $a[]; /////////////////////////////////// $a = array(); isset($a[]); /////////////////////////////////// function test($var) { } test($arr[]); /////////////////////////////////// $a = array(); foreach($a[] as $b) { } /////////////////////////////////// $a = array(); foreach($a[]['test'] as $b) { } /////////////////////////////////// $a = array(); foreach($a['test'][] as $b) { } /////////////////////////////////// You can find more example here The problem i had I came accros some code trying to use variables variables and []. Since the precedence is kind of blurry here, i got this error in this particular scenario. $test = 'var'; $name = 'test' . $test; $testvar = array(); $$name[] = 3; // Add something to $testvar // PHP Fatal error: Cannot use [] for reading The solution 1- Use array_push : array_push($$name, 3); 2- Use {} to force precedence : ${$name}[] = 3; What i have found I ran it over 3v4l and it seems to work now on php7@20140901 - 20150501 Share this:TwitterFacebookGoogleLike this:Like Loading... Tags: array, precedence Comments RSS feed Leave a Reply Cancel reply Enter your comment here... Fill in your details below or click an icon to log in: Email (required) (Address never made public) Name (required) Website You are commenting using your Word
Cannot use [] for reading The SitePoint Forums have moved. You can now find them here. This forum is now closed to new posts, but you can browse existing http://www.sitepoint.com/forums/showthread.php?548963-Fatal-error-Cannot-use-for-reading content. You can find out more information about the move and how to open a new account (if necessary) here. If you get stuck you can get support by emailing forums@sitepoint.com If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start cannot use viewing messages, select the forum that you want to visit from the selection below. SitePoint Sponsor User Tag List Results 1 to 2 of 2 Thread: Fatal error: Cannot use [] for reading Thread Tools Show Printable Version Subscribe to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode May 17, 2008,23:03 #1 Shaun(OfTheDead) View Profile View Forum Posts Keep Moving Forward Join fatal error cannot Date Nov 2005 Location Trinidad Posts 3,746 Mentioned 45 Post(s) Tagged 0 Thread(s) Fatal error: Cannot use [] for reading Fatal error: Cannot use [] for reading Has anyone ever seen that error message before? Anyone know what causes it ? I'm writing a PHP function and creating an array of the HTML output which I intended to implode at the end and return, but for some odd reason that error message keeps coming up at the implode line. I've double checked my syntax about fifteen times and everything seems right. Here's a simple function to illustrate... Code PHP: function deBugger() { $output_Debugger[] = '
'; $output_Debugger[] = 'Hello World'; $output_Debugger[] = '
'; echo implode("\n",$output_Debugger[]); } Am I missing something obvious? I have no idea why this souldn't be working. Thanks a lot in advance. Trying to fill the unforgiving minute with sixty seconds' worth of distance run. Update on Sitepoint's Migration to Discourse May 17, 2008,23:10 #2 Shaun(OfTheDead) View Profile View Forum Posts Keep Moving Forward Join Date Nov 2005 Location Trinidad Posts 3,746 Mentioned 45 Post(s) Tagged 0 Thread(s) hahaha Okay I figured it out. This... Code PHP: echo implode("\n",$output_Debugger[]); Should be this... Code PHP: echo implode("