Old 08-12-2003, 02:41   #1 (permalink)
gwx
Dr. Macromedia
 
gwx's Avatar
 
Join Date: Dec 2003
Location: Alpha Centauri
Posts: 106
Send a message via MSN to gwx
PHP Loop - Syntax

This should be easy for you all. This is just a small chunk of code and should take no time to figure out for PHP experts. I'm also just starting out in PHP.

$counter = 1;
while ( $counter <= 12 )

{ print "$counter times 2 is ".($counter*2)."<br>";
$counter++
}

Basically, I don't know what the ".($counter^2)."<br>" thing does.

From what I understand..

First Line :
$counter is declared and set the value of "1"

Second Line :
Whenever the while statement expression is true. That is, whenver $counter contains a number that is smaller than or equal to 12, the below code block enclosed in {} will run on and on.

Third line :
I don't understand that. its like all gibberish to me. From what I currently know, it prints ( or outputs ) the value inside the " " parentheses. And counter*2 is definitely the variable $counter x 2 ( multplied by 2 )

Fourth Line:
Increment ( add 1 ) to counter

Last line :
End code block
  Reply With Quote
Old 08-12-2003, 03:09   #2 (permalink)
smallbeer
I Ain't Losing Any Sleep™
 
Join Date: Apr 2003
Posts: 5,240
Well mik, I mean gwx, here you go.

In PHP
$counter = 1
while ( $counter <= 12 )
{ print "$counter times 2 is ".($counter*2)."<br>";
$counter++
}
In Plain English
Counter = 1
If 1 is less than or equal to 12
Print - 1 times 2 is 2
Add 1 to Counter

Loop again...
Counter now equals 2
If 2 is less than or equal to 12 blah blah blah
__________________
That's fuckin' ingenious, if I understand it correctly. It's a Swiss fuckin' watch.
  Reply With Quote
Old 08-12-2003, 03:28   #3 (permalink)
gwx
Dr. Macromedia
 
gwx's Avatar
 
Join Date: Dec 2003
Location: Alpha Centauri
Posts: 106
Send a message via MSN to gwx
Thanks smallbeer, but I still don't really get part of it.

PHP is asked to print "$counter times 2 is ".($counter*2)."<br>"

I understand that $counter would evaluate to 1

Thus, PHP is to print

1 times 2 is ".($counter)*2."<br>

What does the brackets enclosing $counter above do? I'm not sure of what the dot do, but its supposed to connect the left and right strings together. Therefore,

1 times 2 is "($counter)^2}"<br>

What is that that pair of " signs do? and what does the bracket tell php to do? Is it possible that the " " signs tell php that the stuff inside are to be evaluated ( is an expression )?

I'm so confused. Thnx in advnce.
  Reply With Quote
Old 08-12-2003, 04:51   #4 (permalink)
smallbeer
I Ain't Losing Any Sleep™
 
Join Date: Apr 2003
Posts: 5,240
I've been biting all weekend. Today I finally saw the light.
__________________
That's fuckin' ingenious, if I understand it correctly. It's a Swiss fuckin' watch.
  Reply With Quote
Old 08-12-2003, 10:18   #5 (permalink)
gwx
Dr. Macromedia
 
gwx's Avatar
 
Join Date: Dec 2003
Location: Alpha Centauri
Posts: 106
Send a message via MSN to gwx
Saw the light???
__________________
Macintosh - Style
Windows - Monotony-polyzed
  Reply With Quote
Old 09-12-2003, 02:39   #6 (permalink)
gwx
Dr. Macromedia
 
gwx's Avatar
 
Join Date: Dec 2003
Location: Alpha Centauri
Posts: 106
Send a message via MSN to gwx
Did I post to the wrong area or is everyone here just designers ( and not php programmers )
__________________
Macintosh - Style
Windows - Monotony-polyzed
  Reply With Quote
Old 09-12-2003, 04:16   #7 (permalink)
sleepingfish
css is for divs
 
sleepingfish's Avatar
 
Join Date: Feb 2003
Location: Norwich
Posts: 4,527
Do a poll
  Reply With Quote
