9 Replies - 713 Views - Last Post: 24 December 2008 - 07:08 AM Rate Topic: -----

#1 midasxl  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 193
  • Joined: 03-December 08

PHP Loop Code Defined

Post icon  Posted 23 December 2008 - 08:45 AM

Hello and thanks for your time. In the following code there is a bit I do not understand.

for($i=0; $i<count($imagefiles); $i++){
// print will then output one or more strings
		print $imagefiles[$i].'|';
	}



What does print $imagefiles[$i].'|'; mean?

Thanks!!
Is This A Good Question/Topic? 0
  • +

Replies To: PHP Loop Code Defined

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3034
  • Posts: 10,593
  • Joined: 08-August 08

Re: PHP Loop Code Defined

Posted 23 December 2008 - 08:50 AM

Output item number $i of the array $imagefiles and then output the character |.
Was This Post Helpful? 1
  • +
  • -

#3 midasxl  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 193
  • Joined: 03-December 08

Re: PHP Loop Code Defined

Posted 23 December 2008 - 08:55 AM

View PostCTphpnwb, on 23 Dec, 2008 - 07:50 AM, said:

Output item number $i of the array $imagefiles and then output the character |.


Thank you! What do you suppose is the purpose of the character | ?

And while I have your attention, which I greatly appreciate, what is the meaning of this...

$file_dir = '.';



Thanks!

This post has been edited by midasxl: 23 December 2008 - 08:57 AM

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3034
  • Posts: 10,593
  • Joined: 08-August 08

Re: PHP Loop Code Defined

Posted 23 December 2008 - 09:42 AM

hard to say from the code provided.

The '|' could be a delimiter, and the '.' might be the current directory.
Was This Post Helpful? 0
  • +
  • -

#5 midasxl  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 193
  • Joined: 03-December 08

Re: PHP Loop Code Defined

Posted 23 December 2008 - 10:10 AM

Gotcha, thanks for your response! I appreciate it.
Was This Post Helpful? 0
  • +
  • -

#6 ~NJ  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 52
  • Joined: 22-December 08

Re: PHP Loop Code Defined

Posted 23 December 2008 - 02:44 PM

In the first example,
$imagefiles is an array containing x amount of elements. The for loop simply loops through each value in the array, prints out the contents of each value with a separating "|" character.

EG:
"value1|value2|value3|value4|" et cetera.

In the second example, the variable $file_dir is simply getting set to ".". In terms of file I/O the "." character is used to represent the current directory or subdirectory.
Was This Post Helpful? 1
  • +
  • -

#7 midasxl  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 193
  • Joined: 03-December 08

Re: PHP Loop Code Defined

Posted 24 December 2008 - 06:06 AM

Thanks very much for the explanations. Makes perfect sense. In my research I have learned that the separator "|" is called a pipe.

Thank you!!
Was This Post Helpful? 0
  • +
  • -

#8 midasxl  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 193
  • Joined: 03-December 08

Re: PHP Loop Code Defined

Posted 24 December 2008 - 06:33 AM

OK, just one more and I promise to stop bothering everyone. I am simply trying to get my head around this piece of php code that runs an image gallery.

Check this out

substr($file, strpos($file, ".")+1)


Here's what I understand about this piece...

This is a function that returns part of a string. The string in this case is the variable $file, and the start point of the part to return is determined by a strpos function.

Now this strpos function returns the position of the first occurrence of a string inside another string, in this case the string to search is $file, and the string to find is the current directory plus (+) 1.

So what this is looking for is the position of the current directory within the $file variable + 1.

That makes sense to me on paper, just following the logical flow, but what does it mean? What does this actually accomplish and why?
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,548
  • Joined: 23-August 08

Re: PHP Loop Code Defined

Posted 24 December 2008 - 06:46 AM

Most likely it extacts the extension from the file name. If there are other '.' characters in the file name, though, it's not going to work as expected.
$file1 = '123.jpg';
$file2 = '123.456.jpg';
$dotLocation = strpos($file1, '.'); // $dotLocation = 3
$extension = substr($file1, $dotLocation + 1); // + 1 to go one character past the dot
print $extension . "\n"; // prints jpg

$dotLocation = strpos($file2, '.'); // $dotLocation = 3
$extension = substr($file2, $dotLocation + 1); // + 1 to go one character past the dot
print $extension . "\n"; // prints 456.jpg


Better would be to use strrpos, search for the first '.' from the end of the string, toward the front, and to use error checking:
$file1 = '123.jpg';
$file2 = '123.456.jpg';
$dotLocation = strrpos($file1, '.'); // $dotLocation = 3
if ($dotLocation > 0)
{
    $extension = substr($file1, $dotLocation + 1); // + 1 to go one character past the dot
    print $extension . "\n"; // prints jpg
}
else
{
    print "No '.' found in filename\n";
}

$dotLocation = strrpos($file2, '.'); // $dotLocation = 7
if ($dotLocation > 0)
{
    $extension = substr($file2, $dotLocation + 1); // + 1 to go one character past the dot
    print $extension . "\n"; // prints jpg
}
else
{
    print "No '.' found in filename\n";
}

Was This Post Helpful? 1
  • +
  • -

#10 midasxl  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 193
  • Joined: 03-December 08

Re: PHP Loop Code Defined

Posted 24 December 2008 - 07:08 AM

Ah, so the "." in this context does not refer to the shorthand for current directory, it actually references dot. I was not sure on that one. Ok, great! Thanks for your response!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1