Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP "Use of undefined constant length" Array to string conversion

Can someone tell me why the constant length isn't defined? I'm using glob to find and put all .txt files into an array then using for to loop through them echoing them out.

$filew = glob("/xampp/htdocs/new/*.txt"); 
for($n=0; $n<$filew.length; $n++) { 
  $filew['$n'] = substr($filew['$n'], 18, -4);
  echo $filew['n'] . " ";
}

The error message indicates the problem lines in the second line, someone told me to put array content into quote marks but it hasn't worked. Thank you

like image 329
user3063481 Avatar asked Feb 16 '26 23:02

user3063481


1 Answers

PHP arrays don't have a length property. You should use count instead

$filew = glob("/xampp/htdocs/new/*.txt"); 
for($n = 0; $n < count($filew); $n++) { 
  $filew[$n] = substr($filew[$n], 18, -4);
  echo $filew[$n] . " ";
}

You also have some errors in the way you are accessing your array. Using a single quote will cause the variable inside to not be rendered. Using $filew['$n'] will not give you a numeric value for $n. It will give a literal string with the value $n. Remove the single quotes should be just fine.

like image 163
Robbert Avatar answered Feb 19 '26 12:02

Robbert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!