Old 09-12-2003, 04:20   #8 (permalink)
oli
I Call Shenanigans™
 
oli's Avatar
 
Join Date: Feb 2003
Location: Manchester
Posts: 9,651
please dont encourage him

gwx , go here - http://www.programmingtalk.com , Ive put in a good word for you.
__________________
Linked In :: Last.fm
..................
  Reply With Quote
Old 09-12-2003, 04:40   #9 (permalink)
gwx
Dr. Macromedia
 
gwx's Avatar
 
Join Date: Dec 2003
Location: Alpha Centauri
Posts: 106
Send a message via MSN to gwx
Quote:
Originally Posted by oli
please dont encourage him

gwx , go here - http://www.programmingtalk.com , Ive put in a good word for you.

Hi. Thanks for the link!
  Reply With Quote
Old 12-12-2003, 10:56   #10 (permalink)
philster
Registered User
 
Join Date: Jun 2003
Posts: 6
Quote:
Originally Posted by gwx
Thanks smallbeer, but I still don't really get part of it.

PHP is asked to print "$counter times 2 is ".($counter*2)."<br>"

I understand that $counter would evaluate to 1

Thus, PHP is to print

1 times 2 is ".($counter)*2."<br>

What does the brackets enclosing $counter above do? I'm not sure of what the dot do, but its supposed to connect the left and right strings together. Therefore,

1 times 2 is "($counter)^2}"<br>

What is that that pair of " signs do? and what does the bracket tell php to do? Is it possible that the " " signs tell php that the stuff inside are to be evaluated ( is an expression )?

I'm so confused. Thnx in advnce.

The full stop allows you to add something to a string in PHP. So assuming $counter is 2...

PHP Code:
echo "$counter times 2 is ".($counter*2)."<br>" 

will actually write

Code:
2 times 2 is 4<br>

Effectively, everything inside the " " is echosed to the screen, but you obviously want to calculate the value of $counter*2 before you echo it, so you close the brackets, add the evaluated answer to the string (using the .) and then finish the string off with a <br> tag.

IF you try this:

Code:
$a = "Ruud"; $b = "Van"; $c = "Nistlerooy";

then

PHP Code:
echo $a.$b.$c

will display

Code:
RuudVanNistlerooy

whereas

PHP Code:
echo $a " " $b $c

will display

Code:
Ruud VanNistlerooy

or

PHP Code:
echo $a "<br>" $b "<br><br>" $c

will display

Code:
Ruud Van Nistlerooy

Not explained in an even remotely eloquent fashion... but it's the best I can do. Friday avo, and Thursday night drinkies are starting to take their toll...

Phil
  Reply With Quote
Old 12-12-2003, 13:18   #11 (permalink)
dan
Iris Folder
 
dan's Avatar
 
Join Date: Apr 2003
Location: smokey
Posts: 2,676
phil, does the php actually evaluate the $counter in the string or just print it?

"$counter times 2 is ".($counter*2)."<br>"

to

$counter times 2 is 4 <br> ?

I know this is how it would work in Actionscritp / javascript, but it's been a while since I did any PHP - bit rusty.
  Reply With Quote
Old 12-12-2003, 16:08   #12 (permalink)
ThePhilster
Registered User
 
Join Date: Jun 2003
Location: Belfast
Posts: 43
Quote:
Originally Posted by dan
phil, does the php actually evaluate the $counter in the string or just print it?

"$counter times 2 is ".($counter*2)."<br>"

to

$counter times 2 is 4 <br> ?

I know this is how it would work in Actionscritp / javascript, but it's been a while since I did any PHP - bit rusty.

As far as I know (still getting into PHP myself at the mo) it needs to evaluate the statement inside the brackets before it can echo it...
  Reply With Quote
Old 13-12-2003, 09:37   #13 (permalink)
dan
Iris Folder
 
dan's Avatar
 
Join Date: Apr 2003
Location: smokey
Posts: 2,676
S'ok I tested it, and my result cuncured with yours.

I wasn't asking about the multiplied $counter but the $ counter contained in the first part of the statement -

"$counter times 2 is"

- anyways the answer is that it does evaluate it even though it's in a string and I was wrong.

yay me
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8